Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 214
pter Avatar asked Aug 22 '12 21:08

pter


2 Answers

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.

like image 173
tiago Avatar answered Oct 21 '22 08:10

tiago


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

like image 27
nye17 Avatar answered Oct 21 '22 08:10

nye17