ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 14 Sep 2018 14:16:52 +0200Only one of two identical entries in the notebook gives graphics output for show()https://ask.sagemath.org/question/43638/only-one-of-two-identical-entries-in-the-notebook-gives-graphics-output-for-show/I use SageMath to teach a bunch of kids. I have to confess a certain amount of frustration with the SageMath notebook. In particular, tonight I composed a notebook entry which shows some data points and curve-fitted plots. I pressed Shift-Enter to evaluate, and there was no graphical output (nor any error message).
And then I copied/pasted the exact same entry into a blank entry in the same SageMath notebook, pressed Shift-Enter, and it showed the graphical output.
Yes, this is the SAME notebook page which, if you scroll up, shows no graphical output (I inserted a print("Ok.") just to confirm that there was SOME output), and if you scroll down, you see the IDENTICAL python-like SageMath script, and then it shows the graphics of the plotted points as desired.
How could this happen? Is there something that happens in between notebook entries? Perhaps only the last entry is evaluated? I will try posting screenshots.
ok, the screen shots are as follows:
<a href="https://pasteboard.co/HDowwUc.png">no plot, just text</a>
<a href="https://pasteboard.co/HDoxbZ1.png">plot after the text</a>
in the first entry, you can see the notebook entry, and then underneath in blue the SageMath output "Ok.", and that's all; the next notebook entry follows immediately. There is no plot.
in the second entry, you can see the notebook entry, and then underneath in blue the SageMath output "Ok.", and under that is the plot of the two data points and the axes.
For what it's worth, the SageMath notebook entry is as follows. The variable "stage" is so that I can easily turn on/off various parts of the entry by modifying it as I get to various parts of the lesson. (But there is no difference between the entry that works and the entry that doesn't.)
"all_go" is the accumulation of all the graphics objects that I want to display, that is, the output of the points() function in this case, and depending on the setting of my "stage" variable, possibly also a plot of f(x).
I am using SageMath 7.5.1 on Kubuntu 14.04. I know it's not the newest version, but I mst be missing something.
Any help would be appreciated
<pre>
---( start )---
stage="p2m1"
# stage can be p2 or p3 = data points
# stage can be d0 or d1 = display
# stage can be m1 or m2 or me = model
var('a,b,c')
if stage.find("p2") >= 0 :
mydata = [[1,2],[3,5]]
else :
mydata = [[1,2],[2,4],[3,5]]
#mydata
mydata_plot = points(mydata,color='purple', size=100)
if stage.find("m1") >= 0 :
mymodel(x) = b*x + c
elif stage.find("m2") >= 0 :
mymodel(x) = a*x^2 + b*x + c
elif stage.find("me") >= 0 :
mymodel(x) = a*exp(b*x) + c
myfit = find_fit(mydata,mymodel,solution_dict=True)
if stage.find("m1") >= 0 :
f(x) = mymodel(b=myfit[b], c=myfit[c])
else :
f(x) = mymodel(a=myfit[a], b=myfit[b], c=myfit[c])
#f(x) = 6 - 2^(3-x)
#f(x) = (-8) * exp( x*(-ln(2))) + 6
mymodel_plot = plot( f, (x,0,4,), color='red')
all_go = mydata_plot
if stage.find("d1") >= 0 :
all_go += mymodel_plot
t_min=0
t_max=5
t_default=1.5
sample_t=t_default
global_go = all_go
all_go
if stage.find("d1") < 0 :
all_go.show()
print("Ok.")
elif stage.find("d1") >= 0 :
@interact
def _(sample_t=slider(t_min, t_max, default=t_default, step_size=0.1)):
global global_go
all_go = global_go
#sample_t=2
output_y = f(sample_t)
point_tuple = ( sample_t, output_y )
plotpoint_go = point2d( point_tuple , size=200, color='green')
all_go += plotpoint_go
all_go.show()
f.show()
print( "f(" + str(sample_t) + ") = " + str(output_y) )
---( end )---
</pre>
Tue, 11 Sep 2018 15:14:09 +0200https://ask.sagemath.org/question/43638/only-one-of-two-identical-entries-in-the-notebook-gives-graphics-output-for-show/Answer by tmonteil for <p>I use SageMath to teach a bunch of kids. I have to confess a certain amount of frustration with the SageMath notebook. In particular, tonight I composed a notebook entry which shows some data points and curve-fitted plots. I pressed Shift-Enter to evaluate, and there was no graphical output (nor any error message).</p>
<p>And then I copied/pasted the exact same entry into a blank entry in the same SageMath notebook, pressed Shift-Enter, and it showed the graphical output.</p>
<p>Yes, this is the SAME notebook page which, if you scroll up, shows no graphical output (I inserted a print("Ok.") just to confirm that there was SOME output), and if you scroll down, you see the IDENTICAL python-like SageMath script, and then it shows the graphics of the plotted points as desired.</p>
<p>How could this happen? Is there something that happens in between notebook entries? Perhaps only the last entry is evaluated? I will try posting screenshots.</p>
<p>ok, the screen shots are as follows:</p>
<p>https://pasteboard.co/HDowwUc.png (no plot, just text)</p>
<p>https://pasteboard.co/HDoxbZ1.png (plot after the text)</p>
<p>in the first entry, you can see the notebook entry, and then underneath in blue the SageMath output "Ok.", and that's all; the next notebook entry follows immediately. There is no plot.</p>
<p>in the second entry, you can see the notebook entry, and then underneath in blue the SageMath output "Ok.", and under that is the plot of the two data points and the axes.</p>
<p>For what it's worth, the SageMath notebook entry is as follows. The variable "stage" is so that I can easily turn on/off various parts of the entry by modifying it as I get to various parts of the lesson. (But there is no difference between the entry that works and the entry that doesn't.)</p>
<p>"all_go" is the accumulation of all the graphics objects that I want to display, that is, the output of the points() function in this case, and depending on the setting of my "stage" variable, possibly also a plot of f(x).</p>
<p>I am using SageMath 7.5.1 on Kubuntu 14.04. I know it's not the newest version, but I mst be missing something.</p>
<p>Any help would be appreciated</p>
<pre>---( start )---
stage="p2m1"
# stage can be p2 or p3 = data points
# stage can be d0 or d1 = display
# stage can be m1 or m2 or me = model
var('a,b,c')
if stage.find("p2") >= 0 :
mydata = [[1,2],[3,5]]
else :
mydata = [[1,2],[2,4],[3,5]]
#mydata
mydata_plot = points(mydata,color='purple', size=100)
if stage.find("m1") >= 0 :
mymodel(x) = b*x + c
elif stage.find("m2") >= 0 :
mymodel(x) = a*x^2 + b*x + c
elif stage.find("me") >= 0 :
mymodel(x) = a*exp(b*x) + c
myfit = find_fit(mydata,mymodel,solution_dict=True)
if stage.find("m1") >= 0 :
f(x) = mymodel(b=myfit[b], c=myfit[c])
else :
f(x) = mymodel(a=myfit[a], b=myfit[b], c=myfit[c])
#f(x) = 6 - 2^(3-x)
#f(x) = (-8) * exp( x*(-ln(2))) + 6
mymodel_plot = plot( f, (x,0,4,), color='red')
all_go = mydata_plot
if stage.find("d1") >= 0 :
all_go += mymodel_plot
t_min=0
t_max=5
t_default=1.5
sample_t=t_default
global_go = all_go
all_go
if stage.find("d1") < 0 :
all_go.show()
print("Ok.")
elif stage.find("d1") >= 0 :
@interact
def _(sample_t=slider(t_min, t_max, default=t_default, step_size=0.1)):
global global_go
all_go = global_go
#sample_t=2
output_y = f(sample_t)
point_tuple = ( sample_t, output_y )
plotpoint_go = point2d( point_tuple , size=200, color='green')
all_go += plotpoint_go
all_go.show()
f.show()
print( "f(" + str(sample_t) + ") = " + str(output_y) )
---( end )---
</pre>
https://ask.sagemath.org/question/43638/only-one-of-two-identical-entries-in-the-notebook-gives-graphics-output-for-show/?answer=43646#post-id-43646The Sage notebook is deprecated, so only critical things will be fixed. You shoud use the Jupyter notebook instead. Note that when you run the command `sage -n` you will see a page that allows you to convert automatically your worksheets from the sage notebook format (`.sws`) to the jupyter notebook format (`.ipynb`).Wed, 12 Sep 2018 10:33:47 +0200https://ask.sagemath.org/question/43638/only-one-of-two-identical-entries-in-the-notebook-gives-graphics-output-for-show/?answer=43646#post-id-43646Answer by kcrisman for <p>I use SageMath to teach a bunch of kids. I have to confess a certain amount of frustration with the SageMath notebook. In particular, tonight I composed a notebook entry which shows some data points and curve-fitted plots. I pressed Shift-Enter to evaluate, and there was no graphical output (nor any error message).</p>
<p>And then I copied/pasted the exact same entry into a blank entry in the same SageMath notebook, pressed Shift-Enter, and it showed the graphical output.</p>
<p>Yes, this is the SAME notebook page which, if you scroll up, shows no graphical output (I inserted a print("Ok.") just to confirm that there was SOME output), and if you scroll down, you see the IDENTICAL python-like SageMath script, and then it shows the graphics of the plotted points as desired.</p>
<p>How could this happen? Is there something that happens in between notebook entries? Perhaps only the last entry is evaluated? I will try posting screenshots.</p>
<p>ok, the screen shots are as follows:</p>
<p>https://pasteboard.co/HDowwUc.png (no plot, just text)</p>
<p>https://pasteboard.co/HDoxbZ1.png (plot after the text)</p>
<p>in the first entry, you can see the notebook entry, and then underneath in blue the SageMath output "Ok.", and that's all; the next notebook entry follows immediately. There is no plot.</p>
<p>in the second entry, you can see the notebook entry, and then underneath in blue the SageMath output "Ok.", and under that is the plot of the two data points and the axes.</p>
<p>For what it's worth, the SageMath notebook entry is as follows. The variable "stage" is so that I can easily turn on/off various parts of the entry by modifying it as I get to various parts of the lesson. (But there is no difference between the entry that works and the entry that doesn't.)</p>
<p>"all_go" is the accumulation of all the graphics objects that I want to display, that is, the output of the points() function in this case, and depending on the setting of my "stage" variable, possibly also a plot of f(x).</p>
<p>I am using SageMath 7.5.1 on Kubuntu 14.04. I know it's not the newest version, but I mst be missing something.</p>
<p>Any help would be appreciated</p>
<pre>---( start )---
stage="p2m1"
# stage can be p2 or p3 = data points
# stage can be d0 or d1 = display
# stage can be m1 or m2 or me = model
var('a,b,c')
if stage.find("p2") >= 0 :
mydata = [[1,2],[3,5]]
else :
mydata = [[1,2],[2,4],[3,5]]
#mydata
mydata_plot = points(mydata,color='purple', size=100)
if stage.find("m1") >= 0 :
mymodel(x) = b*x + c
elif stage.find("m2") >= 0 :
mymodel(x) = a*x^2 + b*x + c
elif stage.find("me") >= 0 :
mymodel(x) = a*exp(b*x) + c
myfit = find_fit(mydata,mymodel,solution_dict=True)
if stage.find("m1") >= 0 :
f(x) = mymodel(b=myfit[b], c=myfit[c])
else :
f(x) = mymodel(a=myfit[a], b=myfit[b], c=myfit[c])
#f(x) = 6 - 2^(3-x)
#f(x) = (-8) * exp( x*(-ln(2))) + 6
mymodel_plot = plot( f, (x,0,4,), color='red')
all_go = mydata_plot
if stage.find("d1") >= 0 :
all_go += mymodel_plot
t_min=0
t_max=5
t_default=1.5
sample_t=t_default
global_go = all_go
all_go
if stage.find("d1") < 0 :
all_go.show()
print("Ok.")
elif stage.find("d1") >= 0 :
@interact
def _(sample_t=slider(t_min, t_max, default=t_default, step_size=0.1)):
global global_go
all_go = global_go
#sample_t=2
output_y = f(sample_t)
point_tuple = ( sample_t, output_y )
plotpoint_go = point2d( point_tuple , size=200, color='green')
all_go += plotpoint_go
all_go.show()
f.show()
print( "f(" + str(sample_t) + ") = " + str(output_y) )
---( end )---
</pre>
https://ask.sagemath.org/question/43638/only-one-of-two-identical-entries-in-the-notebook-gives-graphics-output-for-show/?answer=43670#post-id-43670That is a pretty thorny one; I just tested it out and it seems that simply running it a couple times in different cells works, as well as if you remove the last `elif` statement code, and then replace it again (that is, the image then shows up). My best guess is that the image generated is being swallowed somehow at first, but there isn't an obvious place for where I would suspect it would happen. One could analyze this by examining the local folder where the images and code live (often `.sage/sage_notebook.sagenb/home/admin/`) but as tmonteil says, it's probably not worth the effort to track this down at this point. Thank you nonetheless for the report.Fri, 14 Sep 2018 14:16:52 +0200https://ask.sagemath.org/question/43638/only-one-of-two-identical-entries-in-the-notebook-gives-graphics-output-for-show/?answer=43670#post-id-43670