Ask Your Question

Revision history [back]

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:

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="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 )---