0

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.

rook1996
  • 237
  • 3
  • 9
  • It is an exception that it contradicts the usual format of this Forum. – rook1996 Aug 23 '18 at 19:36
  • 1
    just comment the line `plt.show()` – abc Aug 23 '18 at 19:43
  • I'll try give me a second – rook1996 Aug 23 '18 at 19:43
  • Have you tried saving the figure as a png, and then linking it to the tex document? – Nick Tallant Aug 23 '18 at 19:45
  • I'll try both Solutions, thanks to you guys – rook1996 Aug 23 '18 at 19:45
  • `plt.show()` shows your figure. My guess is that you are closing it pressing on the `x` button and so stopping the execution. If you comment it `#plt.show()` you will reach the `savefig` command. See e.g., https://stackoverflow.com/questions/21875356/saving-a-figure-after-invoking-pyplot-show-results-in-an-empty-file – abc Aug 23 '18 at 19:46
  • I also saved it as pdf. The pdf file is produced, but I cant print the regarding pdf file. Are there maybe some other formats to save it ? – rook1996 Aug 23 '18 at 19:48
  • It works as png but the resolution isnt quite well. Maybe the dpi of the pdf figure is too high or something – rook1996 Aug 23 '18 at 19:58
  • 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 – Sheldore Aug 23 '18 at 20:00
  • I'll try, you think that this could work ? – rook1996 Aug 23 '18 at 20:05
  • 1
    Of course. I have already tried this. In your latex compiler, you will have to choose the option of ps--> pdf during compilation. Just google for more on your TeX editor...whichever you are using – Sheldore Aug 23 '18 at 20:08
  • The eps version works ! I can print the thesis at my home printer. So it will also be printed by a professional one. You saved my night :D Do you want to write an answer ? – rook1996 Aug 23 '18 at 20:37

2 Answers2

1

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.

Sheldore
  • 37,862
  • 7
  • 57
  • 71
0

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.

DYZ
  • 55,249
  • 10
  • 64
  • 93