You are not logged in.

#1 2014-11-25 03:20:10

godott
Member
Registered: 2013-11-04
Posts: 12

Python Pylot savefig()/show() can't generate quality figure in pdf.

Recently I am plotting some figures using pyplot. If I save the figure in png format, it has great quality, but not in pdf.
Tried setting the dpi and figure size but doesn't work. Seesm on Ubuntu and Windows I have not this problem. Anybody know why? Thanks. ^_^.

Offline

#2 2014-11-26 00:38:58

godott
Member
Registered: 2013-11-04
Posts: 12

Re: Python Pylot savefig()/show() can't generate quality figure in pdf.

update.

Last edited by godott (2014-11-29 21:34:36)

Offline

#3 2014-11-29 21:34:09

godott
Member
Registered: 2013-11-04
Posts: 12

Re: Python Pylot savefig()/show() can't generate quality figure in pdf.

can anybody give a solution? Thanks very much!

Offline

#4 2014-11-30 01:42:35

Sanne
Member
Registered: 2012-03-13
Posts: 87

Re: Python Pylot savefig()/show() can't generate quality figure in pdf.

I guess your description is much too vague for people to be able to help you. Can you be more specific? What exactly did you do, what did you expect? In what way is the pdf quality not sufficient? What exactly doesn't work when setting dpi and figure size? What is different in Ubuntu and Windows?

Can you provide a minimal test case that shows your problem (python code, screenshots etc)? I could run such a script, I have matplotlib installed, then we can compare. I'm no master, though, just played around a bit so far. smile

Right now I can only say: works for me. When I set my script output to pdf, I get a nice vectorized plot inside with, for me, good quality.

Last edited by Sanne (2014-11-30 01:48:29)


If our currency were not money but appreciation and acknowledgement for what we do for others, for the community, for the benefit of all, we would have paradise on earth.

Offline

#5 2014-12-01 06:21:01

godott
Member
Registered: 2013-11-04
Posts: 12

Re: Python Pylot savefig()/show() can't generate quality figure in pdf.

Sanne wrote:

I guess your description is much too vague for people to be able to help you. Can you be more specific? What exactly did you do, what did you expect? In what way is the pdf quality not sufficient? What exactly doesn't work when setting dpi and figure size? What is different in Ubuntu and Windows?

Can you provide a minimal test case that shows your problem (python code, screenshots etc)? I could run such a script, I have matplotlib installed, then we can compare. I'm no master, though, just played around a bit so far. smile

Right now I can only say: works for me. When I set my script output to pdf, I get a nice vectorized plot inside with, for me, good quality.


Thanks very much for your reply!
I use the standard Python 3 comes with Arch Linux(If I use python2 in virtualenv, matplotlib will have some backend problem). The code for plotting is also pretty standard:

import matplotlib.pyplt as plt

fig=plt.figure()
ax=fig.add_subplot(111)
ax.loglog(x_data, y_data)
ax.fill_between(x_data, y_data1,y_data2)

plt.show()
fig.savefig("result.pdf", format="pdf", dpi=600)

Either I save the figure when the it shows or by "savefig", the pdf picture is not giving me a good quality one like png is. I don't know what could be the problem.

Thanks.

Offline

#6 2014-12-01 08:40:09

FreeTheBee
Member
Registered: 2011-01-02
Posts: 125

Re: Python Pylot savefig()/show() can't generate quality figure in pdf.

For me it also works fine. I tried with and without the dpi setting. I see no difference. I think, because a vector image is created with pdf the dpi setting would normally be ignored, unless there is a bitmap embedded.

Offline

#7 2014-12-01 13:32:56

Sanne
Member
Registered: 2012-03-13
Posts: 87

Re: Python Pylot savefig()/show() can't generate quality figure in pdf.

godott, your example doesn't run for me, you didn't define x_data, y_data, y_data2:

Traceback (most recent call last):
NameError: name 'x_data' is not defined

Can you give a working example that runs as it is, and best also a screenshot of the bad pdf output you're seeing?

About the dpi setting, I think FreeTheBee is correct, as vector data is dpi independent.

Another shot in the dark: what about your optional dependencies on matplotlib? Maybe there's something missing. Check with

$ pacman -Qi python-matplotlib

Here's what I have:

