matplotlib for loop to show, save and redraw all plots - python

Here's my python code,
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
from matplotlib.pyplot import savefig
a = np.genfromtxt('do_cv.csv', skiprows = 1, delimiter = ',')
for i in xrange(2):
t = a[i+1:(i+1)*60, 2]
z = a[i+1:(i+1)*60, 3]
est_z = a[i+1:(i+1)*60, 6]
figure(i+1)
plt.plot(t, z, 'bo-', t, est_z, 'go-')
plt.xlabel('time')
plt.ylabel('data value')
plt.grid(True)
plt.legend(['sample data', 'estimated sample data'])
plt.savefig('test + str(i).png')
plt.show()
then 2 windows come out, like this,
figure 2 contains plots of figure 1, how to redraw the plot before the second loop begins?
And I only got 1 png file saved in my folder.
How to modify my code and get the result I want? Please give me some suggestions, thanks a lot.

You should write your self a helper function:
def my_plotter(ax, t, z, est_z):
ln1 = ax.plot(t, z, 'bo-', label='sample data')
ln2 = ax.plot(t, est_z, 'go-', label='estimated sample data')
ax.xlabel('time')
ax.ylabel('data value')
ax.grid(True)
ax.legend()
return ln1 + ln2
for i in xrange(2):
# get the data
t = a[i+1:(i+1)*60, 2]
z = a[i+1:(i+1)*60, 3]
est_z = a[i+1:(i+1)*60, 6]
# make the figure
fig, ax = plt.subplots()
# do the plot
my_plotter(ax, t, z, est_Z)
# save
fig.savefig('test_{}.png'.format(i))
Now if you decide you want to put both of these is one figure as sub-plots, all you have to do is:
# make one figure with 2 axes
fig, ax_lst = plt.subplots(1, 2)
for i, ax in zip(xrange(2), ax_lst):
# get the data
t = a[i+1:(i+1)*60, 2]
z = a[i+1:(i+1)*60, 3]
est_z = a[i+1:(i+1)*60, 6]
# do the plot
my_plotter(ax, t, z, est_Z)
# save the figure with both plots
fig.savefig('both.png')

You overwrite your png file every iteration of the loop, that's why you only have one.
plt.savefig('test + str(i).png')
Should be
plt.savefig('test ' + str(i) + '.png')

Related

How to assign a plot to a variable and use the variable in a multiplot in Python

I would like to have a function which create a plot. Once I have the plot, I would like to use that in a multiplot.
For example, I could create the following function:
def fig_1(x):
# create a new figure
fig = plt.figure()
plt.plot([1*x, 2*x, 3*x, 4*x])
return fig
after that I would like something like:
subplot(3,2,1) = fig_1(1)
subplot(3,2,2) = fig_1(2)
subplot(3,2,3) = fig_1(3)
subplot(3,2,4) = fig_1(4)
subplot(3,2,5) = fig_1(5)
subplot(3,2,6) = fig_1(6)
In order to plot the final plot:
from pylab import *
pdf = matplotlib.backends.backend_pdf.PdfPages("Cal8010.pdf")
for fig in xrange(1,figure().number):
In this way, it does not work. Could I do what I have in mind?
Thanks for any kind of help
First: I create subplots and in each one a plot:
import matplotlib.pyplot as plt
import numpy as np
def fig_1(ax, x, y):
ax.plot(x, y)
fig, ax = plt.subplots(3, 2)
for i in range(3):
for j in range(2):
x = np.random.random(10)
y = np.random.random(10)
fig_1(ax[i, j], x, y)
ax[i, j].set_title(f"Subplot #{2*i + j + 1}")
plt.show()
Now, you can also plot an empty array and further update datas to this plot:
import matplotlib.pyplot as plt
import numpy as np
def fig_1(ax):
line, = ax.plot([], [])
return line
fig, ax = plt.subplots(3, 2)
lines = []
for i in range(3):
for j in range(2):
x = np.random.random(10)
y = np.random.random(10)
lines.append((fig_1(ax[i, j]), x, y))
ax[i, j].set_title(f"Subplot #{2*i + j + 1}")
for p in lines:
l, x, y = p
l.set_xdata(x)
l.set_ydata(y)
fig.canvas.draw()
fig.canvas.flush_events()
plt.show()
but this could be tricky because both axis on each plot are not adapted to the datas so plots can be out of bounds (so you possibly need to fix the x and y limits to min and max of datas)
Dear Reviewer,
here the solution that I have work out thanks to another post that I hope to find again in order to give the right credit to it.
fig, axs = plt.subplots(2,2)
def plot_ff(ax=None,data):
ax.plot(data)
return
plot_ff(axs[0, 0],data_1)
plot_ff(axs[0, 1],data_2)
plot_ff(axs[1, 0],data_3)
plot_ff(axs[0, 1],data_3)
In this way, it works and it is easily manage with different type of multiplot
What do you think about this solution?
Should I erase this questions?
Diego

