Matplotlib and Numpy Math

Question:

I’m trying get some traction with Matplotlib and Numpy but it is not very easy.

I’m doing a mini project to start dealing with Matplotlib and Numpy but I’m stuck…

Here is the code:

# Modules
import datetime
import numpy as np
import matplotlib.finance as finance
import matplotlib.mlab as mlab
import matplotlib.pyplot as plot

# Define quote
startdate = datetime.date(2010,10,1)
today = enddate = datetime.date.today()
ticker = 'uso'

# Catch CSV
fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)

# From CSV to REACARRAY
r = mlab.csv2rec(fh); fh.close()
# Order by Desc
r.sort()


### Methods Begin
def moving_average(x, n, type='simple'):
    """
    compute an n period moving average.

    type is 'simple' | 'exponential'

    """
    x = np.asarray(x)
    if type=='simple':
        weights = np.ones(n)
    else:
        weights = np.exp(np.linspace(-1., 0., n))

    weights /= weights.sum()


    a =  np.convolve(x, weights, mode='full')[:len(x)]
    a[:n] = a[n]
    return a
### Methods End


prices = r.adj_close
dates = r.date
ma20 = moving_average(prices, 20, type='simple')
ma50 = moving_average(prices, 50, type='simple')

# Get when ma20 crosses ma50
equal = np.round(ma20,1)==np.round(ma50,1)
dates_cross  = (dates[equal])
prices_cross = (prices[equal])

# Get when ma20 > ma50
ma20_greater_than_ma50 = np.round(ma20,1) > np.round(ma50,1)
dates_ma20_greater_than_ma50  = (dates[ma20_greater_than_ma50])
prices_ma20_greater_than_ma50 = (prices[ma20_greater_than_ma50])

print dates_ma20_greater_than_ma50
print prices_ma20_greater_than_ma50

Now I need to do something like this:

store the price of the "price_cross"
see if one day after the "ma20_greater_than_ma50" statment is true, if true store the price as "price of the one day after"
now do "next price_cross" - "price of the one day after"  (price2 - price1) for all occurences

How can I do this math and more important. How can I get traction with Matplotlib and Numpy. What books should I buy?

Give me some clues.

Best Regards,

Asked By: André

||

Answers:

I would say that you don’t necessarily need to go out and purchase any books. The better (and cheaper) solution is to take a look at online tutorials like:

Link

http://matplotlib.sourceforge.net/examples/index.html

and try to piece together things from the documentation and searching pertinent keywords. From the code you’ve presented (assuming you wrote it), you have some grasp of numpy. You will need to be a bit more specific with the problems you’re encountering to get more specific/detailed help.

Answered By: JoshAdel

Here’s a list to start with, you probably find the parts that are most important for you after browsing through them:

  1. Python tutorial http://docs.python.org/tutorial/
  2. Numpy user guide from http://docs.scipy.org/doc/
  3. Matplotlib user guide http://matplotlib.sourceforge.net/users/index.html
  4. Numpy/Scipy additional documentation sources http://www.scipy.org/Additional_Documentation

You may want to subscribe to the mailling lists for numpy and/or matplotlib.

Answered By: Bernhard

I agree with Josh, but wanted to add the matplotlib gallery:

http://matplotlib.sourceforge.net/gallery.html

Most of my plots start off directly copying something close to what I want, and then modifying it to fit my needs. The matplotlib gallery has many such examples.

Answered By: matt

matplotlib and numpy have a huge list of useful functions, you should always google first before implementing.

for example see matplotlib movavg function.

Answered By: Andrea Zonca