Ask Your Question
0

Notebook text and figure layout control

asked 14 years ago

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?

Preview: (hide)

4 Answers

Sort by » oldest newest most voted
3

answered 14 years ago

Mike Hansen gravatar image

updated 14 years ago

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:

plot(sin(x)).save(DATA+"/p1.png")
plot(cos(x)).save(DATA+"/p2.png")

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')
Preview: (hide)
link

Comments

That's quite neat!

cswiercz gravatar imagecswiercz ( 14 years ago )
0

answered 14 years ago

mhfrey gravatar image

Thank you again, this now works as advertised.

Preview: (hide)
link
0

answered 14 years ago

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?

Preview: (hide)
link

Comments

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

Mike Hansen gravatar imageMike Hansen ( 14 years ago )
0

answered 14 years ago

mhfrey gravatar image

Thank you, It is ugly, but it works.

Preview: (hide)
link

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: 14 years ago

Seen: 807 times

Last updated: Feb 17 '11