interpolation with matplotlib pcolor


I have two numpy arrays, the first one is (30, 365) and contains values for for 30 depths throughout the year, the second array is (30, 1) and contains the actual depth (in meters) corresponding to the depths in the first array. I want to plot the first array so the depths are scaled according to the second array but I also want the data to be interpolated (the first few depths are relatively close together while lower depths are far apart, providing a blocky look to the pcolor image.)

This is what i'm doing:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 365, 1)
X, Y = np.meshgrid(x, depth)    #depth is the (30, 1) array

plt.pcolor(X, -Y, data)         #data is the (30, 365) array

which results in the blocky look, any ideas on how I could get a smoother looking graph?

8/22/2012 9:51:05 PM

Accepted Answer

Are your depths on a regular grid (ie, constant spacing)? If so, you can use imshow and set the range with the extent keyword and aspect='auto'. Otherwise, you have two choices.

You can use pcolormesh instead and use shading='gouraud'. This will help with the sharp colour quantisation, but not as good as interpolation.

The second choice is to interpolate data to a new regular depth grid, so you can use imshow and the different interpolation options. For example, to interpolate only along the depth direction you can use scipy's interpolate.interp1d:

from scipy.interpolate import interp1d

fint = interp1d(depth, data.T, kind='cubic')
newdata = fint(newdepth).T

The .T were added because interpolation has to be on the last index, and depth is the first index of your data. You can replace kind by 'linear' if you prefer.

8/22/2012 11:42:29 PM

no, pcolor doesn't do interpolation. You can try NonUniformImageor even imshow instead. Check out the examples here

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow