我试图从一组指定z值的数据中绘制一个3D曲面。我得到了一些奇怪的透明艺术品,在那里我可以透过表面看到,即使我设置alpha=1.0。
打印时和保存到文件时(均为png和pdf格式)都会显示该工艺品:
我试过改变线条宽度,把步幅从1改为10(在后一种情况下,由于分辨率太粗糙,表面不可见)。
问:我怎样才能摆脱这种透明性?
这是我的代码:
import sys
import numpy as np
import numpy.ma as ma
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
y_label = r'x'
x_label = r'y'
z_label = r'z'
x_scale = 2.0*np.pi
y_scale = 2.0*np.pi
y_numPoints = 250
x_numPoints = 250
def quasiCrystal(x, y):
z = 0
for i in range(0,5):
z += np.sin(x * np.cos(float(i)*np.pi/5.0) +
y * np.sin(float(i)*np.pi/5.0))
return z
x = np.linspace(-x_scale, x_scale, x_numPoints)
y = np.linspace(-y_scale, y_scale, y_numPoints)
X,Y = np.meshgrid(x,y)
Z = quasiCrystal(X, Y)
f = plt.figure()
ax = f.gca(projection='3d')
surf = ax.plot_surface( X, Y, Z,
rstride=5, cstride=5,
cmap='seismic',
alpha=1,
linewidth=0,
antialiased=True,
vmin=np.min(Z),
vmax=np.max(Z)
)
ax.set_zlim3d(np.min(Z), np.max(Z))
f.colorbar(surf, label=z_label)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
ax.set_zlabel(z_label)
plt.show()
Matplotlib不是一个真正的3D引擎。这是一个众所周知的问题,偶尔会出现一个与您类似的问题(请参见this和this)。问题是同一件艺术品可能引发看起来不同的问题。我相信你就是这样。
在继续我的建议之前,让我先quote this information from the maplotlib website:
It seems that Mayavi has finally moved on to Python 3,这当然是一种可能性。如果要坚持使用matplotlib进行此类绘图,我的建议是使用rstride和cstride值来查看哪些值可以生成令您满意的绘图。
另一种可能是尝试看看其他类型的3D绘图是否更好。检查plot_trisurf、contour或contourf。我知道这不太理想,但在过去我也设法做到了。
很抱歉没有更满意的答复。或许其他的用户对此有更好的解决方案。祝你好运。
我遇到了一些类似的问题,发现它们是抗锯齿工件,可以通过在
plot_surface
中设置antialiased=False
来修复。相关问题 更多 >
编程相关推荐