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 ?
fig, (ax1, ax2) = plt.subplots(nrows=2)gives you two subplots. Plot the lines toax1and the barax2.ax2.clear()andax2.barinstead of what you have now and don't usepyplotcommands.return rects + [line1, line2]. However, more generally, since you clear the axes anyways, it would not make too much sense to use blitting. I.e. you may turnblit=False. In that case you can even remove thereturnline completely.return rects + (line1,line2)and it works !! If I use list symbol, it generates error "TypeError: can only concatenate tuple (not "list") to tuple". Thank you.