Displaying images with matplotlib

Hi! I'm unable to get some of my old code (written in the Sage notebook around 2011) to work on CoCalc. I found very similar code on the SageMath interact/graphics wiki page at

https://wiki.sagemath.org/interact/graphics#Interact_with_matplotlib

That code also doesn't work for me in CoCalc: no image gets displayed, just the two slider bars. Is there a simple change that needs to be made?

In general, should I expect Sage code to work in a CoCalc Sage worksheet? I admit that I don't know to what extent the two are compatible. Thanks for any advice!

Here is the code from that SageMath Wiki:

# Simple example demonstrating how to interact with matplotlib directly.
# Comment plt.clf() to get the plots overlay in each update.
# Gokhan Sever & Harald Schilly (2010-01-24)

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt

@interact
def plot_norm(loc=(0,(0,10)), scale=(1,(1,10))):
rv = stats.norm(loc, scale)
x = np.linspace(-10,10,1000)
plt.plot(x,rv.pdf(x))
plt.grid(True)
plt.savefig('plt.png')
plt.clf()
edit retag close merge delete

Sort by » oldest newest most voted

Easy: use plt.show() to actually show the plot instead of saving it to a file:

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt

@interact
def plot_norm(loc=(0,(0,10)), scale=(1,(1,10))):
rv = stats.norm(loc, scale)
x = np.linspace(-10,10,1000)
plt.plot(x,rv.pdf(x))
plt.grid(True)
plt.show()
more

This works in a .sagews file in CoCalc. It does not work interactively in a Jupiter notebook in CoCalc, but I understand that interacts don't work in that notebook in CoCalc right now.

@calc314, I cocalc you can click on the "classical jupyter" button to switch to the legacy implementation. It has some drawbacks and performance issues, but interacts should work there.

Hi, thank you for posting this question. I added the link to the Sage wiki in your question. In general, you should expect most Sage code to work in all settings. But sometimes it's not quite the case. I tried the code:

• on my computer in a legacy SageNB notebook worksheet (by running sage -n sagenb) -- it worked
• on my computer in a Jupyter notebook worksheet (by running sage -n jupyter) -- it did not work
• on CoCalc in a .sagews worksheet -- it did not work
• on CoCalc in a .ipynb Jupyter notebook worksheet -- it did not work

If you make a support request we can probably help you rewrite code...

You should definitely not expect everything that worked in sagenb to work anywhere else. For example, the above code only works with sagenb because one day Tom Boothby had the funny idea: heh, if any code running in a cell creates a file as a side effect, let's just show it. There are no other notebook interfaces in existence as far as I can tell that do that -- definitely not sagews, jupyter, .... It seemed like a good idea at the time, but wasn't. :-)

So if I were you I would contact the CoCalc support team and try to get help to rewrite your interact. Let us know here when you have a replacement, and maybe edit the "graphics interacts" wiki page too.

more

Thanks very much for your help! Jeroen's example worked in my sagews