Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rotate a 3D surface in matplotlib

I have written code to plot a 3D surface of a parabaloid in matplotlib.

How would I rotate the figure so that the figure remains in place (i.e. no vertical or horizontal shifts) however it rotates around the line y = 0 and z = 0 through an angle of theta ( I have highlighted the line about which the figure should rotate in green). Here is an illustration to help visualize what I am describing: enter image description here

For example, If the figure were rotated about the line through an angle of 180 degrees then this would result in the figure being flipped 'upside down' so that the point at the origin would be now be the maximum point.

I would also like to rotate the axis so that the colormap is maintained. Here is the code for drawing the figure:

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

#creating grid
y = np.linspace(-1,1,1000)
x = np.linspace(-1,1,1000)
x,y = np.meshgrid(x,y)

#set z values
z = x**2+y**2

#label axes
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')


#plot figure
ax.plot_surface(x,y,z,linewidth=0, antialiased=False, shade = True, alpha = 0.5)

plt.show()
like image 294
fosho Avatar asked Jul 12 '16 11:07

fosho


People also ask

How to create a 3D surface plot in Matplotlib?

Creation of 3D Surface Plot. To create the 3-dimensional surface plot the ax.plot_surface () function is used in matplotlib. The required syntax for this function is given below: ax.plot_surface (X, Y, Z) In the above syntax, the X and Y mainly indicate a 2D array of points x and y while Z is used to indicate the 2D array of heights.

How to make a rotating 3D graph in Matplotlib?

To make a rotating 3D graph in matplotlib, we can use Animation class for calling a function repeatedly. Initialize variables for number of mesh grids, frequency per second to call a function, frame numbers. Create x, y, and z array for a curve.

What is mplot3d in Matplotlib?

Initially, matplotlib was used to plot and visualize only 2D graphs, but we should thank the mplot3d toolkit available in the matplotlib library. We can visualize 3D plots such as 3D scatter plots, 3D line plots, Surface plots, Rotated plot angles, etc.

What is the difference between 3D surface plot and gradient surface plot?

This plot is a combination of a 3D surface plot with a 2D contour plot. In the Gradient surface plot, the 3D surface is colored same as the 2D contour plot. The parts that are high on the surface contains different color rather than the parts which are low at the surface.


1 Answers

Something like this?

ax.view_init(-140, 30)

Insert it just before your plt.show() command.

like image 125
Aguy Avatar answered Sep 20 '22 13:09

Aguy