Ask Your Question

Notebook text and figure layout control

asked 2011-02-17 10:56:02 +0200

mhfrey gravatar image

I have created 2 text strings in a sage notebook and 2 figures with Matplotlib. I in the following top to bottom order: show(B_txt); Bw_fig.canvas.print_figure('bw_plot.png'); show(U_txt); BwU_fig.canvas.print_figure('bwu_plot.png')

The displayed order top to bottom is B_txt, U_Txt; figure; figure. How can I get Sage to display these is in the correct order?

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted

answered 2011-02-17 11:37:17 +0200

Mike Hansen gravatar image

updated 2011-02-17 18:16:50 +0200

There's no good way to put them in the "correct order". Due to the way the notebook works, all of the plots are shown after all of the text output. I don't believe the notebook takes into account the file creation times when choosing what order to display the images -- it's just alphabetical.

Your best bet is to put the first two commands in one cell, and the second two in a different cells.

A slightly more complicated solution is to save the plots to the data directory in a cell like:


We'll use a little helper function to fix issues with reloading. (The timestamp after the image filename keeps the browser from caching it).:

def data_image(filename):
    import time
    return html('<img src="data/%s?%s"/>'%(filename, time.time()))

Then, you can reference those in another cell like:

print "sin(x)"
print data_image('p1.png')
print "cos(x)"
print data_image('p2.png')
edit flag offensive delete link more


That's quite neat!

cswiercz gravatar imagecswiercz ( 2011-02-17 12:53:47 +0200 )edit

answered 2011-02-17 19:56:15 +0200

mhfrey gravatar image

Thank you again, this now works as advertised.

edit flag offensive delete link more

answered 2011-02-17 17:06:49 +0200

mhfrey gravatar image

There is a problem, if I change the parameters and recalculate the print html call does not update to the new data. any ideas on how to fix this?

edit flag offensive delete link more


I've updated my answer to take this into account.

Mike Hansen gravatar imageMike Hansen ( 2011-02-17 18:18:06 +0200 )edit

answered 2011-02-17 12:22:44 +0200

mhfrey gravatar image

Thank you, It is ugly, but it works.

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: 2011-02-17 10:56:02 +0200

Seen: 647 times

Last updated: Feb 17 '11