Ask Your Question
2

Displaying images with matplotlib

asked 2017-09-27 01:08:07 +0100

Christopher Davis gravatar image

updated 2017-09-27 02:03:01 +0100

slelievre gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2017-09-28 17:37:36 +0100

Jeroen Demeyer gravatar image

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()
edit flag offensive delete link more

Comments

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 gravatar imagecalc314 ( 2017-09-28 19:16:25 +0100 )edit

Thanks Jeroen!

Christopher Davis gravatar imageChristopher Davis ( 2017-09-28 21:35:01 +0100 )edit

@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.

Harald Schilly gravatar imageHarald Schilly ( 2017-09-29 12:15:38 +0100 )edit
1

answered 2017-09-27 02:08:28 +0100

slelievre gravatar image

updated 2017-09-28 03:54:32 +0100

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

Here is what William Stein says about this:

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.

edit flag offensive delete link more

Comments

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

Christopher Davis gravatar imageChristopher Davis ( 2017-09-28 21:35:37 +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

1 follower

Stats

Asked: 2017-09-27 01:08:07 +0100

Seen: 1,463 times

Last updated: Sep 28 '17