Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, 28 April 2009

Creating simple plots with Python

Making plots with Python is easy provided you've downloaded the matplotlib plotting library. You will also probably need Numpy for the numerical routines it provides.

The following code example will produce two interactive figures. The first one demonstrates how to overplot curves, the second one shows how to get two separate plots on the figure.

#First, import the libraries as plt and np
import matplotlib.pyplot as plt
import numpy as np

#Define a function
def f(t):
return np.exp(-t)*np.cos(2*np.pi*t)

# Set up two arrays
t = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)

#make single plot with overplots
plt.plot(t, f(t), 'g^', t, f(t*t), 'r--', t, f(t*t*t), 'bs')
plt.plot(t2, f(t2), 'g', t2, f(t2*t2), 'r', t2, f(t2*t2*t2), 'b')

# add labels
plt.ylabel('y values')
plt.xlabel('x values')

# Example of how to get math characters.
# Not that the format is the same as TeX markup but you don't need to have
# TeX installed since matplotlib has it's own parser, layout engine and fonts.
# For example, to print the greek letter sigma as the x title, use:
# plt.xlabel(r'$\sigma$')
plt.title(r'$\Delta\chi^2$')

# add some text on the plot at location 2, 0.6
plt.text(2, 0.6, r'$\alpha_i=100,\ \Delta\chi^2=15$')

# use gridmarks
plt.grid(True)

#make second plot with 2 subplots
plt.figure(2)

#first subplot
plt.subplot(211)
plt.plot(t, f(t), 'bo', t2, f(t2), 'k')
plt.ylabel('y values')
plt.xlabel('x values')
plt.title('blah')
plt.grid(True)

#second subplot
plt.subplot(212)
plt.plot(t2,np.cos(2*np.pi*t2), 'r--')
plt.ylabel('y2 Values')
plt.xlabel('x2 values')
plt.grid(True)

# realise plot
plt.show()

This snippet will produce the following figures:


Friday, 24 April 2009

Python vs IDL. The intricacies of the "where" statement.

Selecting specific elements from arrays by means of their index is a quite useful tool when you're manipulating huge data files. In the past I had been using mainly IDL (great but very expensive licensed software) and standard C-shell scripting to do most of the processing but I have recently started to experiment with Python v2.5 (open-source ftw!) and specifically the Enthought and Python(x,y) distributions which, among other things, contain the Matplotlib and SciPy libraries.

Here's how I used to do it in IDL:
First set up a test array called data
IDL>data = findgen(10)
This statement will create an integer array with 10 elements, from 0 to 9.
To select a part of the elements then use:
IDL> data_sub = data(where(data lt 8 and data gt 3))
data_sub now contains the elements 4,5,6,7

In Python we can do something similar using Numpy.
At the Python prompt:
>>> import numpy as np
Set up the test data array as previously. In Python we can do that with:
>>> data = np.arange(0,10,1) # from 0 to 9 incrementing by 1

Now define the limits
>>> lim1 = data > 3
>>> lim2 = data <>>> data_sub = data[lim1 & lim2]
data_sub
is now an array with the values 4,5,6,7

It is possible to easily replace specific elements with zero values:
>>> data_zeros = np.where(data > 5, 0, data)
will replace all array elements with a value greater than 5 with 0.
data_zeros
is then this array: 0,1,2,3,4,5,0,0,0,0