我已经编写了代码来绘制matplotlib中的一个准parabaloid的三维曲面。
我如何旋转图形,使图形保持不变(即没有垂直或水平移动),然而,它通过θ的角度围绕直线y=0和z=0旋转(我突出显示了图形应该以绿色旋转的线)。这里有一个例子来帮助我想象我所描述的:

例如,如果图形是通过180度的角度绕直线旋转,那么这将导致图形被“倒转”,因此原点现在是最大点。
我也想旋转轴,以便保持彩色地图。下面是绘制该图形的代码:
#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()发布于 2021-03-06 09:20:29
我似乎还不能补充评论,但我想对Tamas的实现做一个修改。有一个问题,表面不是逆时针旋转到轴(在本例中是y轴),其中y轴是从页面中出来的。相反,它是顺时针旋转的。
为了纠正这一点,并使之更加简单,我构造了x,y和z网格,并将它们重新组合成简单的列表,我们在其中执行旋转。然后,我将它们重组为网格,以便使用plot_surface()函数:
import numpy as np
from matplotlib import pyplot as plt
from math import sin, cos, pi
import matplotlib.cm as cm
num_steps = 50
# Creating grid
y = np.linspace(-1,1,num_steps)
x = np.linspace(-1,1,num_steps)
x,y = np.meshgrid(x,y)
# Set z values
z = x**2+y**2
# Work with lists
x = x.reshape((-1))
y = y.reshape((-1))
z = z.reshape((-1))
# Rotate the samples by pi / 4 radians around y
a = pi / 4
t = np.array([x, y, z])
m = [[cos(a), 0, sin(a)],[0,1,0],[-sin(a), 0, cos(a)]]
x, y, z = np.dot(m, t)
ax = plt.axes(projection='3d')
# Label axes
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
# Plot the surface view it with y-axis coming out of the page.
ax.view_init(30, 90)
# Plot the surface.
ax.plot_surface(x.reshape(num_steps,num_steps), y.reshape(num_steps,num_steps), z.reshape(num_steps,num_steps));https://stackoverflow.com/questions/38326983
复制相似问题