Interpolation with matplotlib pcolor

I have two numpy arrays, the first one (30, 365) and contains values ​​for 30 depths during the year, the second array (30, 1) and contains the actual depth (in meters) corresponding to the depth in the first array. I want to build 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 to each other, and the lower depths are far apart, providing a block view of the pcolor image.)

This is what I do:

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 leads to a blocky look, any ideas on how I could get a smoother schedule?

+5
source share
2 answers

Are your depths a regular grid (i.e. a constant spacing)? If so, you can use imshow and set the range using the extent and aspect='auto' keywords. Otherwise, you have two options.

Instead, you can use pcolormesh and use shading='gouraud' . This will help with sharp color quantization, but not as good as interpolation.

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

 from scipy.interpolate import interp1d fint = interp1d(depth, data.T, kind='cubic') newdata = fint(newdepth).T 

Added .T , because the interpolation should be at the last index, and the depth is the first index of your data. You can replace kind with 'linear' if you want.

+4
source

no, pcolor does not interpolate. You can try NonUniformImage or even imshow . Check out the examples here.

+3
source

All Articles