I'm trying to overlay a scatter plot (showing observations) onto a filled contour plot (showing model data for the same variable) using exactly the same levels for mapping onto che colorscale. The main problem is that I'm not using the "common" contour
/contourf
functions but tricontourf
, since my data is on an unstructured grid.
It looks like some of the methods normally working on contourf
are ignored by tricontourf
and it's not the first time that I have to find a workaround, as options actually working with tricontourf
are poorly documented (e.g. how to plot with missing values).
I want to plot accumulated precipitation. I have punctual data from stations and gridded data from the model. Both are 1-D arrays described by 1-D latitude and longitude arrays. I NEED to choose the same levels for both plots since I want to overlay them and see the differences. I'm using pyplot.scatter
to plot the points and pyplot.tricontourf
for the model data. Here are the approaches that I've used:
- Define
vmin
andvmax
for bothpyplot.scatter
andpyplot.tricontourf
with the same cmap so that they are mapped with the same levels -> doesn't work aspyplot.tricontourf
apparently ignoresvmin
andvmax
. Following this example https://matplotlib.org/examples/pylab_examples/tricontour_vs_griddata.html does NOT work for me. They are simply ignored and the levels are automatically defined. Since the only way of specifying levels in
pyplot.tricontourf
is to use thelevels
argument I do so by defining an array of values. This works fortricontourf
but then I have to map it for the scatter plot.levels=[10, 12, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 125, 150, 175, 200, 225, 250] contourf=plt.tricontourf(lon, lat, rain_acc, levels=levels, cmap='GnBu', extend='max') bounds=np.array(levels) norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256) scatter=plt.scatter(x, y, c=rain_stations, s=50, cmap='GnBu', zorder=10, norm=norm)
Unfortunately this does not work either, as
BounaryNorm
linearly interpolates between the levels.
So for now the only way I had to overlay the data was to choose a linear sequence of levels (defined with np.linspace
) for tricontourf
and then apply the method of point 2. to normalize the colors for the scatter plots.
Here you can see the picture that I'm producing
However what I would like to do is to be able to define the levels manually. Does anyone know how to achieve that?
And, most importantly, why are vmin
and vmax
ignored by tricontourf
?