Difference between revisions of "Pylab"

From Rizzo_Lab
Jump to: navigation, search
 
 
Line 1: Line 1:
Plotting a histogram
+
This is combination of python, numpy and matplotlib which supposedly can do everything matlab can. We are still investigating this package to see if we can use directly for analysis scripts. This way be will not have to write python scripts that produce csv files for analysis in matlab.
 +
 
 +
==Bar charts==
 +
import numpy.numarray as na
 +
from pylab import *
 +
 +
labels = ["Baseline", "System"]
 +
data =  [3.75              , 4.75]
 +
error =  [0.3497            , 0.3108]
 +
 +
xlocations = na.array(range(len(data)))+0.5
 +
width = 0.5
 +
bar(xlocations, data, yerr=error, width=width)
 +
yticks(range(0, 8))
 +
xticks(xlocations+ width/2, labels)
 +
xlim(0, xlocations[-1]+width*2)
 +
title("Average Ratings on the Training Set")
 +
gca().get_xaxis().tick_bottom()
 +
gca().get_yaxis().tick_left()
 +
show()
 +
 
 +
==Plotting a histogram==
 
  from matplotlib.figure import Figure
 
  from matplotlib.figure import Figure
 
  from matplotlib.backends.backend_agg import FigureCanvasAgg
 
  from matplotlib.backends.backend_agg import FigureCanvasAgg
Line 12: Line 33:
 
  canvas = FigureCanvasAgg(fig)
 
  canvas = FigureCanvasAgg(fig)
 
  canvas.print_figure("hist.png", dpi=300)
 
  canvas.print_figure("hist.png", dpi=300)
 +
 +
==Resources==
 +
*[http://www.scipy.org/Cookbook/Matplotlib/ Matplotlib Cookbook]
 +
*[http://matplotlib.sourceforge.net/ Matplotlib webpage]

Latest revision as of 14:03, 12 February 2010

This is combination of python, numpy and matplotlib which supposedly can do everything matlab can. We are still investigating this package to see if we can use directly for analysis scripts. This way be will not have to write python scripts that produce csv files for analysis in matlab.

Bar charts

import numpy.numarray as na
from pylab import *

labels = ["Baseline", "System"]
data =   [3.75               , 4.75]
error =  [0.3497             , 0.3108]

xlocations = na.array(range(len(data)))+0.5
width = 0.5
bar(xlocations, data, yerr=error, width=width)
yticks(range(0, 8))
xticks(xlocations+ width/2, labels)
xlim(0, xlocations[-1]+width*2)
title("Average Ratings on the Training Set")
gca().get_xaxis().tick_bottom()
gca().get_yaxis().tick_left()
show()

Plotting a histogram

from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg
from pylab import randn 

x = randn(10000)
fig = Figure(figsize=(10,10))
ax = fig.add_subplot(111)
ax.hist(x,100)
ax.set_xlabel("Random Numbers")
ax.set_ylabel("Population")
canvas = FigureCanvasAgg(fig)
canvas.print_figure("hist.png", dpi=300)

Resources