Ask Your Question
1

Save 3d plot as vector format

asked 2012-06-10 08:19:24 -0500

daniel.e2718 gravatar image

Is there a way to export a 3d plot as a pdf file?

I know you can't do p.save('filename.extension') because 3dplot doesn't create a graphics item. But is there a way to do this?

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted
6

answered 2012-06-11 03:29:48 -0500

Volker Braun gravatar image

The Sage 3D plots don't support output in a 2D vector format (like PDF), at least not for now. You can do it by calling matplotlib directly: PDF version of the image below

image description

from mpl_toolkits.mplot3d import axes3d
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rc
rc('text', usetex=True)

rays = [(-3, -2, 4), (0, 1, 0), (1, 0, 0), (2, 1, -4)]
extrapoints = [(-1, -1, 2), (1, 1, -2)]
cones = [[0,1,2],[0,1,3],[0,2,3],[1,2,3]]

poly3d = []
for cone in cones:
    verts = [ rays[i] for i in cone ]
    poly3d.append(verts)

polygons = Poly3DCollection(poly3d, facecolor='green', 
                            linewidths=2,
                            alpha=0.1)
polygons.set_alpha(0.3)

fig = plt.figure()
fig.set_figwidth(13.0/2.54)
fig.set_figheight(9.0/2.54)

ax = axes3d.Axes3D(fig)
ax.scatter3D(*zip(*rays), s=50, c='red')
ax.scatter3D(*zip(*extrapoints), s=50, c='blue')
ax.scatter3D([0],[0],[0], s=20, c='black')

ax.add_collection3d(polygons)  
ax.set_xlabel(r'$X$')
ax.set_ylabel(r'$Y$')
ax.set_zlabel(r'$Z$')
ax.set_xlim3d(-3, 2)
ax.set_ylim3d(-2, 1)
ax.set_zlim3d(-4, 4)

r = rays[0]
ax.text(r[0]-0.2,r[1],r[2], r'${}^{'+str(r)+'}$', 
        horizontalalignment='right',
        verticalalignment='top',
        color='red')

r = rays[3]
ax.text(r[0]-0.2,r[1],r[2], r'${}^{'+str(r)+'}$', 
        horizontalalignment='right',
        verticalalignment='top',
        color='red')

r = rays[1]
ax.text(r[0],r[1],r[2]+0.3, r'${}^{'+str(r)+'}$', 
        horizontalalignment='center',
        verticalalignment='bottom',
        color='red')

r = rays[2]
ax.text(r[0],r[1],r[2]-0.3, r'${}^{'+str(r)+'}$', 
        horizontalalignment='center',
        verticalalignment='top',
        color='red')

r = extrapoints[0]
ax.text(r[0],r[1],r[2]-0.3, r'${}^{'+str(r)+'}$', 
        horizontalalignment='center',
        verticalalignment='top',
        color='blue')

r = extrapoints[1]
ax.text(r[0]+0.2,r[1],r[2], r'${}^{'+str(r)+'}$', 
        horizontalalignment='left',
        verticalalignment='bottom',
        color='blue')


ax.text(-0.2,0,0, r'${}^0$', 
        horizontalalignment='right',
        verticalalignment='top',
        color='black')

# plt.show()

for axis in [ax.w_xaxis, ax.w_yaxis, ax.w_zaxis]:
    axis.get_major_locator()._integer = True
    for t in axis.get_ticklabels(): 
        t.set_fontsize(6)

plt.savefig('fig_NablaBsing.pdf', bbox_inches='tight')
edit flag offensive delete link more

Comments

That response made me think curse words. Seems like a lot of work for one picture.

daniel.e2718 gravatar imagedaniel.e2718 ( 2012-06-20 17:40:32 -0500 )edit
1

answered 2012-06-10 16:01:13 -0500

calc314 gravatar image

I don't think the 3d plots support output to a PDF. I have gotten a TIFF, however, and then converted it to a PDF.

You can also use the viewer='tachyon' option and get a TIFF (but not a PDF).

edit flag offensive delete link more
0

answered 2018-02-03 07:16:47 -0500

al gravatar image

updated 2018-02-03 07:19:06 -0500

I had the same question. The screen capture suggested by calc314 worked fine, thank you! But I wonder how much quality is lost, and whether a more direct output to pdf wouldn't be better. I tried Tachyon but the result was much worse- axes had been shifted, a transparent surface had disappeared....!!!

In fact, when people illustrate textbooks, do they sometimes use Sagemath, or do they always switch to Mathematica or Maple or something partly because of this issue ?

edit flag offensive delete link more
0

answered 2012-06-11 01:20:54 -0500

calc314 gravatar image

updated 2012-06-11 01:21:51 -0500

Another option that might work is a screen capture to a PDF, although it's not an elegant solution. This has the advantage of letting you turn the 3D graphic to the viewpoint that you'd like and then getting the image. To do that otherwise requires some dancing using Tachyon commands, as I understand it.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-06-10 08:19:24 -0500

Seen: 1,755 times

Last updated: Feb 03