我编写的代码用于在matplotlib中绘制一个抛物面的3D表面。
如何旋转图形以使图形保持在原位(即没有垂直或水平位移),但是它绕着线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()
答案 0 :(得分:4)
这样的东西?
ax.view_init(-140, 30)
在plt.show()
命令之前插入它。
答案 1 :(得分:3)
根据我的评论:
import mayavi.mlab as mlab
import numpy as np
x,y = np.mgrid[-1:1:0.001, -1:1:0.001]
z = x**2+y**2
s = mlab.mesh(x, y, z)
alpha = 30 # degrees
mlab.view(azimuth=0, elevation=90, roll=-90+alpha)
mlab.show()
或关注@Tamas回答:
#parabaloid
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from math import sin, cos, pi
import matplotlib.cm as cm
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#creating grid
y = np.linspace(-1,1,200)
x = np.linspace(-1,1,200)
x,y = np.meshgrid(x,y)
#set z values
z0 = x**2+y**2
# rotate the samples by pi / 4 radians around y
a = pi / 4
t = np.transpose(np.array([x,y,z0]), (1,2,0))
m = [[cos(a), 0, sin(a)],[0,1,0],[-sin(a), 0, cos(a)]]
x,y,z = np.transpose(np.dot(t, m), (2,0,1))
# or `np.dot(t, m)` instead `t @ m`
#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, facecolors=cm.viridis(z0))
plt.show()
答案 2 :(得分:2)
我能想到的最好的就是旋转数据本身。
#parabaloid
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from math import sin, cos, pi
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#creating grid
y = np.linspace(-1,1,200)
x = np.linspace(-1,1,200)
x,y = np.meshgrid(x,y)
#set z values
z = x**2+y**2
# rotate the samples by pi / 4 radians around y
a = pi / 4
t = np.transpose(np.array([x,y,z]), (1,2,0))
m = [[cos(a), 0, sin(a)],[0,1,0],[-sin(a), 0, cos(a)]]
x,y,z = np.transpose(t @ m, (2,0,1))
# or `np.dot(t, m)` instead `t @ m`
#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()
答案 3 :(得分:1)
我似乎还不能添加评论,但我想对 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));
答案 4 :(得分:0)
这是最好的解决方案: -首先,您必须在Spyder环境中执行python脚本,可通过下载Anaconda轻松获得该脚本。在Spyder中执行脚本后,您所需要做的就是遵循以下说明:
理论上,一旦执行了前面的步骤,如果您运行脚本,则创建的图形将出现在不同的窗口中,您可以通过缩放和平移与它们进行交互。如果是3d绘图(3d曲面),您将能够绕其旋转。