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:
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()
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.
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.
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.
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.
Something like this?
ax.view_init(-140, 30)
Insert it just before your plt.show()
command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With