Python Sage and Matplotlib

The Python computer algebra package Sage and its built-in Matplolib are useful shortcuts to the development of integrated solution to teaching and grading numerous calculus exercises and concepts. This article summarizes major differences of plotting via Sage graphics vs. using Matplotlib directly. Even though I am not experienced with Python, my initial exposure to Python convinced me that the language/library quality is superior to php in terms of plotting support, math libraries, and data types.

1. Sage has its own built-in graphics. Axes created by Sage are more in line with textbook mathematical presentation with the default x and y-axis. Matplotlib default setup for axes looks more chart-like. Matplotlib’s plotting area and axis area can have different background colors.
2. Python uses indentation for conditional statements, continuation, etc. There is no need to have and ending semicolon at the end of the statement. Data types include lists, tuples, iterations. Iteration loops are much cleaner than C and php.
3. After Sage graphics is created, Matplotlib can be called directly to access options not provided by Sage (minor tick mark control, location and format of grid lines, etc).
4. Sage mathematical functions include gcf, lcm, fraction, f(x), derivative, matrix calculations, etc. This is a superior and much more flexible substitute for hand-held graphic calculator (TI-CAS and HP). Sage notebook and command line works on Android tablet and phone devices.
5. Definition and log files can be loaded or attached in addition to log and session information (see dictionary.sage).

The following graph tests the following options provided by Sage/Matplotlib.

6. Name of properties or functions can be found by typing part of the initial characters and .
7. Sage’s image file format does not support .jpg (only .png and .pdf) while Matplotlib offers .jpg support through PIL Python Image Library.
8. latex(f(x)) to convert standard string to LaTex format (can use $string$ to do the conversion).
9. Power of Sage:
Find the roots of quadratic equation:
sage: s=solve(3*x^2+4*x-11==0,x)
sage: s
[x == -1/3*sqrt(37) – 2/3, x == 1/3*sqrt(37) – 2/3]
Check for the number of roots (can set up for real or imaginary).
sage: len(s)
2
sage: s[0]
x == -1/3*sqrt(37) – 2/3
sage: s[1]
x == 1/3*sqrt(37) – 2/3
sage: s[0].right()
-1/3*sqrt(37) – 2/3
Return in 5 digits of 32-bit precision
sage: s[0].right().n(digits=5)
-2.6943
sage: s[0].right().n(prec=32)
-2.69425418