Ask Your Question

Only one of two identical entries in the notebook gives graphics output for show()

asked 2018-09-11 08:14:09 -0600

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: (no plot, just text) (plot after the text)

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

---( start )--- 

# stage can be p2 or p3 = data points
# stage can be d0 or d1 = display
# stage can be m1 or m2 or me = model

if stage.find("p2") >= 0 :
    mydata = [[1,2],[3,5]]
else :
    mydata = [[1,2],[2,4],[3,5]]
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


global_go = all_go

if stage.find("d1") < 0 :
elif stage.find("d1") >= 0 :
    def _(sample_t=slider(t_min, t_max, default=t_default, step_size=0.1)):
        global global_go
        all_go = global_go

        output_y = f(sample_t)
        point_tuple = ( sample_t, output_y )
        plotpoint_go = point2d( point_tuple , size=200, color='green')
        all_go += plotpoint_go
        print( "f(" + str(sample_t) + ") = " + str(output_y) )

---( end )--- 
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2018-09-14 07:16:52 -0600

kcrisman gravatar image

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

edit flag offensive delete link more

answered 2018-09-12 03:33:47 -0600

tmonteil gravatar image

updated 2018-09-13 00:30:32 -0600

The 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).

edit flag offensive delete link more

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


Asked: 2018-09-11 08:14:09 -0600

Seen: 67 times

Last updated: Sep 14