6

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?

pter
  • 611
  • 2
  • 9
  • 16

2 Answers2

4

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.

tiago
  • 22,602
  • 12
  • 72
  • 88
  • It's not on a regular grid. The pcolormesh had an interesting result but it seemed almost too fuzzy. I think interpolating to a regular grid then using imshow is the way to go, but I wasn't quite able to figure out how to do that. – pter Aug 22 '12 at 23:13
  • I just updated my answer with a simple example of interpolation. – tiago Aug 22 '12 at 23:42
3

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

nye17
  • 12,857
  • 11
  • 58
  • 68
  • I guess the issue isn't so much that I'm stuck on pcolor as much as I don't know how I could produce a plot that would stick to the same axis (if that makes sense) – pter Aug 22 '12 at 22:05
  • @pter If I understand you correctly this time, you want a color scale that is more stretched for high depths and less so for low ones? – nye17 Aug 22 '12 at 22:12
  • Hmm, I'm not sure if thats what I want. On my pcolor graph the upper depths transistion smoothly, the top 1 is only a pixel and they slowly get taller from there, so its very easy to see the line where the bottom and second to bottom depths meet. I wish I could add an image of my plot. – pter Aug 22 '12 at 22:21
  • @pter a plot would help, why not? – nye17 Aug 22 '12 at 22:22
  • @pter it sounds like you want to stretch your axis, why not do a logarithmic scale for your depth? – nye17 Aug 22 '12 at 22:26
  • I'm really struggling to figure out how upload this image. I need the scale to stay the same, otherwise a logarithmic scale would definitely solve the problem. – pter Aug 22 '12 at 22:31
  • @pter do you have the image ready or not? if yes, you can simply click the picture icon on the top of your editing box after you click the "edit" button underneath your post. – nye17 Aug 22 '12 at 22:34
  • @pter still, you can do a transformation for your depth variable, sometthing like, z = log(15.0 - depth) to highlight the higher depths. And for the colorbar, you can manually change the axis labels to be the actually values of depth rather than z, although you will be using z in the pcolor plot. – nye17 Aug 22 '12 at 22:43
  • I'm sorry, I guess I'm confused about what you're saying. What's the z variable? – pter Aug 22 '12 at 22:56