I'm trying to display 2D data with axis labels using both contour
and pcolormesh
. As has been noted on the matplotlib user list, these functions obey different conventions: pcolormesh
expects the x and y values to specify the corners of the individual pixels, while contour
expects the centers of the pixels.
What is the best way to make these behave consistently?
One option I've considered is to make a "centers-to-edges" function, assuming evenly spaced data:
def centers_to_edges(arr):
dx = arr[1]-arr[0]
newarr = np.linspace(arr.min()-dx/2,arr.max()+dx/2,arr.size+1)
return newarr
Another option is to use imshow
with the extent
keyword set.
The first approach doesn't play nicely with 2D axes (e.g., as created by meshgrid
or indices
) and the second discards the axis numbers entirely