How to set the "camera position" for three-dimensional graphs using python / matplotlib?

I am learning how to use mplot3d to create good 3d data graphics, and so far I am satisfied. What I'm trying to do at the moment is a small animation of a rotating surface. To do this, I need to set the camera position for 3D projection. I suggest that this should be possible, as the surface can rotate with the mouse when using matplotlib in interactive mode. But how can I do this with a script? I found many conversions in mpl_toolkits.mplot3d.proj3d, but I could not find out how to use them for my purpose, and I did not find any example for what I'm trying to do.

+113
python matplotlib mplot3d
Oct. 15 '12 at 10:25
source share
2 answers

The "camera position" sounds as if you want to adjust the height and azimuth angle that you use to view a three-dimensional graph. You can set this with ax.view_init . I used the script below to first create a plot, then I determined a good height, or elev , from which to view my plot. Then I adjusted the azimuth or azim angle to change the full 360 degrees around my graph, keeping the shape in each case (and noting which azimuth angle while keeping the graph). For more complex camera panning, you can adjust the height and angle to achieve the desired effect.

  from mpl_toolkits.mplot3d import Axes3D ax = Axes3D(fig) ax.scatter(xx,yy,zz, marker='o', s=20, c="goldenrod", alpha=0.6) for ii in xrange(0,360,1): ax.view_init(elev=10., azim=ii) savefig("movie%d.png" % ii) 
+131
Oct. 15 '12 at 23:31
source share

What would be convenient to apply the position of the camera to a new plot. Therefore, I draw and then move the plot with the mouse, changing the distance. Then try to reproduce the view, including the distance in another area. I find that axx.ax.get_axes () returns me an object with old .azim and .elev.

IN PYTHON ...

 axx=ax1.get_axes() azm=axx.azim ele=axx.elev dst=axx.dist # ALWAYS GIVES 10 #dst=ax1.axes.dist # ALWAYS GIVES 10 #dst=ax1.dist # ALWAYS GIVES 10 

Late 3D chart ...

 ax2.view_init(elev=ele, azim=azm) #Works! ax2.dist=dst # works but always 10 from axx 

EDIT 1 ... OK, the camera position is the wrong way of thinking about the value of .dist. It surpasses everything as a kind of scanning hacks for the entire graph.

This works to enlarge / scale the view:

 xlm=ax1.get_xlim3d() #These are two tupples ylm=ax1.get_ylim3d() #we use them in the next zlm=ax1.get_zlim3d() #graph to reproduce the magnification from mousing axx=ax1.get_axes() azm=axx.azim ele=axx.elev 

Late schedule ...

 ax2.view_init(elev=ele, azim=azm) #Reproduce view ax2.set_xlim3d(xlm[0],xlm[1]) #Reproduce magnification ax2.set_ylim3d(ylm[0],ylm[1]) #... ax2.set_zlim3d(zlm[0],zlm[1]) #... 
+11
Sep 15 '15 at 23:25
source share



All Articles