Optional Deps  : python-gobject: for GTK3Agg/GTK3Cairo backend
                 python-cairo: for GTK3Cairo backend [installed]
                 python-pyqt4: for Qt4Agg backend [installed]
                 tk: used by the TkAgg backend [installed]
                 ghostscript: usetex dependencies [installed]
                 texlive-bin: usetex dependencies
                 python-tornado: for webagg backend [installed]
                 python-pillow: for reading/saving .jpg/bmp/tiff files [installed]

If our currency were not money but appreciation and acknowledgement for what we do for others, for the community, for the benefit of all, we would have paradise on earth.

Offline

#8 2014-12-01 21:09:22

godott
Member
Registered: 2013-11-04
Posts: 12

Re: Python Pylot savefig()/show() can't generate quality figure in pdf.

Sanne wrote:

godott, your example doesn't run for me, you didn't define x_data, y_data, y_data2:

Traceback (most recent call last):
NameError: name 'x_data' is not defined

Can you give a working example that runs as it is, and best also a screenshot of the bad pdf output you're seeing?

About the dpi setting, I think FreeTheBee is correct, as vector data is dpi independent.

Another shot in the dark: what about your optional dependencies on matplotlib? Maybe there's something missing. Check with

$ pacman -Qi python-matplotlib

Here's what I have:

Optional Deps  : python-gobject: for GTK3Agg/GTK3Cairo backend
                 python-cairo: for GTK3Cairo backend [installed]
                 python-pyqt4: for Qt4Agg backend [installed]
                 tk: used by the TkAgg backend [installed]
                 ghostscript: usetex dependencies [installed]
                 texlive-bin: usetex dependencies
                 python-tornado: for webagg backend [installed]
                 python-pillow: for reading/saving .jpg/bmp/tiff files [installed]

for x_data, y_data you can do

x_data=numpy.linspace(0, 1, 100)
y_data=numpy.sin(x_data)
y_data1=y_data
y_data2=y_data-1

I installed matplotlib from pip originally. Now I reinstalled it using pacman and have all the dependancies installed. I still have the problem. Especially when I do "fill_between".  I will update the comparison of the result of pdf and png. There is big difference.
pdf https://www.dropbox.com/s/u95y0ohr10lw9 … 5.pdf?dl=0
pnghttps://www.dropbox.com/s/txgk3k8jk9hey … 5.png?dl=0

Last edited by godott (2014-12-01 21:10:14)

Offline

#9 2014-12-01 21:14:32

godott
Member
Registered: 2013-11-04
Posts: 12

Re: Python Pylot savefig()/show() can't generate quality figure in pdf.

Now I see. The problem could be the image viewer. I used both mupdf and xnviewmp to view the generated pdf file. But they both give me bad quality especially when "fill_between". Now if I open with dropbox viewer the figure seems fine. I will try to figure it out.

Thanks @Sanne @FreeTheBee!

BTW what image viewer do you guys use?

Last edited by godott (2014-12-01 21:14:41)

Offline

#10 2014-12-01 21:45:25

Sanne
Member
Registered: 2012-03-13
Posts: 87

Re: Python Pylot savefig()/show() can't generate quality figure in pdf.

Ah, the viewer. Should have thought about that. I usually use Okular, renders fine. Xpdf and ePDFViewer also. Zathura and Mupdf, however, show a white gap between the filling and the border. Is that what you mean?

So the problem seems to be the viewer then. Good to know there are differences.

Last edited by Sanne (2014-12-01 21:45:46)


If our currency were not money but appreciation and acknowledgement for what we do for others, for the community, for the benefit of all, we would have paradise on earth.

Offline

#11 2014-12-02 07:27:14

FreeTheBee
Member
Registered: 2011-01-02
Posts: 125

Re: Python Pylot savefig()/show() can't generate quality figure in pdf.

Same for me, just checked with mupdf. I usually use evince or acroread (although it is time to replace the latter).

Offline

#12 2014-12-04 00:59:23

Sanne
Member
Registered: 2012-03-13
Posts: 87

Re: Python Pylot savefig()/show() can't generate quality figure in pdf.

So is this issue solved now? If so, godott, would you please mark the thread as solved?


If our currency were not money but appreciation and acknowledgement for what we do for others, for the community, for the benefit of all, we would have paradise on earth.

Offline

Board footer

Powered by FluxBB