Ask Your Question
2

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 15:36:21 +0100

anonymous user

Anonymous

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?

Cheers

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
2

answered 2011-12-01 16:13:36 +0100

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 * 
imshow([[(0,0,0)]])
savefig('')

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

Comments

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

kcrisman gravatar imagekcrisman ( 2011-12-01 16:13:56 +0100 )edit
1

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

Jason Grout gravatar imageJason Grout ( 2011-12-01 21:52:39 +0100 )edit

Jason (G.), let's add that to the documentation, shall we? This is now http://trac.sagemath.org/sage_trac/ticket/12114. More and more SD 35.5 fodder...

kcrisman gravatar imagekcrisman ( 2011-12-03 10:49:51 +0100 )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 19:16:13 +0100 )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 19:37:49 +0100 )edit
0

answered 2011-12-01 21:54:25 +0100

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, http://sagenb.org/home/pub/1685/

edit flag offensive delete link more

Comments

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 10:47:07 +0100 )edit
0

answered 2011-12-01 16:26:02 +0100

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
pylab.imshow([[1,0],[0,1]])
pylab.savefig('plot1.png')
pylab.close()
pylab.imshow([[1,2],[0,1]])
pylab.savefig('plot2.png')
edit flag offensive delete link more

Comments

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

Jason gravatar imageJason ( 2011-12-01 18:13:30 +0100 )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 19:30:29 +0100 )edit
1

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 21:51:49 +0100 )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

Stats

Asked: 2011-12-01 15:36:21 +0100

Seen: 1,801 times

Last updated: Dec 01 '11