View Single Post
Posts: 1,269 | Thanked: 3,961 times | Joined on May 2011 @ Brazil
#30
The 1st post cites the new plotting module of SymPy 0.7.2, which depends on MatPlotLib and is available on Maemo 5 and MeeGo Harmattan.

Documentation and examples for SymPy 0.7.2 plotting module :
- plotting module documentation;
- 6 examples/tutorials in IPython notebook format in the "examples/beginner" github folder, with names starting with "plot_".

Here are some examples of using the new "sympy.plotting" module, repeating some plots from post #11 (where the other option to make plots in SymPy, "Plot()"/PyGlet, was used).

Observations about MatPlotLib on :
- Maemo 5, there is the warning message :
"/usr/lib/python2.5/site-packages/matplotlib/font_manager.py:1242: UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to Bitstream Vera Sans"
and some symbols ('-' in axis numbers, etc) are not shown.
- Maemo 5 and MeeGo Harmattan, plotting commands without the option "show=False", or using the function "show()", opens the interactive backend of MatPlotLib on your N900;
- Maemo 5 has MatPlotLib v1.0.0 which is not 100% compatible with sympy.plotting 3D graphics, so "plot3d_parametric_line", "surface_color" option for 3D plots, don't work;
- MeeGo Harmattan, use MatPlotLib 1.2.0 so all the features cited here will work.

The 1st time is measured (with "%time") after loading "isympy", the 2nd time is for repeating plottings. Time is measured on Nokia N9 @ 1 GHz (with MatPlotLib 1.2.0 & NumPy 1.7.0) and Nokia N900 @ 600 Mhz (with MatPlotLib 1.0.0 & NumPy 1.4.0).

2D Plot of a function of 1 variable, y = f(x), a dumped harmonic oscillator :
In [1]: oscillator_plot = plot(2.5*exp(-1.0*x/2)*cos(pi*x), (x,0,10), title='Dumped harmonic oscillator', legend=True, xlabel='x', show=False)
In [2]: oscillator_plot.save("oscillator.png")
Timing : Nokia N9 (2.9s/2.6s), N900 (6.1s/4.0s).


2D parametric function of 1 variable, x = f(t), y = g(t), here an explosion. The "line_color" option can be a constant or a function of parameter (here the radius). The sampling is changed from the default adaptive to 1600 points.
In [1]: from sympy.plotting import plot_parametric
In [2]: explosion = plot_parametric(t*sin(8*t)*cos(8*t)*cos(t), t*sin(8*t)*cos(8*t)*sin(t), (t,0,4*pi), title='Parametric Explosion', xlabel='x', ylabel='y', show=False)
In [3]: explosion[0].line_color = lambda x, y : sqrt(x**2 + y**2)
In [4]: explosion[0].adaptive = False
In [5]: explosion[0].nb_of_points = 1600
In [6]: explosion.save("explosion.png")
Timing : Nokia N9 (5.1s/4.0s), N900 (16s/9.8s).


2D parametric plot of 1 variable (t), where x = r*cos(t), y = r*sin(t), with r the polar coordinate. Here r = cos(4*t), i.e., a polar rose with 8 petals :
In [1]: from sympy.plotting import plot_parametric
In [2]: polar_rose = plot_parametric(cos(4*t)*cos(t), cos(4*t)*sin(t), (t,0,2*pi), title='Polar Rose', show=False)
In [3]: polar_rose[0].line_color = lambda t : cos(t)
In [4]: polar_rose.save("polar-rose.png")
Timing : Nokia N9 (5.6s/2.7s), N900 (11.5s/4.8s).