Creating a modulo/folded plot in Python

I am trying to "fold" an exponential plot (and a fit to it - see the first image below) around a discrete interval on the x-axis (a.k.a a "modulo plot"). The aim is that after 10 x-units the exponential is continued on the same plot from 0 for the 10 to 20 interval, as shown on a second "photoshopped" image below.
The MWE code is below:
import numpy as np
from scipy import optimize
import matplotlib.pyplot as plt
Generate points
x=np.arange(20)
y=np.exp(-x/10)
Fit to data
def fit_func(x, t):
return np.exp(-x/t)
par, pcov = optimize.curve_fit(f=fit_func, xdata=x, ydata=y)
Plot data and fit function
fig, ax = plt.subplots()
ax.plot(x,y, c='g', label="Data");
ax.plot(x,fit_func(x, par), c='r', linestyle=":", label="Fit");
ax.set_xlabel("x (modulo 10)")
ax.legend()
plt.savefig("fig/mod.png", dpi=300)
What I have: Origianl exponential from 0 to 20
What I want: Modulo/folded exponential in intervals of 10
You could try to simply write:
ax.plot(x % 10,y, c='g', label="Data")
ax.plot(x % 10, f, c='r', linestyle=":", label="Fit")
but then you get confusing lines connecting the last point of one section to the first point of the next.
Another idea is to create a loop to plot every part separately. To avoid multiple legend entries, only the first section sets a legend label.
import numpy as np
from scipy import optimize
import matplotlib.pyplot as plt
x=np.arange(40)
y=np.exp(-x/10)
def fit_func(x, t):
return np.exp(-x/t)
par, pcov = optimize.curve_fit(f=fit_func, xdata=x, ydata=y)
f = fit_func(x, par)
fig, ax = plt.subplots()
left = x.min()
section = 1
while left < x.max():
right = left+10
filter = (x >= left) & (x <= right)
ax.plot(x[filter]-left,y[filter], c='g', label="Data" if section == 1 else '')
ax.plot(x[filter]-left, f[filter], c='r', linestyle=":", label="Fit" if section == 1 else '')
left = right
section += 1
ax.set_xlabel("x (modulo 10)")
ax.legend()
#plt.savefig("fig/mod.png", dpi=300)
plt.show()
Assuming that x is a sorted array, we'll have :
>>> y_ = fit_func(x, par)
>>> temp_x = []
>>> temp_y = []
>>> temp_y_ = []
>>> fig, ax = plt.subplots()
>>> for i in range(len(x)):
if x[i]%10==0 or i == len(x)-1:
ax.plot(temp_x,temp_y, c='g', label="Data");
ax.plot(temp_x,temp_y_, c='r', linestyle=":", label="Fit")
temp_x,temp_y,temp_y_ = [],[],[]
else:
temp_x.append(x[i]%10)
temp_y.append(y[i])
temp_y_.append(y_[i])
>>> plt.show()
and this would be the resulting plot :

How to change marker size/scale in legend when marker is set to pixel

