Export plot to a .pdf file.

405 views
Skip to first unread message

Trifon Trifonov

unread,
Mar 11, 2019, 5:25:29 PM3/11/19
to pyqtgraph
The title says it all.

Currently I can export to an .svg, which I can convert to a .pdf,

or

for some plots I can use the matplotlib UI exporter and from there to save a pdf.


None of these is convenient and fast.


Does anybody have an idea how can I hack for exaple the ImageExporter.py or the  SVGExporter.py to produce a .pdf output?





Trifon Trifonov

unread,
Mar 11, 2019, 6:52:47 PM3/11/19
to pyqtgraph
Clarifications why using the pyqtgraph's .png and .svg export does not work for me:

1. The original .png export is usually with very low resolution for scientific publication.
One can change the "width" and "height" of the export, which has an effect, but not to
lines! For example see the plot:

test.png

the model is barley visible, while on the GUI I see it like this:

test_org.png

Ok, so .png --> .pdf will not work!


2. The SVG output is also very strange:

test_svg_pdf_png.png

The last image is svg --> pdf --> png (so I can upload it.) The model of this export also looks like that there is no anti-aliasing. It does not look good on a paper.


If someone can help on any of these will be highly appreciated.

I love how pyqtgraph works on my GUI project, but at the moment I simply do not have a good quality plot out of it!

Jonathán de Jesús Estrella Ramírez

unread,
Nov 17, 2020, 2:02:36 AM11/17/20
to pyqtgraph
Could you export the plot to PDF directly?, I'm in the same situation

Jerzy Karczmarczuk

unread,
Nov 17, 2020, 8:30:27 AM11/17/20
to pyqt...@googlegroups.com


Le 17/11/2020 à 08:02, Jonathán de Jesús Estrella Ramírez a écrit :
Could you export the plot to PDF directly?, I'm in the same situation

El lunes, 11 de marzo de 2019 a la(s) 16:52:47 UTC-6, 3fon....@gmail.com escribió:
Clarifications why using the pyqtgraph's .png and .svg export does not work for me:
/.../

Ok, so .png --> .pdf will not work!


2. The SVG output is also very strange:

test_svg_pdf_png.png

The last image is svg --> pdf --> png (so I can upload it.) The model of this export also looks like that there is no anti-aliasing. It does not look good on a paper.

Also, Trifon Trifonov complained, and used such  words as "convenient and fast"...  But 'Convenient' is very relative, and 'fast' even more...

I must say that I do not understand the issue. I had never any true problems with SVG. What is strange in the .svg export??  That after two conversions you lost the aliasing?

Many people (including some hundreds of my students)  look too often for magic solutions, but there are some good universal tools. Try the following:

1. Export duly the .svg file.

2. Open it with Inkscape. Edit it at your convenience.

3. Export as PDF, or as PNG, or whatever. But look what you are doing, in particular if your soft didn't change the colour space...

Too many people insist on including PNG graphics in their publications, theses, etc. Well, those who include scientific plots as JPG are even worse, but pdfLaTeX permits to include pdfs directly, and a pdf exported from Inkscape has the "vector" resolution.

==

Jerzy Karczmarczuk
/Caen, France/


Trifon Trifonov

unread,
Nov 23, 2020, 7:09:53 AM11/23/20
to pyqtgraph
Dear Jerzy,

The SVG export within the pyqtgraph NEVER exports the plot as seen in the interactive plot. See the attached file. The data are not filled, and the model is with SUPER low resolution. How Can I put this in my paper? I tried Inkscape with the exported files, with not much success. Maybe I did not get how to work with Inkscape, but after many hours trying to produce reasonable plots I simply give up.

Try your self:


load a data set, perform a model, and try to export. If you can provide me a reasonable .PDF from a SVG export I would be very happy to learn from you.

My solution at the moment is to export to a custom version of the matplotlib exporter GUI, and from there to PDF.

Yet, please agree with me, export directly in .PS, EPS or preferably .PDF is the best solution.


I will open an Issue in the GitHub. Let's see what will be the outcome.

Trifon

Jamie Rollins

unread,
Dec 7, 2020, 12:30:04 PM12/7/20
to pyqtgraph
fwiw, the following works well for me:
```
    from pyqtgraph import exporters
    exporter = exporters.SVGExporter(scene)
    svg = exporter.export(toBytes=True)
    pdf = cairosvg.svg2pdf(svg)
```
I note though that there's a bug in the SVG exporter such that the viewBox is not set correctly, so I add the following hack to fix it:
```
    import xml.etree.ElementTree as ET
    ET.register_namespace("", "http://www.w3.org/2000/svg")
    root = ET.fromstring(svg)
    size = scene.parent().size()
    width = size.width()
    height = size.height()
    root.attrib['viewBox'] = f"0 0 {width} {height}"
    # FIXME: in 3.9 we can just do this:
    #out = ET.tostring(root, encoding='UTF-8', xml_declaration=True)
    with io.BytesIO() as bout:
        ET.ElementTree(root).write(bout, encoding='UTF-8', xml_declaration=True)
        svg = bout.getvalue()
```
Reply all
Reply to author
Forward
0 new messages