Matplotlib 3d 曲面图显示轴限制之外的值
在 matplotlib 3d 绘图 plot_surface
中,即使 x、y 和 z 轴限制设置为 >=0,表面的负 z 部分仍在绘制.二维绘图不会发生同样的情况——如果在为绘图提供的 20 个数据点中,有 10 个位于轴限制之外,例如第一象限,它们根本不会显示在绘图中.
In matplotlib 3d-plotting plot_surface
even when the x, y and z axis limits are set to be >=0 the negative z-portion of the surface is still getting plotted. The same does not happen for 2d plots- if out of 20 data points provided as input for plotting, 10 fall outside the axis limits, say first-quadrant, they simply do not get displayed in the plot.
请看下面的代码和相应的情节作为证据(代码在 Jupyter notebook 中运行)-
Please see the below code and corresponding plot as an evidence (code run in Jupyter notebook) -
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
%matplotlib notebook
x = np.linspace(0,1.5,100)
y = np.linspace(0,1.5,100)
X,Y = np.meshgrid(x,y)
Z = 1-X-Y
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
surf = ax.plot_surface(X,Y,Z,cmap=cm.coolwarm)
ax.set_xlim(0,1.5)
ax.set_ylim(0,1.5)
ax.set_zlim(0,3)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
fig.colorbar(surf, shrink=0.5, aspect=5)
fig.savefig('Question10Fig')
蓝色以及旁边的颜色条的存在表明图中存在 z
The existence of the blue colour along with the colour-bar alongside makes it evident that z<0 values are present in the plot. Why is that so?
那似乎起源于
ax.set_zlim(0,3)
去掉这条线,我觉得这个图很好看.这似乎是 matplotlib 中的一个问题(另请参见 这篇文章).
Removing this line, the figure looks good to me. That seems to be a problem in matplotlib (see also this post).