Code Search for Developers
 
 
  

helpers.py from matplotlib at Krugle


Show helpers.py syntax highlighted

import time, datetime
from matplotlib.numerix import *
from matplotlib.numerix.mlab import *
from matplotlib.dates import date2num


def load_quotes(fname, maxq=None):
    """
    Load quotes from the representative data files vineet sent If any
    of the values are missing I force all missing.  Quotes are sorted
    in increasing time.  Return value is a list of tuples

      (epoch, open, high, low, close, volume )
    """
    quotes = []
    
    for i, line in enumerate(file(fname)):
        if maxq is not None and i>maxq: break
        ts,o,h,l,c,v = line.split(',')

        dt = datetime.datetime(*time.strptime(ts.strip('"'), '%Y%m%d%H%M%S')[:6]) # convert to float days
        d = date2num(dt)
        o,h,l,c,v = [float(val) for val in o,h,l,c,v]

        if o==-1 or h==-1 or l==-1 or c==-1 or v==-1:
            o,h,l,c,v = -1, -1, -1, -1,-1
        quotes.append((d,o,h,l,c,v))

    quotes.sort()  # increasing time
    return quotes


def ema(s, n):
    """
    returns an n period exponential moving average for
    the time series s

    s is a list ordered from oldest (index 0) to most recent (index
    -1) n is an integer

    returns a numeric array of the exponential moving average
    """
    s = array(s)
    ema = []
    j = 1
    #get n sma first and calculate the next n period ema
    sma = sum(s[:n]) / n
    multiplier = 2 / float(1 + n)
    ema.append(sma)
    #EMA(current) = ( (Price(current) - EMA(prev) ) xMultiplier) + EMA(prev)
    ema.append(( (s[n] - sma) * multiplier) + sma)
    #now calculate the rest of the values
    for i in s[n+1:]:
        tmp = ( (i - ema[j]) * multiplier) + ema[j]
        j = j + 1
        ema.append(tmp)
    return ema

def movavg(s, n):
    """
    returns an n period moving average for the time series s
       
    s is a list ordered from oldest (index 0) to most recent (index -1)
    n is an integer

        returns a numeric array of the moving average

    See also ema in this module for the exponential moving average.
    """
    s = array(s)
    c = cumsum(s)
    return (c[n-1:] - c[:-n+1]) / float(n-1)

def fill_over(ax, x, y, val, color, over=True):
    """
    Plot filled x,y for all y over val
    if over = False, fill all areas < val
    """
    ybase = asarray(y)-val
    crossings = nonzero(less(ybase[:-1] * ybase[1:],0))
    
    if ybase[0]>=0: fillon = over
    else:           fillon = not over


    indLast = 0
    for ind in crossings:        
        if fillon:
            thisX = x[indLast:ind+1]
            thisY = y[indLast:ind+1]
            thisY[0] = val
            thisY[-1] = val
            ax.fill(thisX, thisY, facecolor=color)
        fillon = not fillon
        indLast = ind

    
def random_signal(N, tau):
    'generate a length N  random signal with time constant tau'
    t = arange(float(N))
    filter = exp(-t/tau)
    return convolve( randn(N), filter, mode=2)[:len(t)]





See more files for this project here

matplotlib

Matplotlib is a pure python plotting library with the goal of making\r\npublication quality plots using a syntax familiar to matlab users. \r\nThe library uses Numeric for handling large\r\ndata sets and supports a variety of output backends

Project homepage: http://sourceforge.net/projects/matplotlib
Programming language(s): C,C++,Python
License: other

  data/
    500hgtdata.gz
    500hgtlats.gz
    500hgtlons.gz
    chandra.dat
    eeg.dat
    etopo20data.gz
    etopo20lats.gz
    etopo20lons.gz
    hst.zdat
    intc.csv
    msft.csv
    s1045.ima
  __init__.py
  align_text.py
  align_text_large.png
  align_text_small.png
  alignment_test.py
  axes_demo.py
  axes_demo_large.png
  axes_demo_small.png
  barchart_demo.py
  barchart_demo_large.png
  barchart_demo_small.png
  colours.py
  contour_shot.py
  contour_small.png
  data_helper.py
  date_demo.py
  date_demo_large.png
  date_demo_small.png
  eeg.py
  eeg_large.png
  eeg_small.png
  etopo20data.gz
  etopo20lats.gz
  etopo20lons.gz
  fill_demo.py
  fill_demo_large.png
  fill_demo_small.png
  finance_work2.py
  finance_work2_large.png
  finance_work2_small.png
  helpers.py
  histogram_demo.py
  histogram_demo_large.png
  histogram_demo_small.png
  hstdemo.py
  layer_images.py
  layer_images_large.png
  layer_images_small.png
  legend_demo.py
  legend_demo_large.png
  legend_demo_small.png
  log_shot.png
  log_shot.py
  log_shot_large.png
  log_shot_small.png
  logo.png
  makeshots.py
  mathtext_demo.py
  mathtext_demo_large.png
  mathtext_demo_small.png
  matplotlibrc
  mri_demo.py
  mri_with_eeg.py
  mri_with_eeg_large.png
  mri_with_eeg_small.png
  msft_nasdaq_d.csv
  pcolor_demo.py
  pcolor_demo_large.png
  pcolor_demo_small.png
  pfm-lsm.png
  pie_demo.py
  pie_demo_large.png
  pie_demo_small.png
  plotmap.py
  plotmap_large.png
  plotmap_small.png
  polar_demo.py
  polar_demo_large.png
  polar_demo_small.png
  proj.py
  scatter_demo2.py
  scatter_demo2_large.png
  scatter_demo2_small.png
  simple_plot.py
  simple_plot_large.png
  simple_plot_small.png
  slider_demo.py
  slider_demo_large.png
  slider_demo_small.png
  subplot_demo.py
  subplot_demo_large.png
  subplot_demo_small.png
  table_demo.py
  table_demo_large.png
  table_demo_small.png
  tex_demo.py
  tex_demo_large.png
  tex_demo_small.png
  text_themes.py
  text_themes_large.png
  text_themes_small.png
  wheeler_demo.py
  wheeler_demo_large.png
  wheeler_demo_small.png