Showing posts with label matlibplot. Show all posts
Showing posts with label matlibplot. Show all posts

Thursday, June 21, 2012

Importing data into Python for a 2D plot

Yay!  I'm learning some Python! (For Family Guy fans this is in Peter's voice when Lois lets him take his cage of parrots on a trip and he says, Yay, you're letting me be myself!).

So I needed to load a small sample of data (about 7 data points) into a 2D graph in Python.  After awhile of searching and trial and error, I finally found a solution.

First, I had to figure out how to get Python to read the data and read it in a proper format or format that I needed for use.  Basically, a 2x2 array for x and y values.

At first I thought plotfile under matplolib under cbook and using fname and get_sample_data would work.

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plotfile

http://matplotlib.sourceforge.net/examples/pylab_examples/plotfile_demo.html

http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/plotfile_demo.py

http://matplotlib.sourceforge.net/api/cbook_api.html?highlight=fname

I also tried the csv package/module/library or whatever the proper term is in Python and its reader function.

http://docs.python.org/release/2.5.2/lib/csv-examples.html

http://www.endlesslycurious.com/2011/05/06/graphing-real-data-with-matplotlib/

http://docs.python.org/library/csv.html#csv.reader

http://docs.python.org/library/csv.html

http://www.doughellmann.com/PyMOTW/csv/

I did get this to display my data in a table
import csv
reader = csv.reader(open("some.csv", "rb"))
for row in reader:
    print row
Here is a screenshot:


However, I found loadtxt and upack=True but ended up using NumPy's genfromtxt which seems like they accomplish essentialyl the same thing, to load my data into a 2x2 matrix or array.

http://bulldog2.redlands.edu/facultyfolder/deweerd/tutorials/Tutorial-ReadingWritingData.pdf

http://www.programmingforbiologists.org/importing-data-python

http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html

CSV stands for Comma Separated Value

http://docs.python.org/library/csv.html
The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. There is no “CSV standard”, so the format is operationally defined by the many applications which read and write it. The lack of a standard means that subtle differences often exist in the data produced and consumed by different applications. These differences can make it annoying to process CSV files from multiple sources. Still, while the delimiters and quoting characters vary, the overall format is similar enough that it is possible to write a single module which can efficiently manipulate such data, hiding the details of reading and writing the data from the programmer. The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel. Programmers can also describe the CSV formats understood by other applications or define their own special-purpose CSV formats.
My code is:

import numpy as np
import matplotlib.pyplot as plt

x, y = np.genfromtxt('1952_Kelsall_ax_vel_ser_I_first_z_loc_closeup_2.csv', delimiter = ',', unpack=True)
y = np.multiply(1.62, y)
y = np.divide(y, 2885)

plt.plot(x, y, 'o')

plt.show()

So I imported my data into x and y using unpack=True to ensure that the data went into its own column. I then did some scaling using NumPy's multiply and divide. Then simple plotted onto a 2D graph using 'o' for data points only.

Screenshot:


http://matplotlib.sourceforge.net/examples/api/unicode_minus.html

http://matplotlib.sourceforge.net/mpl_examples/api/unicode_minus.py

http://docs.scipy.org/doc/numpy/reference/generated/numpy.divide.html

http://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html

http://www.scipy.org/NumPy_for_Matlab_Users


Here is a NumPy tutorial and reference:

Tentative NumPy Tutorialhttp://www.scipy.org/Tentative_NumPy_Tutorial

Numpy Example Listhttp://www.scipy.org/Numpy_Example_List

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

or


from pylab import *


From here I found this nice quote: http://old.nabble.com/pylab-td24910613.html
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:

http://johnstachurski.net/lectures/more_numpy.html

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])
pylab.show()
The same can be achieved by
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.show()
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: http://code.activestate.com/lists/python-tutor/87392/
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: http://code.activestate.com/lists/python-tutor/87394/
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: http://code.activestate.com/lists/python-tutor/87400/
> 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 http://docs.python.org/contents.html which is what I plan on doing.

On to the plot.

The code:

from pylab import *

r=arange(0,1,0.01)

z=arange(0,1,0.01)

sigma=1

l=1

kappa=1/(2*pi*sigma*l)

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

plot(r,u)

ylabel('$ u_r $')

xlabel('$ r $')

title('$ u_r $')

show()

The figure:


Screenshot of Spyder IDE:


Thursday, February 2, 2012

mlab — matplotlib.mlab

mlab — Matplotlib v1.1.0 documentation
Numerical python functions written for compatability with MATLAB commands with the same names.

Tuesday, January 31, 2012

matplotlib: python plotting

http://matplotlib.sourceforge.net/
matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell (ala MATLAB®* or Mathematica®†), web application servers, and six graphical user interface toolkits. matplotlib tries to make easy things easy and hard things possible. You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc, with just a few lines of code. For a sampling, see the screenshots, thumbnail gallery, and examples directory