Ask Your Question
2

How to set the matplotlib backend used by plot()

asked 2020-10-08 18:50:44 -0500

leogama gravatar image

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.

edit retag flag offensive close merge delete

Comments

2

I 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 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 helps you:

sage: from sage.repl.rich_output import get_display_manager 
sage: dm = get_display_manager();  dm     
sage: dm.preferences
Sébastien gravatar imageSébastien ( 2020-10-10 08:28:02 -0500 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2020-10-10 20:39:51 -0500

leogama gravatar image

updated 2020-10-10 20:42:57 -0500

Thank 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 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!

edit flag offensive delete link more

Comments

Which interface are you using? Because, in Jupyter, the Graphics2d objects are shown inline automatically right?

Sébastien gravatar imageSébastien ( 2020-10-11 14:20:50 -0500 )edit

Also 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.

Sébastien gravatar imageSébastien ( 2020-10-11 15:00:57 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-10-08 18:50:44 -0500

Seen: 59 times

Last updated: Oct 10