Posts tagged ‘matplotlib’

Matplotlib: Graph with vertical lines ascending to points

This is different from a bar chart, since in a bar chart the bars have width.

The vlines function will do what you want this takes arrays for its various values.

Example

import pylab

pylab.vlines([1,2,3,4,5], 0, [2,4,6,8,10])
pylab.show()
Advertisements

March 27, 2011 at 11:09 pm Leave a comment

Python numpy moving average for data

The following examples produces a moving average of the preceding WINDOW values. We truncate the first (WINDOW -1) values since we can’t find the average before them. (The default behaviour for convolution is to assume that values before the start of our sequence are 0). (More formally, we construct the sequence y for the sequence x where y_i = (x_i + x_(i+1) + …. x_(i+n)) / n)

WINDOW = 10
data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]
weightings = numpy.repeat(1.0, WINDOW) / WINDOW
numpy.convolve(data, weightings)[WINDOW-1:-(WINDOW-1)]

This makes use of numpy’s convolution function. This is a general purpose moving average operation.

Changing weightings makes some values more important; offsetting appropriately allows you to view average as around point rather than before point.

Rather than truncating values we can fix the initial values in place, as illustrated in this example:

WINDOW = 10
data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]
extended_data = numpy.hstack([[data[0]] * (WINDOW- 1), data])
weightings = numpy.repeat(1.0, WINDOW) / WINDOW
numpy.convolve(extended_data, weightings)[WINDOW-1:-(WINDOW-1)]

February 24, 2011 at 11:58 pm 5 comments

Setting the minimum values for axes in matplotlib

pylab.axis(xmin=0, ymin=0)

February 24, 2011 at 11:28 pm Leave a comment

Drawing lines with matplotlib

Matplotlib’s pyplot library has the following functions for drawing lines:

axvline – A full vertical line (does not start or stop)
vlines – A vertical line segment (or multiple)
axhline – A full horizontal line
hlines – …

The relevant plot in the gallery is here:

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

March 25, 2010 at 3:55 pm Leave a comment


October 2017
M T W T F S S
« Nov    
 1
2345678
9101112131415
16171819202122
23242526272829
3031