ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 29 Sep 2017 05:15:38 -0500Displaying images with matplotlibhttp://ask.sagemath.org/question/38982/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](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()Tue, 26 Sep 2017 18:08:07 -0500http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/Answer by Jeroen Demeyer for <p>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</p>
<p><a href="https://wiki.sagemath.org/interact/graphics#Interact_with_matplotlib">https://wiki.sagemath.org/interact/graphics#Interact_with_matplotlib</a></p>
<p>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? </p>
<p>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!</p>
<p>Here is the code from that SageMath Wiki:</p>
<pre><code># 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()
</code></pre>
http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?answer=38992#post-id-38992Easy: 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()Thu, 28 Sep 2017 10:37:36 -0500http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?answer=38992#post-id-38992Comment by schilly for <p>Easy: use <code>plt.show()</code> to actually show the plot instead of saving it to a file:</p>
<pre><code>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()
</code></pre>
http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?comment=38999#post-id-38999@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.Fri, 29 Sep 2017 05:15:38 -0500http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?comment=38999#post-id-38999Comment by Christopher Davis for <p>Easy: use <code>plt.show()</code> to actually show the plot instead of saving it to a file:</p>
<pre><code>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()
</code></pre>
http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?comment=38995#post-id-38995Thanks Jeroen!Thu, 28 Sep 2017 14:35:01 -0500http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?comment=38995#post-id-38995Comment by calc314 for <p>Easy: use <code>plt.show()</code> to actually show the plot instead of saving it to a file:</p>
<pre><code>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()
</code></pre>
http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?comment=38993#post-id-38993This 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.Thu, 28 Sep 2017 12:16:25 -0500http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?comment=38993#post-id-38993Answer by slelievre for <p>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</p>
<p><a href="https://wiki.sagemath.org/interact/graphics#Interact_with_matplotlib">https://wiki.sagemath.org/interact/graphics#Interact_with_matplotlib</a></p>
<p>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? </p>
<p>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!</p>
<p>Here is the code from that SageMath Wiki:</p>
<pre><code># 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()
</code></pre>
http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?answer=38983#post-id-38983Hi, 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.Tue, 26 Sep 2017 19:08:28 -0500http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?answer=38983#post-id-38983Comment by Christopher Davis for <p>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:</p>
<ul>
<li>on my computer in a legacy SageNB notebook worksheet (by running <code>sage -n sagenb</code>) -- it worked</li>
<li>on my computer in a Jupyter notebook worksheet (by running <code>sage -n jupyter</code>) -- it did not work</li>
<li>on CoCalc in a .sagews worksheet -- it did not work</li>
<li>on CoCalc in a .ipynb Jupyter notebook worksheet -- it did not work</li>
</ul>
<p>Here is what William Stein says about this:</p>
<blockquote>
<p>If you make a support request we can probably help you rewrite code...</p>
<p>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 <em>creates a file as a side effect</em>,
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. :-)</p>
</blockquote>
<p>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.</p>
http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?comment=38996#post-id-38996Thanks very much for your help! Jeroen's example worked in my sagewsThu, 28 Sep 2017 14:35:37 -0500http://ask.sagemath.org/question/38982/displaying-images-with-matplotlib/?comment=38996#post-id-38996