Question

I'm plotting a group of 3D points using matplotlib.pyplot and numpy. Rotating the image using the mouse produces the azimuthal and elevation angles at the bottom of the figure window.

How can I get at these values programatically so that I can use them in subsequent calculations, for example when the mouse is released or I click a button on the screen.

I'm using python 2.7.5. My code below should illustrate the problem.

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

mol=np.random.rand(6,3)*10.0

fig3d = plt.figure(figsize = ( 6.5, 6.5 ) )

fig3d.canvas.set_window_title('3D')

ax3d = fig3d.gca(  projection='3d')

ax3d.scatter( mol[:,0], mol[:,1], mol[:,2], s=200)  

plt.show()
Was it helpful?

Solution

Below is some code which uses a callback to print out the current azimuthal and elevation angles, as well as append them to a list for further use later. These details are kept in ax3d.azim and ax3d.elev respectively.

Everytime you release your mouse button it will call on_click. This will allow you to simply rotate your graph to your chosen rotation and then save the projection.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

mol=np.random.rand(6,3)*10.0

fig3d = plt.figure(figsize = ( 6.5, 6.5 ) )

fig3d.canvas.set_window_title('3D')

ax3d = fig3d.gca(projection='3d')
ax3d.scatter(mol[:,0], mol[:,1], mol[:,2], s=200)  

# List to save your projections to
projections = []

# This is called everytime you release the mouse button
def on_click(event):
    azim, elev = ax3d.azim, ax3d.elev
    projections.append((azim, elev))
    print(azim, elev)

cid = fig3d.canvas.mpl_connect('button_release_event', on_click)

plt.show()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top