ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 11 Oct 2020 22:00:57 +0200How to set the matplotlib backend used by plot()https://ask.sagemath.org/question/53794/how-to-set-the-matplotlib-backend-used-by-plot/SageMath uses `matplotlib` as its graphics engine and the module itself can work with many backends. But how can I set a GUI matplotlib backend to be used by the `plot()` function and its friends, at least in the command-line interface? And is there a way to set a default backend?
In my setup, `plot()` doesn't respect the backend set by the `%matplotlib` magic, but `matplotlib.pyplot.plot()` does. Even worse, the default backend creates a PNG image file and opens it in the system's viewer.
I'm on macOS 10.13 "High Sierra" running SageMath 9.1, the binary app version.Fri, 09 Oct 2020 01:50:44 +0200https://ask.sagemath.org/question/53794/how-to-set-the-matplotlib-backend-used-by-plot/Comment by Sébastien for <p>SageMath uses <code>matplotlib</code> as its graphics engine and the module itself can work with many backends. But how can I set a GUI matplotlib backend to be used by the <code>plot()</code> function and its friends, at least in the command-line interface? And is there a way to set a default backend?</p>
<p>In my setup, <code>plot()</code> doesn't respect the backend set by the <code>%matplotlib</code> magic, but <code>matplotlib.pyplot.plot()</code> does. Even worse, the default backend creates a PNG image file and opens it in the system's viewer.</p>
<p>I'm on macOS 10.13 "High Sierra" running SageMath 9.1, the binary app version.</p>
https://ask.sagemath.org/question/53794/how-to-set-the-matplotlib-backend-used-by-plot/?comment=53816#post-id-53816I don't have an answer, but few ideas. The `_rich_repr_` method of `Graphics2d` object is what tells to open the figure in the system's viewer. Maybe you can achieve what you want by [extracting the matplotlib](https://doc.sagemath.org/html/en/reference/plotting/sage/plot/graphics.html#sage.plot.graphics.Graphics.matplotlib) object from the Graphics2d object as in:
sage: p = plot(sin(x), (x, -2*pi, 2*pi))
sage: figure = p.matplotlib()
sage: axes = figure.axes[0]
Or you may check if changing the preferences of the [display manager](https://doc.sagemath.org/html/en/reference/repl/sage/repl/rich_output/display_manager.html) helps you:
sage: from sage.repl.rich_output import get_display_manager
sage: dm = get_display_manager(); dm
sage: dm.preferencesSat, 10 Oct 2020 15:28:02 +0200https://ask.sagemath.org/question/53794/how-to-set-the-matplotlib-backend-used-by-plot/?comment=53816#post-id-53816Answer by leogama for <p>SageMath uses <code>matplotlib</code> as its graphics engine and the module itself can work with many backends. But how can I set a GUI matplotlib backend to be used by the <code>plot()</code> function and its friends, at least in the command-line interface? And is there a way to set a default backend?</p>
<p>In my setup, <code>plot()</code> doesn't respect the backend set by the <code>%matplotlib</code> magic, but <code>matplotlib.pyplot.plot()</code> does. Even worse, the default backend creates a PNG image file and opens it in the system's viewer.</p>
<p>I'm on macOS 10.13 "High Sierra" running SageMath 9.1, the binary app version.</p>
https://ask.sagemath.org/question/53794/how-to-set-the-matplotlib-backend-used-by-plot/?answer=53825#post-id-53825Thank you, @sébastien. From your comment and documentation references, it seems clear that:
1. The `%matplotlib` is an IPython-only mechanism; and
2. SageMath's command-line IPython backend currently doesn't support `matplolib` GUI.
The second conclusion comes from the reported by `get_display_manager().supported_output()`:
```
frozenset({...,
<class 'sage.repl.rich_output.output_graphics.OutputImageDvi'>,
<class 'sage.repl.rich_output.output_graphics.OutputImageGif'>,
<class 'sage.repl.rich_output.output_graphics.OutputImagePdf'>,
<class 'sage.repl.rich_output.output_graphics.OutputImagePng'>, ...})
```
The only other output types for 2D images defined in [Sage's library](https://doc.sagemath.org/html/en/reference/repl/sage/repl/rich_output/output_graphics.html) are for JPEG and SVG files. Not a single mention to `matplotlib` GUI backends. It would be necessary to define a new output type class in order to use it natively in Sage, although it doesn't seem too hard to implement.
For now, I'll live with the `Graphics2d` to `matplotlib` object conversion!Sun, 11 Oct 2020 03:39:51 +0200https://ask.sagemath.org/question/53794/how-to-set-the-matplotlib-backend-used-by-plot/?answer=53825#post-id-53825Comment by Sébastien for <p>Thank you, <a href="/users/23197/sebastien/">@Sébastien</a>. From your comment and documentation references, it seems clear that:</p>
<ol>
<li>The <code>%matplotlib</code> is an IPython-only mechanism; and</li>
<li>SageMath's command-line IPython backend currently doesn't support <code>matplolib</code> GUI.</li>
</ol>
<p>The second conclusion comes from the reported by <code>get_display_manager().supported_output()</code>:</p>
<p><code>
frozenset({...,
<class 'sage.repl.rich_output.output_graphics.OutputImageDvi'>,
<class 'sage.repl.rich_output.output_graphics.OutputImageGif'>,
<class 'sage.repl.rich_output.output_graphics.OutputImagePdf'>,
<class 'sage.repl.rich_output.output_graphics.OutputImagePng'>, ...})
</code></p>
<p>The only other output types for 2D images defined in <a href="https://doc.sagemath.org/html/en/reference/repl/sage/repl/rich_output/output_graphics.html">Sage's library</a> are for JPEG and SVG files. Not a single mention to <code>matplotlib</code> GUI backends. It would be necessary to define a new output type class in order to use it natively in Sage, although it doesn't seem too hard to implement.</p>
<p>For now, I'll live with the <code>Graphics2d</code> to <code>matplotlib</code> object conversion!</p>
https://ask.sagemath.org/question/53794/how-to-set-the-matplotlib-backend-used-by-plot/?comment=53834#post-id-53834Which interface are you using? Because, in Jupyter, the Graphics2d objects are shown inline automatically right?Sun, 11 Oct 2020 21:20:50 +0200https://ask.sagemath.org/question/53794/how-to-set-the-matplotlib-backend-used-by-plot/?comment=53834#post-id-53834Comment by Sébastien for <p>Thank you, <a href="/users/23197/sebastien/">@Sébastien</a>. From your comment and documentation references, it seems clear that:</p>
<ol>
<li>The <code>%matplotlib</code> is an IPython-only mechanism; and</li>
<li>SageMath's command-line IPython backend currently doesn't support <code>matplolib</code> GUI.</li>
</ol>
<p>The second conclusion comes from the reported by <code>get_display_manager().supported_output()</code>:</p>
<p><code>
frozenset({...,
<class 'sage.repl.rich_output.output_graphics.OutputImageDvi'>,
<class 'sage.repl.rich_output.output_graphics.OutputImageGif'>,
<class 'sage.repl.rich_output.output_graphics.OutputImagePdf'>,
<class 'sage.repl.rich_output.output_graphics.OutputImagePng'>, ...})
</code></p>
<p>The only other output types for 2D images defined in <a href="https://doc.sagemath.org/html/en/reference/repl/sage/repl/rich_output/output_graphics.html">Sage's library</a> are for JPEG and SVG files. Not a single mention to <code>matplotlib</code> GUI backends. It would be necessary to define a new output type class in order to use it natively in Sage, although it doesn't seem too hard to implement.</p>
<p>For now, I'll live with the <code>Graphics2d</code> to <code>matplotlib</code> object conversion!</p>
https://ask.sagemath.org/question/53794/how-to-set-the-matplotlib-backend-used-by-plot/?comment=53840#post-id-53840Also note that `p.matplotlib()` support many options which Graphics2d uses to set the image. Using no options may explain why the figure is not the same as the one obtained from just showing the Graphics2d object.Sun, 11 Oct 2020 22:00:57 +0200https://ask.sagemath.org/question/53794/how-to-set-the-matplotlib-backend-used-by-plot/?comment=53840#post-id-53840