1

I have two independent datasets, z_p and z_g here, and I would like to put two tricontourf() instances on the same axis, each instance corresponding to a contour of one dataset. Below is a pseudo-code of what I do:

  cmap_p      = plt.get_cmap('Reds') 
  norm_p      = BoundaryNorm(levels, ncolors=cmap_p.N, clip=True)
  cmap_g      = plt.get_cmap('Blues')
  norm_g      = BoundaryNorm(levels, ncolors=cmap_g.N, clip=True)

  lev = range(lower_level, upper_level+1)
  obj_g = ax.tricontourf(x, y, z_g, cmap=cmap_g, norm=norm_g,
                 levels=lev, extent=[x0, y0, x1, y1], zorder=2)

  obj_p = ax.tricontourf(x, y, z_p, cmap=cmap_p, norm=norm_p,
                 levels=lev, extent=[x0, y0, x1, y1], zorder=3)

The output figure is attached below. Clearly, only the second call to tricontourf() has effectively worked, since there is a patch on the left of the figure in red. If I comment out the call to get obj_p, then I get a blue patch on the right side of the figure in blue color. However, the two subsequent calls to tricontourf() do not work simultaneously.

I would be grateful if someone would tell me how to show both contours on the same axis?

Resulting figure. Only the red contours show on the left, and the blue contours on the right are missing

Ehsan
  • 380
  • 4
  • 9
  • I'm not positive, but my guess is that you expect the "white" spaces to be transparent, when in fact they are opaque. The blue one is probably there, you just can't see it under the white. – Ajean Aug 06 '15 at 01:52
  • @Ajean: Very true; indeed adding `alpha=0.5` to each of my `ax.tricontourf()` instances solved the issue. Thanks a lot for your words. – Ehsan Aug 06 '15 at 07:39

1 Answers1

0

Your second plot is covering up the first plot with an opaque white color, which is why you see the first one when you don't plot the second. If you want to see both, you can set the alpha keyword:

alpha=0.5

to make the plots transparent enough to see through. Alternatively, there is an answer at this SO question about masking a call to tricontourf, if you know where the overlapping region is and want to simply mask it out of the second plot.

Community
  • 1
  • 1
Ajean
  • 5,528
  • 14
  • 46
  • 69
  • Thanks. I already tried `alpha=0.5`, however, both patches become improperly dim, which I do not want at all. So, this has raised another question: Is it possible to superpose two (or even more) `ax.contourf()` instances, but making sure that the patch corresponding to the lowest `levels` is _transparent_. This will allow me to still keep `alpha=1.00`, and superpose multiple `contour()` instances, making sure that their white space of the top overlay does not cover those in the bottom. My contours have enough white spaces. Any recommendations? Best. Ehsan. – Ehsan Aug 06 '15 at 19:16
  • Did you look at the answer in the question I linked? It has info doing precisely that for tricontourf (making part of the output transparent). If you want to use contourf instead (it's actually simpler), you can do the same thing - use NaNs to make matplotlib ignore things. – Ajean Aug 07 '15 at 00:20