python - MatPlotLib plot_surface : hidden lines appearing in PDF

标签 python matplotlib save

我正在使用 MatPlotLib 的 plot_surface 方法生成 3D 表面,然后将其保存为 PDF 文件。我可以通过“linewidth=0”隐藏表面线条,但在保存为 PDF 后线条再次出现。

编辑:当对 .png 和 .svg 执行 savefig() 时,隐藏线保持隐藏状态。

下图第一张是plt.show()结果截图,第二张是PDF结果截图。关于如何使 PDF 中的隐藏线不可见,我有什么想法吗?

我会把代码贴在底部,因为它有点长。 Windows 7(64 位)、Python 2.7.3 (win32)、MatPlotLib 1.2.0 (win32)。

改变计划,这个论坛不允许我发图片,一些关于没有声誉的事情:)。所以只写代码。

#=====================================================================

# get external packages
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#=====================================================================

Gw1 = plt.figure("Sphere-Cylinder Intersection")
diagram1 = Axes3D(Gw1)
diagram1.view_init(33,-48)

# force equal aspect ratio in all 3 directions
# 3D library is missing this -- force it with an invisible bounding cube
diagram1.set_aspect('equal')
CUBE = 1.3
for direction in (-1, 1):
    for point in np.diag(direction * CUBE * np.array([1,1,1])):
        diagram1.plot([point[0]], [point[1]], [point[2]], 'white')

# line for y-axis (show as N/E/D coords, not plotting coords)
diagram1.plot([0.0,1.5],[0.0,0.0],[0.0,0.0],
              linewidth=1,linestyle='-',color='black')
diagram1.text(1.6,0.0,0.0,'y',fontsize=14,fontweight='bold')

# line for x-axis (show as N/E/D coords, not plotting coords)
diagram1.plot([0.0,0.0],[0.0,1.5],[0.0,0.0],
              linewidth=1,linestyle='-',color='black')
diagram1.text(0.0,1.6,0.0,'x',fontsize=14,fontweight='bold')

# line for z-axis (show as N/E/D coords, not plotting coords)
diagram1.plot([0.0,0.0],[0.0,0.0],[0.0,-1.5],
              linewidth=1,linestyle='-',color='black')
diagram1.text(0.0,0.0,-1.7,'z',fontsize=14,fontweight='bold')

# unit sphere about origin
phi = np.linspace(0.0,np.pi/2.0,361)
theta = np.linspace(0.0,np.pi,361)
phi,theta = np.meshgrid(phi,theta)
x = np.sin(theta)*np.cos(phi)
y = np.sin(theta)*np.sin(phi)
z = np.cos(theta)
diagram1.plot_surface(x,y,z,linewidth=0.0,color='DarkKhaki',alpha=0.25)

# elliptical cylinder about z-axis 1
a = 0.10
b = 0.15
x = a*np.cos(np.linspace(0.0,np.pi/2.0,101))
z = np.linspace(-1.3,1.3,101)
x,z = np.meshgrid(x,z)
y = b*np.sin(np.arccos(x/a))
diagram1.plot_surface(x,y,z,linewidth=0.0,color='red',alpha=0.25)

# elliptical cylinder about z-axis 2
a = 0.21
b = 0.315
x = a*np.cos(np.linspace(0.0,np.pi/2.0,101))
z = np.linspace(-1.3,1.3,101)
x,z = np.meshgrid(x,z)
y = b*np.sin(np.arccos(x/a))
diagram1.plot_surface(x,y,z,linewidth=0.0,color='red',alpha=0.25)

# elliptical cylinder about z-axis 3
a = 0.42
b = 0.63
x = a*np.cos(np.linspace(0.0,np.pi/2.0,101))
z = np.linspace(-1.3,1.3,101)
x,z = np.meshgrid(x,z)
y = b*np.sin(np.arccos(x/a))
diagram1.plot_surface(x,y,z,linewidth=0.0,color='red',alpha=0.25)

# sphere-cylinder intersection 1
a = 0.10
b = 0.15
x = a*np.cos(np.linspace(0.0,np.pi/2.0,101))
y = b*np.sin(np.linspace(0.0,np.pi/2.0,101))
z = np.sqrt(np.around(1.0-x**2-y**2,decimals=10))
diagram1.plot(x,y,z,linewidth=1.0,linestyle='-',color='red')
diagram1.plot(x,y,-z,linewidth=1.0,linestyle='-',color='red')

# sphere-cylinder intersection 2
a = 0.21
b = 0.315
x = a*np.cos(np.linspace(0.0,np.pi/2.0,101))
y = b*np.sin(np.linspace(0.0,np.pi/2.0,101))
z = np.sqrt(np.around(1.0-x**2-y**2,decimals=10))
diagram1.plot(x,y,z,linewidth=1.0,linestyle='-',color='red')
diagram1.plot(x,y,-z,linewidth=1.0,linestyle='-',color='red')

# sphere-cylinder intersection 3
a = 0.42
b = 0.63
x = a*np.cos(np.linspace(0.0,np.pi/2.0,101))
y = b*np.sin(np.linspace(0.0,np.pi/2.0,101))
z = np.sqrt(np.around(1.0-x**2-y**2,decimals=10))
diagram1.plot(x,y,z,linewidth=1.0,linestyle='-',color='red')
diagram1.plot(x,y,-z,linewidth=1.0,linestyle='-',color='red')

# plotting axes off
diagram1.axis('off')

# display/save
plt.savefig ("Diagram1.pdf")
plt.show()

#=====================================================================

最佳答案

正如 esmit 所建议的,输出在其他图像格式(PNG 和 SVG)中似乎是正确的这一事实表明 PDF 后端可能存在错误。我将在 github 上发布错误报告。

感谢您的提示!

关于python - MatPlotLib plot_surface : hidden lines appearing in PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17766481/

相关文章:

python - 如何在已经训练好的 xgboost 模型上使用 CalibrateClassifierCV?

Python/matplotlib 在直方图中显示置信度

vba - Excel 在尝试保存时无法使用可用资源完成此任务

java - 尝试保存图像时出错

java - Android文字游戏的保存技巧

python - 递归闭包中的作用域错误

javascript - redis.exceptions.ResponseError : MOVED error in redis set operation 错误

python - Python/Django 中的第三方 API 集成

python - "#using-proxy-artist".format(orig_handle) 带饼图(来自 CSV 的数据) Matplotlib

python - 条形图中的重叠名称