rook1996
rook1996

Reputation: 247

Cant plot an Python matplotlib figure in pdf

I know that my question sounds desperate and also it is not a short reproducible example. However I am writing my thesis and I tried a test print. I couldnt print the document, written in TeX and viewed in pdf, because of one plot that is based on python (all other ones are created in R). I would be so gratefull to someone how could have a quick look at it. I'm really desperate right now because I don't want to take this graphic out of my work. I am not really familiar with python as I am using R, but just wanted to generate this plot for my thesis.

the Code.

I am referring to the figure in the section "Lasso coordinate descent Vary parameter l (lamda) for different results" (field [20]).

I am trying to save it as follows:

    %matplotlib inline
fig = plt.figure(figsize = (16,8))

#Surface plot
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.plot_surface(T1, T2, Z, rstride = 5, cstride = 5, cmap = 'jet', alpha=0.5)
ax.plot(theta_0,theta_1,J_history_lasso, marker = '*', color = 'r', alpha = .4, label = 'Gradient descent')

ax.set_xlabel('theta 1')
ax.set_ylabel('theta 2')
ax.set_zlabel('error')
ax.set_title('RSS gradient descent: Root at {}'.format(theta_result_lasso.ravel()))
ax.view_init(25, -40)


#Contour plot
ax = fig.add_subplot(1, 2, 2)
ax.contour(T1, T2, Z, 100, cmap = 'jet')
ax.quiver(theta_0[:-1], theta_1[:-1], anglesx, anglesy, scale_units = 'xy', angles = 'xy', scale = 1, color = 'r', alpha = .9)



 ax.set_xlabel(r'$\beta_1$', fontsize=20, labelpad = 10)
 ax.set_ylabel(r'$\beta_2$', fontsize=20, labelpad = 10)
 ax.set_zlabel(r'$\mathcal{L}(\beta)$', fontsize=20, labelpad = 10)
 ax.tick_params(labelsize=16)
 ax.set_title(r'Globales Minimum $\hat \beta$ = [{:0.0f}, {:0.2f}]'.format(roundi[0],roundi[1]), fontsize=20, pad=40)

 plt.show()

 fig.savefig("l18.pdf", bbox_inches='tight')

but I cant print out the regarding pdf. I also tried different types of printers. So it should be problem with the regarding figure.

Upvotes: 0

Views: 144

Answers (2)

Sheldore
Sheldore

Reputation: 39052

Try saving it as .eps and use eps2pdf convertor to convert to pdf. Or just use .eps files in your LaTeX thesis. eps are vector plots and have really good resolutions.

Upvotes: 1

DYZ
DYZ

Reputation: 57033

plt.show() shows the picture and clears the canvas. Your PDF file is empty. That's why you cannot print it. Either remove plt.show() or move it after the savefig.

Upvotes: 0

Related Questions