Skip to content

plot_surface: hidden lines re-appearing in PDF and SVG backends #2247

Closed

Description

@pf416facebook

I'm generating a 3D surface using plot_surface, intending to save it to a PDF file. I am able to hide the surface lines in the show() display via "linewidth=0", but the lines appear again in some backends.

The PNG backend leaves the lines hidden, SVG shows them as if linewidth=0 were ignored, and PDF shows them with a change of color. Is there a backend problem here?

Attached images are JPG screenshots (they're quite large otherwise), in order: show() output, PNG output, SVG output, PDF output.

I'm using Windows 7 (64-bit), Python 2.7.3 (win32), and MatPlotLib 1.2.0 (win32).

Code follows, and should be executable standalone if anyone wants to try replicating the problem.

#=====================================================================
#= 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 ("Diagram_Axes_1_raw.pdf")
plt.savefig ("Diagram_Axes_1_raw.svg")
plt.savefig ("Diagram_Axes_1_raw.png")
plt.show()

bug-report-show
bug-report-png
bug-report-svg
bug-report-pdf

Activity

  1. WeatherGod commented on Jul 23, 2013

    @WeatherGod
    Member

    Jeff, I could not run your example (after trying to correct the formatting
    issues). Could you edit your post to fix the example code formatting in
    case that is the issue that I am having?

  2. pf416facebook commented on Jul 23, 2013

    @pf416facebook
    Author

    Sorry about that; when I posted I wasn't able to see how to mark off a code block (I'm very new here). Just found it. I think it's okay now, I was able to copy and paste into IDLE and it ran.

  3. WeatherGod commented on Jul 23, 2013

    @WeatherGod
    Member

    No problem. In any case, I was able to reproduce your problem, even in the latest master.

    So, IIRC, the issue is that various pdf viewers will not understand a linewidth of zero and will enforce a minimum width no matter what. The workaround in your situation is to specify an edgecolor='none' instead of linewidth=0.0. Now, the crazy part is that I could have sworn that we added a check for this condition and handle this automatically in the PDF and SVG backends. @mdboom, thoughts?

  4. added this to the v1.4.x milestone on Aug 17, 2014
  5. modified the milestones: v1.4.x, on Aug 18, 2014
  6. jkseppan commented on Sep 8, 2014

    @jkseppan
    Member

    I think this is fixed by #3485. In fact, the pdf specification requires drawing a line of width zero as thin lines (with some implementation-specific width), so it is the responsibility of the pdf producer to omit stroking paths that should not have their outlines drawn. This is different from several other matplotlib backends, where a width of 0 means that the line is not drawn.

  7. jkseppan commented on Oct 19, 2014

    @jkseppan
    Member

    On current master I don't see the problem. The pdf fix did not make it to 1.4.1, but it has been cherry-picked for 1.4.2. I think the svg part has been fixed earlier.

  8. goerz commented on Nov 27, 2014

    @goerz

    I'm still seeing the problem with PDF in 1.4.2 (Enthought Canopy installation on MacOS)

  9. jenshnielsen commented on Nov 27, 2014

    @jenshnielsen
    Member

    Unfortunately the fix did not make it into 1.4.2 which was released just a few days after 1.4.1 to fix a single serious bug in 1.4.1. It should be in 1.4.3 when that is released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions