I was playing with Sage(math) because I wanted to manage (a bit) the presentation of the plots. Here is what I found out! (most of it is in Sage help)

## Color of Plots.

For changing the color of the plot, use the option *color*,

sage: plot(sin, -3, 3, color='red')

and you’ll get

Nice! but not enough.

One can draw recurrent plots by using a *for* command,

sage: for n in range(1,6): ... plot(sin(n*x), (x, -3, 3), color=hue(n/5.0))

but this code gives 5 different plots, if you want only one try this

sage: p = plot( sin, -3, 3, color=hue(1/5.0)) sage: for n in range(2,6): ... p += plot(sin(n*x), (x, -3, 3), color=hue(n/5.0)) ... sage: p

and the result is

## Adding Text to Plots.

The command for adding text to plots is *text* (very intuitive, Isn’t it?), and the parameters are, the actual text you’d like to insert between single quote marks, the coordinates, and any other characteristic… as color.

Additionally, the text environment accepts mathematical LaTeX inputs by using the dollar signs.

sage: p = plot(sin(x/10.0), -10, 10, color=hue(1/6.0)) + text('$n$=1', (12,1), color=hue(1/6.0)) sage: for n in range(2,6): ... p += plot(sin(n*x/10.0), -10, 10, color=hue(n/6.0)) + text('$n$=%s'%n, (12,1/n), color=hue(n/6.0)) sage: p

and get,

**Note** that the text may include `running` parameters, in the above example the running parameter is , and is called in the text environment as `%s` and later one explain that the value to insert is via %n.

**NOTE 2:** The coordinates of the text might be also changing values.

**NOTE 3:** The *fontsize* and *horizontal_alignment* options are available, for example

sage: text('$E$=%s'%Energy, (5.2, 1.2), horizontal_alignment='right', fontsize=15, color='black')

## Nice Example.

Try this!

reset() def Harm_Osc(n, x): return sqrt(1/(2^n*factorial(n)))/(pi)^(0.25)*e^(-x^2/2)*hermite(n,x) p = plot(x^2/2, (x, -3.2, 3.2), color='black') + text('$x^2/2$', (3.9, 5), color='black', fontsize=13) for n in range(5): p += plot(Harm_Osc(n, x)/2 + n + 0.5, (x, -5, 5), color=hue(n/5.0), fill=n + 0.5) + text('$\Psi_%s$' %n, (5.2, n+0.5), color=hue(n/5.0), fontsize=13) show(p, axes_labels=['$x$','$E$'])

😉

You’ll get this,

This example got colors, text, running text, running color, axes labeling, LaTeX text, fill plot… very complete!

on February 21, 2010 at 5:32 am |omologosThat is a very nice graph of the harmonic oscilator. I’ll copy it if you don’t mind.

on February 21, 2010 at 11:14 am |doxdrumGlad you liked it! I don’t mind if you use it…

on June 29, 2010 at 9:54 am |memereally love the quantum harmonic oscillator! that looks great.

this will be my standard example why sage is the tool to use from now on.

on October 31, 2010 at 7:34 pm |ShrivAwesome example! I am going to download and play with SAGE asap! Thanks..

on January 2, 2011 at 9:45 am |2010 in review « Doxdrum's Blog[…] Playing around with Sage(math) February 20104 comments 5 […]

on November 26, 2013 at 2:44 pm |David HorganHi thanks for showing this quantum simple harmonic oscillator code – beautiful output!