首先用python的matplotlib库画一个图像。
代码如下:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
theta = np.linspace(-4 * np.pi, 4* np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z ** 2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y , z)
plt.show()
如果要拉伸z轴,则可以添加如下代码改变z轴与其他轴的尺寸比例
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([0.5, 0.5, 1, 1]))
整合代码如下:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
theta = np.linspace(-4 * np.pi, 4* np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z ** 2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([0.5, 0.5, 1, 1]))
ax.plot(x, y , z)
plt.show()
如果要改变其他轴或者拉伸/缩短不同的尺寸,只需要改变对应np.diag里的数组大小即可。
参考:https://stackoverflow.com/questions/30223161/matplotlib-mplot3d-how-to-increase-the-size-of-an-axis-stretch-in-a-3d-plo?answertab=oldest#tab-top