MatPlotLib plot_surface:PDF中出现隐藏线 [英] MatPlotLib plot_surface: hidden lines appearing in PDF

查看:82
本文介绍了MatPlotLib plot_surface:PDF中出现隐藏线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MatPlotLib的plot_surface方法生成3D表面,然后将其保存到PDF文件中.我可以通过linewidth=0"隐藏表面线,但是在将savefig保存为PDF后这些线再次出现.

I'm generating a 3D surface using MatPlotLib's plot_surface method, then saving it to a PDF file. I am able to hide the surface lines via "linewidth=0", but the lines appear again after a savefig to PDF.

对.png和.svg执行savefig()时,隐藏的行保持隐藏状态.

When doing the savefig() to .png and .svg, the hidden lines stay hidden.

下面的第一张图片是plt.show()结果的屏幕截图,第二张是PDF结果的屏幕截图.我有什么想法可以使隐藏的线条在PDF中看不见?

The first image below is a screenshot of the plt.show() result, and the second is a screenshot of the PDF result. Any ideas of what I can do to keep the hidden lines out of sight in the PDF?

我会在底部发布代码,因为它有点长.Windows 7(64位),Python 2.7.3(win32),MatPlotLib 1.2.0(win32).

I'll post code at the bottom, since it's a bit long. Windows 7 (64-bit), Python 2.7.3 (win32), MatPlotLib 1.2.0 (win32).

计划变更,这个论坛不允许我发布图片,这是关于没有名望的事情:).所以只写代码.

Change of plan, this forum doesn't allow me to post images, something about not having a reputation :). So code only.

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

# 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上发布错误报告.

As suggested by esmit, the fact that the output appears to be correct in other image formats (both PNG and SVG) suggests there may be a bug in the PDF backend. I'll post a bug report at github.

感谢您的提示!

这篇关于MatPlotLib plot_surface:PDF中出现隐藏线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