I was able to plot animated graphs using advice from the link below.
matplotlib animation multiple datasets
And my code is here.
tt = time_hr.values[:,0]
yy1 =data1.values[:,0]
yy2 =data2.values[:,0]
fig, ax = plt.subplots()
line1, = ax.plot(tt, yy1, color='k')
line2, = ax.plot(tt, yy2, color='r')
def update(num, tt, yy1, yy2, line1, line2):
line1.set_data(tt[:num], yy1[:num])
line1.axes.axis([0, 1, 0, 2500])
line2.set_data(tt[:num], yy2[:num])
line2.axes.axis([0, 1, 0, 2500])
return line1,line2
ani = animation.FuncAnimation(fig, update, len(time_hr), fargs=[tt, yy1, yy2, line1,line2],interval=25, blit=True)
plt.show()
I also have other data set for bar graph and I created animated bar plot using the code below.
x_pos = np.arange(95)
fig = plt.figure()
ax = plt.axis((-1,96,0,1000))
def animate(i):
plt.clf()
pic = plt.bar(x_pos, data3.iloc[i,:], color='c')
plt.axis((-1,96,0,1000))
return pic,
ani = animation.FuncAnimation(fig, animate, interval=25, repeat=False)
plt.show()
My ultimate goal is to plot both animated bar and line graph in one figure using subplot function so that bar graph will be (1,1) and the line graph will be at (2,1) position of the figure.
Could somebody help me to create animated bar and line graphs in one figure window in python ?
More specifically, how to combine both line graphs and bar graph in one animate function ?
Based on comments below, I modified the code like this.
x_pos = np.arange(95)
tt = time_hr.values[:,0]
yy1 =data1.values[:,0]
yy2 =data2.values[:,0]
fig, (ax1, ax2) = plt.subplots(nrows=2)
line1, = ax2.plot(tt, yy1, color='k')
line2, = ax2.plot(tt, yy2, color='r')
rects = ax1.bar(x_pos, data3.iloc[0,:], color='c')
def update(num, tt, yy1, yy2, x_pos, data3, line1, line2, rects):
line1.set_data(tt[:num], yy1[:num])
line1.axes.axis([0, 1, 0, 2500])
line2.set_data(tt[:num], yy2[:num])
line2.axes.axis([0, 1, 0, 2500])
ax1.clear()
rects= ax1.bar(x_pos, data3.iloc[num,:], color='c')
ax1.axis((-1,96,0,1000))
return line1,line2, rects
ani = animation.FuncAnimation(fig, update, len(time_hr), fargs=[tt, yy1, yy2, x_pos,data3, line1,line2,rects], interval=25, blit=True)
plt.show()
But I got error message like this.
"AttributeError: 'BarContainer' object has no attribute 'set_animated'"
Could you help me How to fix this error ?
Related
I create two scatterplots with matplotlib in python with this code, the data for the code is here:
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
fig = plt.figure(figsize=(20,12))
ax1 = fig.add_subplot(111)
ax3 = ax1.twinx()
norm = Normalize(vmin=0.95*min(arr), vmax=1.05*max(arr))
ax1.scatter(x, y1, s=20, c=arr, cmap='Blues_r', norm=norm, marker='x', label='bla1')
ax3.scatter(x, y2, s=(20*(1.1-arr))**3.5, c=arr, cmap='Reds_r', norm=norm, marker='^', label='bla1')
The created fig. looks like this:
So, the dot size (in ax3) and the dot colour (in ax1 and ax3) are taken from arrays containing floats with all kinds of values in the range [0,1]. My question: How do I create a legend that displays the corresponding y-values for, let's say 5 different dot sizes and 5 different colour nuances?
I would like the legend to look like in the figure below (source here), but with the colour bar and size bar put into a single legend, if possible. Thanks for suggestions and code!
# using your data in dataframe df
# create s2
df['s2'] = (20*(1.1-df.arr))**3.5
fig = plt.figure(figsize=(20,12))
ax1 = fig.add_subplot(111)
ax3 = ax1.twinx()
norm = Normalize(vmin=0.95*min(df.arr), vmax=1.05*max(df.arr))
p1 = ax1.scatter(df.x, df.y1, s=20, c=df.arr, cmap='Blues_r', norm=norm, marker='x')
fig.colorbar(p1, label='arr')
p2 = ax3.scatter(df.x, df.y2, s=df.s2, c=df.arr, cmap='Reds_r', norm=norm, marker='^')
fig.colorbar(p2, label='arr')
# create the size legend for red
for x in [15, 80, 150]:
plt.scatter([], [], c='r', alpha=1, s=x, label=str(x), marker='^')
plt.legend(loc='upper center', bbox_to_anchor=(1.23, 1), ncol=1, fancybox=True, shadow=True, title='s2')
plt.show()
There's no legend for p1 because the size is static.
I think this would be better as two separate plots
I used Customizing Plot Legends: Legend for Size of Points
Separate
fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(20, 10))
norm = Normalize(vmin=0.95*min(df.arr), vmax=1.05*max(df.arr))
p1 = ax1.scatter(df.x, df.y1, s=20, c=df.arr, cmap='Blues_r', norm=norm, marker='x')
fig.colorbar(p1, ax=ax1, label='arr')
p2 = ax2.scatter(df.x, df.y2, s=df.s2, c=df.arr, cmap='Reds_r', norm=norm, marker='^')
fig.colorbar(p2, ax=ax2, label='arr')
# create the size legend for red
for x in [15, 80, 150]:
plt.scatter([], [], c='r', alpha=1, s=x, label=str(x), marker='^')
plt.legend(loc='upper center', bbox_to_anchor=(1.2, 1), ncol=1, fancybox=True, shadow=True, title='s2')
plt.show()
I was trying to make a graph with two secondary vertical axis (y-axis), with python matplotlib.
I was using twinx() method, where one of the two new axis is with the default behavior (labels on the right)
and the other with labels on the left, like the example of tan(x) on the figure bellow (created in an specific software).
Is there an easy way to do that? I'm not restricted to use the twinx() method, if there is another way
Here is a way to add two secondary y-axis, one towards the inside:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(.5, 10, 1000)
y1 = np.cos(x)
y2 = np.sin(2 * x)
y3 = np.clip(np.tan(x * .6), -75, 75)
fig, ax1 = plt.subplots()
color = 'dodgerblue'
ax1.set_ylabel('$cos(x)$', color=color)
ax1.plot(x, y1, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx()
color = 'limegreen'
ax2.set_ylabel('$sin(2 x)$', color=color)
ax2.plot(x, y2, color=color)
ax2.tick_params(axis="y", labelcolor=color)
ax3 = ax1.twinx()
color = 'crimson'
ax3.set_ylabel('$tan(.6 x)$', color=color, labelpad=-40)
ax3.plot(x, y3, color=color)
ax3.tick_params(axis="y", labelcolor=color, direction="in", pad=-5)
plt.setp(ax3.get_yticklabels(), ha="right")
ax1.set_xlim(0, 12)
fig.tight_layout()
plt.show()
screen_width = 45
screen_height = 45
plt.rcParams["figure.figsize"] = (screen_width, screen_height)
plt.style.use('fivethirtyeight')
plt.tight_layout()
fig, (ax1, ax2) = plt.subplots(2,1)
ax1.set_title("Voltage vs samples")
ax1.grid()
ax1.set_ylabel("Voltage (V)")
ax1.set_xlabel("Samples")
ax2.set_title("Current vs samples")
ax2.set_ylabel("Current (A)")
ax2.set_xlabel("Samples")
ax2.grid()
# animation def
def animate(i):
plt.cla()
ax1.plot(x_vals, y_vals, 'ro')
ax2.plot(x_vals_1, y_vals_1, 'bo')
plt.tight_layout
ani = FuncAnimation(fig, animate, interval=1000)
The code works somewhat. It does animate and update the data how I want it. However, it creates two figures, the first figure is empty and the second figure is the one being updated. Why is this the case?
When plt.tight_layout() is called prior to the creation of a figure, one is created on-the-fly. To avoid the creation of this figure you can simply call plt.tight_layout() after creating a Figure instance, i.e.
# ...
fig, (ax1, ax2) = plt.subplots(2, 1)
plt.tight_layout()
# ...
Also note that the line
def animate(i):
# ...
plt.tight_layout
Does nothing because the function is not called without the trailing parentheses. If you want to call tight_layout in your animate function it should be
def animate(i):
# ...
plt.tight_layout()
I am trying to make this animated so that the dot and the green line move due to the for loop. This code displays 3 different graphs one below the other. The middle graph has no animation section.
x =lag_range
count = 0
plt.ion()
fig, ax = plt.subplots()
for b in x:
plt.subplot(311)
plt.plot(x,pear_corr, color='b', linewidth=1.5, label ='Pearson')
plt.plot(x,spear_corr, color ='r', linewidth=1.5, label='Spearman')
plt.plot(x[count],pear_corr[count],'yo')
plt.legend()
axes = plt.gca()
plt.ylabel('Correlation coefficients')
plt.xlabel('Lag times /days')
axes.set_xlim([min(lag_list),last])
axes.set_ylim(-1,1)
plt.subplot(312)
plt.plot(x,pear_p_values, color='b', linewidth=1.5)
plt.plot(x,spear_p_values, color ='r', linewidth=1.5)
axes = plt.gca()
plt.ylabel('P values')
plt.xlabel('Lag times /days')
axes.set_xlim([min(lag_list),last])
plt.subplot(313)
ax1 = plt.subplot(313)
x_for_p = range(len(x_prices))
ax1.plot(x_for_p, x_prices, color ='grey', linewidth=1.5)
ax1.set_ylabel('Share price', color ='grey')
ax1.tick_params('y', colors='grey')
ax1.set_xlabel('Days')
axes = plt.gca()
axes.set_xlim([min(lag_list),(2*last)])
ax2 = ax1.twinx()
x_for_den = range(b,(b+len(x_prices)))
ax2.plot(x_for_den, y_planes, color='g', linewidth=1.5)
ax2.set_ylabel('Plane density', color='g')
ax2.tick_params('y', colors='g')
count += 1
plt.pause(2)
plt.draw()
cross_corr2_vis(prices, density_p3)
If you could share a working code or just definitions of variables pear_corr, spear_corr, etc., the following code might have not resulted in this simple animation:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
N_points = 1000
x = np.linspace(0,2.*np.pi,N_points)
y = np.sin(x)
fig, ax = plt.subplots()
ax.set_xlim([0,2.*np.pi])
ax.set_ylim([-1,1])
line, = ax.plot( [],[], lw=2, color='g')
sctr = ax.scatter([],[], s=100, color='r')
def animate(i):
line.set_ydata(y[:i+1]) # update
line.set_xdata(x[:i+1])
sctr.set_offsets((x[i],y[i]))
return line,sctr
ani = animation.FuncAnimation(fig, animate, N_points, interval=5, blit=True)
plt.show()
I wrote the following code below to do the following graph:
fig, ax = plt.subplots(figsize=(8, 6))
ax.patch.set_facecolor('white')
ax.plot(df.index, df.X1.values, 'b',
label='NMA', linewidth=1.5)
ax.set_ylabel('Index')
ax2 = ax.twinx()
ax2.plot(df.index, df.Y.values, 'r--',
label='Rate', linewidth=1.5)
ax2.set_ylabel('Rate')
lines = ax.get_lines() + ax2.get_lines()
lgd = ax.legend(lines, [line.get_label() for line in lines],
loc='lower center', ncol=2, bbox_to_anchor=(0.5, -0.15),
frameon=False)
ax.set_title('Economic Rate and Index',
weight='bold')
for i in range(5):
plt.axvspan(Dates['Peak'][i], Dates['Trough'][i],
facecolor='grey', alpha=0.5)
plt.grid(False)
plt.savefig('C:\\test.pdf',
bbox_extra_artists=(lgd,), bbox_inches='tight')
I am having a hard time to reproduce this figure in a subplot (2X2). The only thing I would change in each of the subplots is the blue line (X1 in df... for X2, X3...). How can I have a 2X2 subplot of the above graph? Of Course I would only keep one legend at the bottom of the subplots. Thanks for the help.
The data is here and the "Dates" to reproduce the gray bars here.
This is how you could create a 2x2 raster with twinx each:
import matplotlib.pyplot as plt
fig, ((ax1a, ax2a), (ax3a, ax4a)) = plt.subplots(2, 2)
ax1b = ax1a.twinx()
ax2b = ax2a.twinx()
ax3b = ax3a.twinx()
ax4b = ax4a.twinx()
ax1a.set_ylabel('ax1a')
ax2a.set_ylabel('ax2a')
ax3a.set_ylabel('ax3a')
ax4a.set_ylabel('ax4a')
ax1b.set_ylabel('ax1b')
ax2b.set_ylabel('ax2b')
ax3b.set_ylabel('ax3b')
ax4b.set_ylabel('ax4b')
plt.tight_layout()
plt.show()
Result: