使用python子图的条形图和折线图的动画图

时间:2019-02-11 21:45:30

标签: python matplotlib animation

我能够使用以下链接的建议来绘制动画图。

matplotlib animation multiple datasets

我的代码在这里。

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()

我还为条形图设置了其他数据,并使用以下代码创建了动画条形图。

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()

我的最终目标是使用子图功能在一个图形中同时绘制动画条形图和折线图,以使条形图为(1,1),折线图位于图形的(2,1)位置。

有人可以帮助我在python的一个图形窗口中创建动画条形图和折线图吗? 更具体地说,如何在一个动画函数中同时组合线形图和条形图?

根据下面的评论,我修改了这样的代码。

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()

但是我收到了这样的错误消息。 “ AttributeError:“ BarContainer”对象没有属性“ set_animated””

您能帮我如何解决此错误?

0 个答案:

没有答案