3D parametric curve of 1 variable, x = f(u), y = g(u), z = h(u), here a 3D wire. We changed the line color to follow the parameter and the sampling from 300 default points to 800 points.
"plot3d_parametric_line" needs MatPlotLib >= 1.1, e.g. MatPlotLib 1.2.0, so only available on Nokia N9/N950.
In [1]: from sympy.plotting import plot3d_parametric_line
In [2]: wire3D = plot3d_parametric_line((2+cos(16*t))*cos(t), (2+cos(16*t))*sin(t), sin(16*t), (t,0,2*pi), title='Wire 3D curve', xlabel='x', ylabel='y', show=False)
In [3]: wire3D[0].line_color = lambda t : t
In [4]: wire3D[0].nb_of_points = 800
In [5]: wire3D.save("wire3D.png")
Timing : Nokia N9 (7s/6s).


3D surface in cartesian coordinates of 2 variables, z = f(x,y), it is by default z colored and has 50 x 50 domain points :
In [1]: from sympy.plotting import plot3d
In [2]: surface3D = plot3d(5*x*y*exp(-x**2-y**2), (x,-2,2), (y,-2,2), title='Surface z colored', xlabel='x', ylabel='y', show=False)
In [3]: surface3D.save("surface3D.png")
Timing : Nokia N9 (11s/10s), N900 (57s/57s).

The "surface_color" option (needs MatPlotLib 1.2.0, available only on Nokia N9/N950) is here a function of the radius and the domain is changed to 100 x 100 points :
In [1]: from sympy.plotting import plot3d
In [2]: surface3D = plot3d(5*x*y*exp(-x**2-y**2), (x,-2,2), (y,-2,2), title='Surface z colored', xlabel='x', ylabel='y', show=False)
In [3]: surface3D[0].surface_color = lambda x, y : sqrt(x**2 + y**2)
In [4]: surface3D[0].nb_of_points_x = 100; surface3D[0].nb_of_points_y = 100
In [5]: surface3D.save("surface3D-2.png")
Timing : Nokia N9 (40s/37s).


3D parametric surface of 2 variables, x = f(u,v), y = g(u,v), z = h(u,v), here a moebius strip, it is by default z colored and has 50 x 50 domain points :
In [1]: from sympy.plotting import plot3d_parametric_surface
In [2]: u, v = symbols('u v')
In [3]: moebius_strip = plot3d_parametric_surface((2+(v/2)*cos(u/2))*cos(u), (2+(v/2)*cos(u/2))*sin(u), (v/2)*sin(u/2), (u,0,2*pi), (v,-1,1), title='Moebius strip', xlabel='x', ylabel='y', show=False)
In [4]: moebius_strip.save("moebius-strip.png")
Timing : Nokia N9 (11s/10s), N900 (95s/95s).

The "surface_color" option (needs MatPlotLib 1.2.0, available only on Nokia N9/N950) is here a function of "u", and the domain is changed to 100 x 20 points :
In [1]: from sympy.plotting import plot3d_parametric_surface
In [2]: u, v = symbols('u v')
In [3]: moebius_strip = plot3d_parametric_surface((2+(v/2)*cos(u/2))*cos(u), (2+(v/2)*cos(u/2))*sin(u), (v/2)*sin(u/2), (u,0,2*pi), (v,-1,1), title='Moebius strip', xlabel='x', ylabel='y', show=False)
In [4]: moebius_strip[0].surface_color = lambda u : cos(u)
In [5]: moebius_strip[0].nb_of_points_u = 100; moebius_strip[0].nb_of_points_v = 20
In [6]: moebius_strip.save("moebius-strip-2.png")
Timing : Nokia N9 (10s/8s).
__________________
Python, C/C++, Qt and CAS developer. For Maemo/MeeGo/Sailfish :
Integral, Derivative, Limit - calculating mathematical integrals, derivatives and limits. SymPy - Computer Algebra System.
MatPlotLib - 2D & 3D plots in Python. IPython - Python interactive shell.
-- My blog about mobile & scientific computing ---
Sailfish : Sony Xperia X, Gemini, Jolla, Jolla C, Jolla Tablet, Nexus 4. Nokia N9, N900, N810.

Last edited by rcolistete; 2013-02-14 at 03:38. Reason: New plots and timings using MatPlotLib 1.2.0
 

The Following 3 Users Say Thank You to rcolistete For This Useful Post: