0

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:

  1. Define vmin and vmax for both pyplot.scatter and pyplot.tricontourf with the same cmap so that they are mapped with the same levels -> doesn't work as pyplot.tricontourf apparently ignores vmin and vmax. 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.
  2. Since the only way of specifying levels in pyplot.tricontourf is to use the levels argument I do so by defining an array of values. This works for tricontourf 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?

Droid
  • 441
  • 1
  • 3
  • 18
  • looking at the link you posted and the docs, `pyplot.tricontourf` doesnt seem to accept `vmin` and `vmax`. The way it was used in the example you posted is by using the `norm` argument: `plt.tricontourf(x, y, z,norm=plt.Normalize(vmax=abs(zi).max(), vmin=-abs(zi).max()))` – gyx-hh Aug 22 '18 at 15:18
  • 1
    I have also done this, and it still didn't work. The levels were still set automatically. By the way, nowhere on the documentation is said that `vmin` and `vmax` are ignored by `tricontourf`. I've found this on another question. – Droid Aug 22 '18 at 15:41

0 Answers0