Ask Your Question

Is there a simple way to plot an image in a notebook? I.e. the output of the imshow function in matplotlib...

asked 2011-12-01 08:36:21 -0500

anonymous user


I have been wondering how to do something seemingly really simple, but seem to be missing something - sage allows users to do all kinds of fancy plotting in the notebook but I can't get it to display a 2d matrix as an image (matplotlib's imshow function).

I see that it is possible to call imshow and save the result to file, but I want to display the result in the notebook instead - did I miss something?


edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2011-12-01 09:13:36 -0500

kcrisman gravatar image

I've modified the examples you're alluding to in the Sage plotting reference, under "NUMERICAL PLOTTING" (caps in original).

from pylab import * 

This works. Because I don't know much about how matplotlib works, unfortunately when I do another figure it just adds to this one (or if I do another one first, this adds to it). Hopefully someone else - or you - can answer that for me!

edit flag offensive delete link more


And once that's resolved, we should probably add this to the doc, right?

kcrisman gravatar imagekcrisman ( 2011-12-01 09:13:56 -0500 )edit

use clf() to "clear" the figure and stop adding to it.

Jason Grout gravatar imageJason Grout ( 2011-12-01 14:52:39 -0500 )edit

Jason (G.), let's add that to the documentation, shall we? This is now More and more SD 35.5 fodder...

kcrisman gravatar imagekcrisman ( 2011-12-03 03:49:51 -0500 )edit

I'm doing imshow(img) and I get this AxesImage(60,48;372x384) AxesImage(60,48;372x384) What does it mean?

roi.holtzman gravatar imageroi.holtzman ( 2017-06-08 12:16:13 -0500 )edit

It means it generates an image. Sage doesn't actually show the image onscreen without further commands, such as savefig; see @benjaminfjones answer below.

kcrisman gravatar imagekcrisman ( 2017-06-08 12:37:49 -0500 )edit

answered 2011-12-01 09:26:02 -0500

benjaminfjones gravatar image

(referencing @kcrisman 's answer) I think it is maybe better not to import pylab into the global namespace, so just import pylab. To reset the plot, so to speak, you should call pylab.close() (or just close() if you import *) like this:

import pylab
edit flag offensive delete link more


Do these saved files ever get deleted once they are created?

Jason gravatar imageJason ( 2011-12-01 11:13:30 -0500 )edit

Only if you do it yourself. If you're working in the Sage notebook, they exist in the DATA directory associated to the worksheet. For example when I ran the above in the Sage notebook I get files created called $HOME/.sage/sage_notebook.sagenb/home/admin/0/cells/1/plot1.png

benjaminfjones gravatar imagebenjaminfjones ( 2011-12-01 12:30:29 -0500 )edit

They aren't stored in the DATA directory, but in a cell-specific output directory. They are supposed to be deleted when the cell is deleted, or even when the output of the cell is deleted (but apparently this didn't actually happen, but a patch was recently merged that actually does delete such files).

Jason Grout gravatar imageJason Grout ( 2011-12-01 14:51:49 -0500 )edit

answered 2011-12-01 14:54:25 -0500

Jason Grout gravatar image

matrix_plotuses matplotlib's imshow to plot a matrix. matrix_plot should be called automatically if you try to plot a matrix. See, for example,

edit flag offensive delete link more


Oh, I thought he was asking about wanting to do it in the context of some other mpl stuff in the notebook, not just to plot a matrix.

kcrisman gravatar imagekcrisman ( 2011-12-03 03:47:07 -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


Asked: 2011-12-01 08:36:21 -0500

Seen: 1,168 times

Last updated: Dec 01 '11