Wednesday, April 18, 2012

Python - 2d plot - matplotlib

Woohoo!  I did my first plot in Python!!  I am trying out Python because it seems to offer many utilities I can use in one place for various scientific research capabilities.  In Ubuntu 11.10 I have installed the Spyder IDE and a few libraries such as scipy, numpy, matplotlib, and mayavi.  Eventually, I want to plot 3-D streamlines which is where mayavi comes into to play, thus I need to learn Python.  Plus, Sage can also use Python which I plan to experiment with later.

I am still very green to Python although I do have some programming experience (mainly MATLAB and a C class I took about 9 years ago, :P!!).  For example, I've seen a few ways how to load these libraries:

import numpy
import pylab


from pylab import *

From here I found this nice quote:
Numpy is the common core, providing N-dimensional arrays and math; matplotlib is a plotting library, using numpy; scipy is a collection of math/science functionality, also using numpy.
Here is a decent (decent as in I still don't understand fully) explanation of the different import/package/library options:

A quick aside on the relationship between Pylab and Matplotlib
One way to do plots with Matplotlib is like this
import pylab
pylab.plot([1, 2, 3])
The same can be achieved by
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
What is the difference?
We can see the difference in the pylab initialization file:
## some stuff
from numpy import *
from numpy.fft import *
from numpy.random import *
from numpy.linalg import *
## some more stuff
from matplotlib.pyplot import *
## some more stuff
Thus, import pylab brings in
  • everything from the NumPy namespace
  • everything from various NumPy submodules (randomlinalg, etc.)
  • everything from matplotlib.pyplot
The plotting functions are in matplotlib.pyplot

Another explanation I found:
what is the basic difference between the commands
import pylab as * 
import matplotlib.pyplot as plt 
import numpy as np import numpy as *
One response:
import pylab as * pollutes your global namespace with all kinds of symbols. If you don't know them all, you might accidentally use one of them in your own code, and wonder why things aren't working the way you expected. Better is import pylab and then use pylab.something to access a symbol from pylab Some prefer import pylab as pab (or something) and then use pab.something to save some typing. import matplotlib.pyplot as plt looks in the matplotlib *package" for the module pyplot, then imports it with a shortcut name of plt
Another response:
> what is the basic difference between the commands > import pylab as * Are you sure you don't mean from pylab import * ??? The other form won't work because * is not a valid name in Python. You should ghet a syntax error. > import matplotlib.pyplot as plt This is just an abbreviation to save typing matplotlib.pyplot in front of every reference to the module names. > import numpy as np as above > import numpy as * again an error.

Anyways, I would recommend reading on the structure of python here which is what I plan on doing.

On to the plot.

The code:

from pylab import *






u=-(kappa/r)*sin(pi*pow(r, 2))


ylabel('$ u_r $')

xlabel('$ r $')

title('$ u_r $')


The figure:

Screenshot of Spyder IDE:

No comments:

Post a Comment