I am scatter ploting data points with a very small marker (see screengrab below). When I use the very small marker ',' the legend is very hard to read (example code taken from here).
(Python 3, Jupyter lab)
How can I increase the size of the marker in the legend. The two versions shown on the above mentioned site do not work:
legend = ax.legend(frameon=True)
for legend_handle in legend.legendHandles:
legend_handle._legmarker.set_markersize(9)
and
ax.legend(markerscale=6)
The two solutions do however work when the marker is set to '.'.
How can I show bigger makers in the legend?
Sample Code from intoli.com:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
for i in range(5):
mean = [np.random.random()*10, np.random.random()*10]
covariance = [ [1 + np.random.random(), np.random.random() - 1], [0, 1 + np.random.random()], ]
covariance[1][0] = covariance[0][1] # must be symmetric
x, y = np.random.multivariate_normal(mean, covariance, 3000).T
plt.plot(x, y, ',', label=f'Cluster {i + 1}')
ax.legend(markerscale=12)
fig.tight_layout()
plt.show()
You can get 1 pixel sized markers for a plot by setting the markersize to 1 pixel. This would look like
plt.plot(x, y, marker='s', markersize=72./fig.dpi, mec="None", ls="None")
What the above does is set the marker to a square, set the markersize to the ppi (points per inch) divided by dpi (dots per inch) == dots == pixels, and removes lines and edges.
Then the solution you tried using markerscale in the legend works nicely.
Complete example:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
for i in range(5):
mean = [np.random.random()*10, np.random.random()*10]
covariance = [ [1 + np.random.random(), np.random.random() - 1], [0, 1 + np.random.random()], ]
covariance[1][0] = covariance[0][1] # must be symmetric
x, y = np.random.multivariate_normal(mean, covariance, 3000).T
plt.plot(x, y, marker='s', markersize=72./fig.dpi, mec="None", ls="None",
label=f'Cluster {i + 1}')
ax.legend(markerscale=12)
fig.tight_layout()
plt.show()
According to this discussion, the markersize has no effect when using pixels (,) as marker. How about generating a custom legend instead? For example, by adapting the first example in this tutorial, one can get a pretty decent legend:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
np.random.seed(12)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
for i in range(5):
mean = [np.random.random()*10, np.random.random()*10]
covariance = [ [1 + np.random.random(), np.random.random() - 1], [0, 1 + np.random.random()], ]
covariance[1][0] = covariance[0][1] # must be symmetric
x, y = np.random.multivariate_normal(mean, covariance, 3000).T
plt.plot(x, y, ',', label=f'Cluster {i + 1}')
##generating custom legend
handles, labels = ax.get_legend_handles_labels()
patches = []
for handle, label in zip(handles, labels):
patches.append(mpatches.Patch(color=handle.get_color(), label=label))
legend = ax.legend(handles=patches)
fig.tight_layout()
plt.show()
The output would look like this:

Plotting Acclerometer Data With Respect To Time

