# Save 3d plot as vector format

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 close merge delete

( 2021-03-22 22:39:02 +0200 )edit

Sort by » oldest newest most voted

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

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.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')

more

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

( 2012-06-21 00:40:32 +0200 )edit

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 ?

more

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.

more

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).

more