1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| @interact
def interactive_2d_plotter(clr=Color('green'), expression=input_box('x^2', 'Expression', str), x_range=range_slider(-6,6,1,(-4,4), label='X Range'), order=slider([0..100],None,None,3), square=checkbox(True, 'Square'), axes=checkbox(False, 'Show Axes')):
.. if expression:
.. try:
.. expression = SR(expression) # turn string into a Sage expression
.. except TypeError:
.. print error_msg('This is not an expression.')
.. return
.. try:
.. xmin, xmax = x_range
.. n = order
.. if square or not axes:
.. print "var('%s')nplot(%s).show(%s%s%s)" % (expression.variables()[0], repr(expression), 'aspect_ratio=1' if square else '', ', ' if square and not axes else '', 'axes=False' if not axes else '')
.. if square:
.. P = plot(FS(expression, n, xmin, xmax), xmin, xmax, color=clr) + plot(expression, xmin, xmax)
.. P.show(aspect_ratio=1, axes=axes)
.. else:
.. P = plot(FS(expression, n, xmin, xmax), xmin, xmax, color=clr) + plot(expression, xmin, xmax)
.. P.show(axes=axes)
.. else:
.. print "var('%s')nplot(%s)" % (expression.variables()[0], repr(expression))
.. P = plot(FS(expression, n, xmin, xmax), xmin, xmax, color=clr) + plot(expression, xmin, xmax)
.. P.show(axes=axes)
.. except ValueError:
.. print error_msg('This expression has more than one variable.')
.. return
.. except TypeError:
.. print error_msg("This expression contains an unknown function.")
.. return |