So I am trying to plot accelerometer data with regards to time, my csv reads like this(columns -> time, x, y, z):
1518999378635,2.275090217590332,8.601768493652344,3.691260576248169
1518999378653,2.38462495803833,8.633491516113281,4.0964789390563965
1518999378658,2.449866771697998,8.506000518798828,4.082113742828369
1518999378667,2.4372973442077637,8.166622161865234,4.016273498535156
1518999378675,1.8381483554840088,8.848969459533691,4.086902141571045
1518999378681,1.1402385234832764,8.762179374694824,4.225766181945801
1518999378688,1.7818846702575684,8.652046203613281,3.6110546588897705
1518999378694,2.076371431350708,8.80467700958252,4.0527849197387695
1518999378700,2.3720552921295166,8.471882820129395,4.120420932769775
My initial bet (as given below!) was to use a scatter with time as color, however the output is, well, not very obvious.
from numpy import genfromtxt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
if __name__ == "__main__":
print("Plotting Accelerometer Data")
acm_data = genfromtxt("acm_data.csv", delimiter=',', names="time, acc_x, acc_y, acc_z")
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = acm_data["acc_x"]
y = acm_data["acc_y"]
z = acm_data["acc_z"]
c = acm_data["time"]
ax.scatter(x, y, z, c=c, cmap=plt.hot())
plt.show()
The output looks viz:
and is not very interpretable. What would be the best way to handle this?
Thanks.
Something like this:
import matplotlib.pyplot as plt
x = [0, 1, 2, 3]
x_accel = [5, 6, 3, 4]
y_accel = [2, 7, 6, 8]
z_accel = [1, 2, 3, 4]
plt.subplot(3, 1, 1)
plt.plot(x, x_accel, '.-')
plt.title('A tale of 3 subplots')
plt.ylabel('X acceleration')
plt.subplot(3, 1, 2)
plt.plot(x, y_accel, '.-')
plt.xlabel('time (s)')
plt.ylabel('Y acceleration')
plt.subplot(3, 1, 3)
plt.plot(x, z_accel, '.-')
plt.xlabel('time (s)')
plt.ylabel('Z acceleration')
plt.show()
Generates:
Of course you'll have to mess with your axes and what not to make the presentation of your data as clear as possible. But in general, this is much clearer than what is posted in your question.
Well, here's my answer (break it into 3 2-dimensional plots):
from numpy import genfromtxt
import matplotlib.pyplot as plt
import numpy as np
if __name__ == "__main__":
print("Plotting Accelerometer Data")
acm_data = genfromtxt("acm_data.csv", delimiter=',', names="time, acc_x, acc_y, acc_z")
fig = plt.figure()
x = acm_data["acc_x"]
y = acm_data["acc_y"]
z = acm_data["acc_z"]
t = acm_data["time"]
for dat, num, axis in zip((x,y,z), range(311, 314), "XYZ"):
plt.subplot(num)
plt.plot(t, dat, ".")
plt.title("%s-axis" %axis)
plt.show()
Which gave me this as the visual output:
Visual output
Which is more readable that color-codes.
Notes:
1) If you want to connect them, remove the "." or change it to "-"
2) This was on Python 3.4
3) If you wanted, you could also add labels on the left and bottom of the graphs.

How to draw vertical lines interactively in matplotlib?

I have a time series plot and I need to draw a moving vertical line to show the point of interest.
I am using the following toy example to accomplish the same. However, it prints all the lines at the same time while I wanted to show these vertical line plotting one at a time.
import time
ion() # turn interactive mode on
# initial data
x = arange(-8, 8, 0.1);
y1 = sin(x)
y2 = cos(x)
line1, = plt.plot(x, y1, 'r')
xvals = range(-6, 6, 2);
for i in xvals:
time.sleep(1)
# update data
plt.vlines(i, -1, 1, linestyles = 'solid', color= 'red')
plt.draw()
If I understood well, you want to use the animation tools of matplotlib. An example (adapted from the doc):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
X_MIN = -6
X_MAX = 6
Y_MIN = -1
Y_MAX = 1
X_VALS = range(X_MIN, X_MAX+1) # possible x values for the line
def update_line(num, line):
i = X_VALS[num]
line.set_data( [i, i], [Y_MIN, Y_MAX])
return line,
fig = plt.figure()
x = np.arange(X_MIN, X_MAX, 0.1);
y = np.sin(x)
plt.scatter(x, y)
l , v = plt.plot(-6, -1, 6, 1, linewidth=2, color= 'red')
plt.xlim(X_MIN, X_MAX)
plt.ylim(Y_MIN, Y_MAX)
plt.xlabel('x')
plt.ylabel('y = sin(x)')
plt.title('Line animation')
line_anim = animation.FuncAnimation(fig, update_line, len(X_VALS), fargs=(l, ))
#line_anim.save('line_animation.gif', writer='imagemagick', fps=4);
plt.show()
Resulting gif looks like this:
Could you try calling plt.draw after plt.vlines? plt.draw is used to interactively redraw the figure after its been modified.

Categories