1

I am trying to animate a 3D bar graph with blit=True. I am able to do animation with blit=False but it takes longer (30 msec for a graph of 60 bars) hence I am trying with blit=True. But the animation doesn't happen, no error as well.

I couldn't find any documentation of animating 3D bar graph though there are details available about animation of 3D line graphs.

My minimal code is given below.

#!/usr/bin/env python3
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random

fig = plt.figure(figsize=(5, 5)) 
plt.subplots_adjust(top=0.99, bottom=0.01, left=0.01, right=0.99)
ax1 = fig.add_subplot(111, projection='3d')

x = 0
y = 0
z = random.randint(0, 10)                 

main = ax1.bar3d(1, 1, 0, 1, 1, z, alpha=1, shade=True)

def init():
    return main,

def animate(i):
   z = random.randint(0, 10)
   print ("z is ", z)
   return main,

ani = FuncAnimation(fig, animate, interval=500, blit=True, init_func=init, frames=200)
plt.show()

What I am expecting with this code is a height changing bar at every 500 msec.

AcK
  • 2,063
  • 2
  • 20
  • 27
TMS
  • 11
  • 2
  • Drawing Poly3DCollections with `blit=True` often [turns out to be difficult](https://stackoverflow.com/questions/66113220/how-to-animate-poly3dcollection-using-funcanimation-with-blit-true/66236583#66236583) and highly dependent on the backend. Is blitting and just modifying just the z-value (which does not work as you imagine it would) really necessary in your case? Redrawing the entire bar is sufficiently fast. – Mr. T Feb 27 '21 at 11:58
  • Thanks Mr.T . Clearing & redrawing works but it takes around 30 msec for drawing 60 bars which increase further if I change the canvas size. I have a requirement to draw it within 15 msec hence I was exploring blit=True option. – TMS Feb 27 '21 at 18:23
  • [Well](https://stackoverflow.com/a/45713451/8881141), good luck implementing this. – Mr. T Feb 27 '21 at 18:42
  • 1
    I could find a solution by using main.set_verts(vertices) and main.do_3d_projection(). Thanks to [this discussion] (https://stackoverflow.com/questions/66113220/how-to-animate-poly3dcollection-using-funcanimation-with-blit-true) . I have to calculate vertices for all polygons and pass , still it is worth as I see refresh happening 2 times faster than clearing & redrawing. – TMS Mar 03 '21 at 16:18

0 Answers0