Issue
I am using the code below to use matplotlib to plot a diagram for a huge number of data.
def plot_en(filename):
data_1 = np.loadtxt(f"{filename[0]}.en")
data_2 = np.loadtxt(f"{filename[1]}.en")
fig, (ax1, ax2) = plt.subplots(1, 2)
plt.subplots_adjust(top = 1, bottom = 0.08, left = 1.5, right = 3.5, hspace = 0.25, wspace = 0.35)
ax1.plot(data_1[:, 0], data_1[:, 1])
ax2.plot(data_2[:, 0], data_2[:, 1])
plot_en(["forward1-on", "forward2-on"])
My diagram looks like this:
However, I need a more accurate diagram something like this:
Is there any way that I can make lines in my plot more distinguishable? or what change should i do on my code
Solution
A simple solution is using moving average [wiki].
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(1, 100, 1001)
data_trend = 1.0 / np.exp(x / 100.0)
data_real = data_trend + (np.random.rand(len(x)) - 0.5) / 5.0
window = 20
data_ma = np.convolve(data_real, np.ones(window), 'valid') / window
fig, ax = plt.subplots()
ax.plot(data_real, c='orange', label='data_real')
ax.plot(data_trend, c='blue', label='data_trend')
ax.plot(data_ma, c='red', label='data_ma')
The one-line moving average calculation code is referenced from How to calculate rolling / moving average using python + NumPy / SciPy?
You can search similar methods using keywords "noise", "smoothing", and others.
Answered By - J. Choi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.