Ask Your Question

GUI backend MATPLOTLIB in Sage

asked 2014-03-25 06:45:29 +0200

gundamlh gravatar image

updated 2017-01-08 12:19:04 +0200

FrédéricC gravatar image

Dear all,


How can one export the data (a object) 'matplotlib.figure.Figure' the namespace of Sagemath to IPython? Can the data be saved in a format (not png, pdf, etc.) that can be opened in IPython using the function show()?

Here is a related Question. I have followed this instruction step by step, every step was successfully executed without any error report/message. However I couldn't invoke TkAgg as Backend of Sagemath nor IPython.

Has SAGE a GUI back-end as MATPLOTLIB, on which one can zoom, select points etc.?

for example,

    sage: bodes['entire']
    <matplotlib.figure.Figure at 0x91ac950>
    sage: bodes['entire'].show() 
    # It doesn't work and a Warning occurs. 
    site-packages/matplotlib/ UserWarning: matplotlib is 
    currently using a non-GUI backend, so cannot show the figure 
    "matplotlib is currently using a non-GUI backend, "
    sage: f = bodes['entire']
    sage: f.savefig('./figures/entire.png') 
    # I can only save my plotting in formats : png, pdf, etc..

Thank you all in advance!

edit retag flag offensive close merge delete


Only steps 6 to 9 are important because by default matplotlib is built without GUI support within Sage. It worked for me. Try again with removing all related to TkAgg.

vdelecroix gravatar imagevdelecroix ( 2014-03-27 16:55:49 +0200 )edit
vdelecroix gravatar imagevdelecroix ( 2014-04-14 03:44:58 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2014-04-14 03:57:53 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

I am just repeating the solution of N. Dunfield at for backup

Matplotlib does all the 2D graphics for Sage, but unfortunately none of its GUI backends are compiled by default. The following suffices to compile the Tk backend on Linux, provided you have the tk-dev(el) package installed:

$ sage -f matplotlib

This doesn't work on OS X because in addition to TkAgg, Sage will try to compile the native Mac backend, which fails since Sage doesn't include an Objective-C compiler. For OS X, download the source tarball for matplotlib, and then do

$ cd matplotlib-*
$ sage -sh echo '[gui_support]' >> setup.cfg
$ echo 'macosx=false' >> setup.cfg
$ python install

Alternatively use the patched spkg (which has a modified file at the top leve ) available at

Note that the TkAgg backend will be compiled against one's current version of Tk, which might not be the one that Sage is linked against. So inaddition one may need to recompile the Tkinter module via

$ sage -f python

Then any matplotlib command will work:

sage: from numpy import arange, sin, pi
sage: import matplotlib.pyplot as plt
sage: X = arange(0.0, 2*pi, 0.01)
sage: plt.plot(X, sin(X))

Something seems to be broken since version 5.11, the GUI blocks the console. In order to correct this behaviour one solution is to use the following (see also #15152):

sage: import IPython.lib.inputhook as ih
sage: ih.clear_inputhook()

Note that after that command, the magic %attach will be broken.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2014-03-25 06:45:29 +0200

Seen: 4,259 times

Last updated: Apr 14 '14