spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.23k stars 1.6k forks source link

SVG renderer does not properly render matplotlib plots #9987

Open akdor1154 opened 5 years ago

akdor1154 commented 5 years ago

Issue Report Checklist

Problem Description

Spyder's SVG renderer does not support SVG features that are used by matplotlib.

What steps reproduce the problem?

  1. run %config InlineBackend.figure_formats = ['svg']

  2. Render the following plot with matplotlib: Screenshot_2019-08-09_12-19-32 (as rendered by Ristretto on my machine) example.svg.txt (matplotlib's svg output, sorry github doesn't accept direct svg uploads)

(sorry I don't have a MRE, but it uses hatching, .e.g.

    ax.fill_between(
        summarySNS['Date'],
        summarySNS['lowBoundary'],
        summarySNS['highBoundary'],
        facecolor="none",
        edgecolor=getFillColour(color, dl=+0.2, ds=-0.1),
        hatch='//////'
    )

)

  1. Observe that spyder renders it as Screenshot_2019-08-09_11-54-17

What is the expected output? What do you see instead?

The expected output would be the SVG properly rendered, at it would be by a browser.

To be honest I'd love to see you use QWebEngine to display it - that way interactive SVG with Javascript could work as well, and it would be a gateway to displaying plot libraries that output as ipython html widgets...

Versions

Dependencies

pygments >=2.0    :  2.3.1 (OK)
qdarkstyle >=2.6.4:  2.7 (OK)
sphinx >=0.6.6    :  2.1.2 (OK)
psutil >=0.3      :  5.5.1 (OK)
pyls >=0.27.0     :  v0.28.1 (OK)
nbconvert >=4.0   :  5.4.0 (OK)
pandas >=0.13.1   :  0.25.0 (OK)
numpy >=1.7       :  1.16.2 (OK)
sympy >=0.7.3     :  None (NOK)
cython >=0.21     :  None (NOK)
qtconsole >=4.5.2 :  4.5.2 (OK)
IPython >=4.0     :  5.8.0 (OK)
matplotlib >=2.0.0:  3.0.2 (OK)
pylint >=0.25     :  2.2.2 (OK)
ccordoba12 commented 5 years ago

Spyder's SVG renderer does not support SVG features that are used by matplotlib.

We're limited to what QtSvg can render, so there's nothing else we can do about it.

To be honest I'd love to see you use QWebEngine to display it - that way interactive SVG with Javascript could work as well, and it would be a gateway to displaying plot libraries that output as ipython html widgets...

That's planned work for Spyder 5.

akdor1154 commented 4 years ago

@ccordoba12 would you accept a dependency on libresvg? It's a reasonably standalone (i.e. doesn't need to pull in Cairo) Rust SVG library. I can probably create Python bindings + prebuilt wheels for it if Spyder would consider using it.

ccordoba12 commented 4 years ago

I think you'll need to add it to qtconsole because we're not in charge of rendering plots directly. We just receive what qtconsole send us and display it in the Plots pane.

Besides, I don't understand how the interaction between that library and Qt would work.

jnsebgosselin commented 4 years ago

I think you'll need to add it to qtconsole because we're not in charge of rendering plots directly. We just receive what qtconsole send us and display it in the Plots pane.

Besides, I don't understand how the interaction between that library and Qt would work.

If we had access to such a package, I could most likely use it to render svg plots in the Plots pane instead of using the QSvgRenderer, which as you mentioned is limited.

In the Plot pane, we always paint a bitmap on a widget, whether we receive a png, jpeg or svg from the console, i.e. that we always use the QPainter.drawPixmap method. So the QSvgRenderer is only used to pre-convert and pre-scale the svg string that we receive from the console into a QImage before painting it. We do not use it to paint the image on the widget from the svg data directly. So, I'm pretty sure we could use anoter svg renderer to do the conversion and render svg plots in the Plots pane.

ccordoba12 commented 4 years ago

Thanks a lot for letting us know about this @jnsebgosselin! This is certainly something doable and it'd be a great improvement for Spyder 5.

jnsebgosselin commented 3 years ago

For future references, here is a link to the resvg repository on github : https://github.com/RazrFalcon/resvg

And here is an interesting article about the library : http://libregraphicsworld.org/blog/entry/introducing-libresvg

ma-sadeghi commented 3 years ago

Hi, is this still planned for v5?

ccordoba12 commented 3 years ago

We'll try to add it in the next three to six months to Spyder 5.