Example Stock Price Charts

Python Stock Chart Lib

Following a convention that stores a short description of the data in the beginning lines of the CSV files can be very useful for annotating a graph or a report associated with the data in the file. To follow along with the example, ensure your directory structure is similar to that presented in Chapter 2 in the section Example Directory Structure for the Book. Your base directory should be Ch4 within Ch4 there should be three subdirectories named src, data, and images. If you wish to use a...

Array Methods and Properties

Loubere Method

Arrays are objects and as such have functions called methods and variables called properties. Using IPython see Chapter 2 , you can list an object's methods and properties by using character completion, accessible via the Tab key. Alternatively, you can issue the following gt gt gt m for m in dir numpy.ndarray if not m.startswith '_' 'T', 'all', 'any', 'argmax', 'argmin', 'argsort', 'astype', 'base', 'byteswap', 'choose', 'clip', 'compress', 'conj', 'conjugate', 'copy', 'ctypes', 'cumprod',...

Piecewise Linear Interpolation

Linear Interpolation

Let's turn back to our half-a-circle example. This time, we'll limit ourselves to a quarter of a circle, that is, positive values of x and y. We start by calculating the y values for x equal to 0, 0.2, . . . , 1. We'll store the results in vectors xp and yp array 0. , 0.2, 0.4, 0.6, 0.8, 1. gt gt gt yp sqrt l-xp 2 We'd like to calculate the values of y for x values equal to 0.1, 0.3, . . . , 0.9 given xp and yp. We'll use the function interp x, xp, yp for this. The function returns the value of...

Example Moving Average

Numpy Smooth List

On many occasions, filtering is used to smooth a signal. A simple algorithm is that of a moving average. For every two consecutive points, we calculate the average and use that value instead. The points are overlapping, so a result of using the algorithm on the vector l, 2, 0, 2 would be 1.5, 1, l . But why stop at two samples Moving average can be performed on several points, returning the average of those points. In Python, you could write gt gt gt from pylab import gt N 512 gt gt gt x l-exp...

Example Automatically Reading Yahoo Financial Data

Matplotlib Yahoo

The following discussion is a bit off-topic, but as it is a direct continuation of the previous example, this is probably a logical spot for it. There's an alternative method to manually saving the charts.xls file from NASDAQ. One such option is using the matplotlib.finance module. The two core functions that fetch the data and parse it are fetch_historical_yahoo and parse_yahoo_historical although you could easily parse the data yourself . Another function of interest is the candlestick...

Example Thumbnail Index Image

Tkinter Examples Python

In a previous example we've created a catalog of images. While that catalog is quite useful, it doesn't show the contents of those images. A more useful catalog perhaps would be a collage of the images annotated with text showing each image's file name see Listing 9-7 . from PIL import Image, ImageDraw def thumbnail_index dirpath Create a thumbnail index from images in dirpath num_images 5 thumb_size 128, 96 cat_size num_images thumb_size o , num_images thumb_size l go through all the pictures...

GPS Location Plot

Matplotlib Gps

Now the moment we've been waiting for, plotting GPS data. To be able to follow along and plot data, be sure to define the functions read_csv_file and process_gps_data as previously detailed and set the file name variable to point to your GPS data file. I've suppressed matplotlib responses so that the code is cleaner to follow. gt gt gt filename 'GPS-2008-05-30-09-00-50.csv' gt gt gt y read_csv_file ' data ' filename gt gt gt lat, long, v, t, sats process_gps_data y gt px long-min long NMI 60.0...

Dict Reader and Dict Writer Objects

The csv module provides us with additional useful objects the DictReader and DictWriter objects, which are similar to the csv.reader and csv.writer objects. If you follow the convention that places a header at the beginning of a CSV file, that is, that each column in the CSV file starts with a field name see Chapter 4 for a discussion of this , accessing values can be done by accessing the dictionary with the field name as key. Let's turn to an example. To follow along, create the file data...