Sage Interact Quickstart

This Sage worksheet was developed for the MAA PREP Workshop "Sage: Using Open-Source Mathematics Software with Undergraduates" (funding provided by NSF DUE 0817071).

Invaluable resources are the Sage wiki http://wiki.sagemath.org/interact (type "sage interact" into Google) and the interact documentation.

 

{{{id=27| plot(x^2,(x,-3,3)) /// }}} {{{id=28| f=x^2 plot(f,(x,-3,3)) /// }}}

Note the "show" that is needed.

{{{id=29| def myplot(f=x^2): show(plot(f,(x,-3,3))) /// }}} {{{id=30| myplot() /// }}} {{{id=32| myplot(x^3) /// }}} {{{id=35| @interact def myplot(f=x^2): show(plot(f,(x,-3,3))) /// }}} {{{id=15| @interact def _(f=x^2): show(plot(f,(x,-3,3))) /// }}} {{{id=21| @interact def _(f=input_box(x^2,width=20)): show(plot(x^2,(x,-3,3))) /// }}} {{{id=19| @interact def _(f=input_box(x^2,width=20), color=color_selector(widget='colorpicker')): show(plot(x^2,(x,-3,3), color=color)) /// }}} {{{id=37| /// }}} {{{id=38| /// }}} {{{id=18| @interact def _(f=input_box(x^2,width=20), color=color_selector(widget='colorpicker', label=""), axes=True, fill=True, zoom=range_slider(-3,3,default=(-3,3))): show(plot(x^2,(x,zoom[0], zoom[1]), color=color, axes=axes,fill=fill)) /// }}} {{{id=24| @interact(layout=dict(top=[['f', 'color']], right=[['axes'],['fill']], bottom=[['zoom']])) def _(f=input_box(x^2,width=20), color=color_selector(widget='colorpicker', label=""), axes=True, fill=True, zoom=range_slider(-3,3, default=(-3,3))): show(plot(x^2,(x,zoom[0], zoom[1]), color=color, axes=axes,fill=fill)) /// }}}

Control Types

Sage has:

We illustrate some of these.

{{{id=9| @interact def _(frame=checkbox(True, label='Use frame')): show(plot(sin(x), (x,-5,5)), frame=frame) /// }}} {{{id=10| var('x,y') colormaps=sage.plot.colors.colormaps.keys() @interact def _(cmap=selector(colormaps)): contour_plot(x^2-y^2,(x,-2,2),(y,-2,2),cmap=cmap).show() /// }}} {{{id=13| var('x,y') colormaps=sage.plot.colors.colormaps.keys() @interact def _(cmap=selector(['RdBu', 'jet', 'gray','gray_r'],buttons=True), type=['density','contour']): if type=='contour': contour_plot(x^2-y^2,(x,-2,2),(y,-2,2),cmap=cmap, aspect_ratio=1).show() else: density_plot(x^2-y^2,(x,-2,2),(y,-2,2),cmap=cmap, frame=True,axes=False,aspect_ratio=1).show() /// }}} {{{id=33| @interact def _(n=(1,20)): print factorial(n) /// }}} {{{id=42| @interact def _(n=slider(1,20,step_size=1)): print factorial(n) /// }}} {{{id=40| @interact def _(n=slider([1..20])): print factorial(n) /// }}} {{{id=41| /// }}}