【发布时间】:2020-11-16 06:51:25
【问题描述】:
我想绘制一个轴位于图中间的 3-D 曲面。
我使用以下代码绘制图形:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 10)
y = np.linspace(-1, 1, 10)
X, Y = np.meshgrid(x, y)
Z = np.array([[2.58677481, 3.22528864, 3.65334814, 3.86669336, 3.86399048,
3.64525411, 3.21186215, 2.56819809, 1.72989472, 0.78569291],
[2.58677481, 3.22528864, 3.65334814, 3.86669336, 3.86399048,
3.64525411, 3.21186215, 2.56819809, 1.72989472, 0.78569291],
[2.58677481, 3.22528864, 3.65334814, 3.86669336, 3.86399048,
3.64525411, 3.21186215, 2.56819809, 1.72989472, 0.78569291],
[2.58677481, 3.22528864, 3.65334814, 3.86669336, 3.86399048,
3.64525411, 3.21186215, 2.56819809, 1.72989472, 0.78569291],
[2.58677481, 3.22528864, 3.65334814, 3.86669336, 3.86399048,
3.64525411, 3.21186215, 2.56819809, 1.72989472, 0.78569291],
[2.58677481, 3.22528864, 3.65334814, 3.86669336, 3.86399048,
3.64525411, 3.21186215, 2.56819809, 1.72989472, 0.78569291],
[2.58677481, 3.22528864, 3.65334814, 3.86669336, 3.86399048,
3.64525411, 3.21186215, 2.56819809, 1.72989472, 0.78569291],
[2.58677481, 3.22528864, 3.65334814, 3.86669336, 3.86399048,
3.64525411, 3.21186215, 2.56819809, 1.72989472, 0.78569291],
[2.58677481, 3.22528864, 3.65334814, 3.86669336, 3.86399048,
3.64525411, 3.21186215, 2.56819809, 1.72989472, 0.78569291],
[2.58677481, 3.22528864, 3.65334814, 3.86669336, 3.86399048,
3.64525411, 3.21186215, 2.56819809, 1.72989472, 0.78569291]])
fig = plt.figure()
ax = plt.axes(projection='3d')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, linewidth=0,
cmap='viridis', edgecolor='none', antialiased=False)
ax.set_xlim(-1.01, 1.01)
fig.colorbar(surf, shrink=0.5, aspect=5)
# Plot the axis in the middle of the figure
xline=((min(X[:,0]),max(X[:,0])),(0,0),(0,0))
ax.plot(xline[0],xline[1],xline[2],'grey')
yline=((0,0),(min(Y[:,1]),max(Y[:,1])),(0,0))
ax.plot(yline[0],yline[1],yline[2],'grey')
zline=((0,0),(0,0),(min(Z[:,2]),max(Z[:,2])))
ax.plot(zline[0],zline[1],zline[2],'grey')
ax.view_init(30,220) # Camera angel
ax.set_title('surface');
通过上面的代码,我得到这样的图
我真正想要的是绘制一个 3-D 轴原点图,如下所示:
如何消除边距,把轴放在图的中间?
【问题讨论】:
-
这似乎是 Python 3d plot - axis centered 的副本。不过,那里没有赞成的答案。
-
这是一个更好的问题,因为这里有示例数据和起点。我宁愿将另一个关闭为重复项(但不确定是否重复项必须始终是最新的)。虽然,这是表面图和另一个散点图的意义上有所不同。
-
“如何消除边距”是什么意思?删除彩条?还是别的什么?
标签: python python-3.x matplotlib