0

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 ?

5
  • Using fig, (ax1, ax2) = plt.subplots(nrows=2) gives you two subplots. Plot the lines to ax1 and the bar ax2. Commented Feb 11, 2019 at 22:04
  • Thank you. Perhaps, my question might not accurately describe what I would like to do. And so I edited my question. You can draw subplots as you suggested. Real question is how to combine both line and bar graphs in one animate function ? This is really tricky for me to figure out. Commented Feb 12, 2019 at 1:38
  • 1
    The above comment is the prerequisite. The rest follows from it. Since you will then only have a single figure, you will only need a single animation. The animating function should contain the code to update the line plot as well as to redraw the bar plot. You will want to use ax2.clear() and ax2.bar instead of what you have now and don't use pyplot commands. Commented Feb 12, 2019 at 1:44
  • I cannot run your code (because it is no minimal reproducible example!!), but I'd suggest to try 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 turn blit=False. In that case you can even remove the return line completely. Commented Feb 12, 2019 at 16:30
  • Thank you. I corrected the code as you suggested.But I use this inline 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. Commented Feb 12, 2019 at 18:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.