4

I am using Python's matplotlib.pyplot.contourf to create a contour plot of my data with a color bar. I have done this successfully countless times, even with other layers of the same variable. However, when the values get small (on the order of 1E-12), parts of the contour show up white. The white color does not show up in the color bar either. Does anyone know what causes this and how to fix this? The faulty contour is attached below.

Bad contour

a1 = plt.contourf(np.linspace(1,24,24),np.linspace(1,20,20),np.transpose(data[:,:,15]))
plt.colorbar(a1)
plt.show()
WVJoe
  • 515
  • 7
  • 21
  • Did you check what values are represented with white? – SergioR Apr 30 '20 at 01:12
  • 2
    Also, what is you plotting code? – SergioR Apr 30 '20 at 01:42
  • 1
    Might want to check and make sure you don't have any `nan`s in your data – William Miller Apr 30 '20 at 02:54
  • There are no nans in the data, and the values in white are just slightly larger than the upper end of the color bar. Also, I updated the post to include the plotting code. – WVJoe Apr 30 '20 at 16:11
  • 1
    Try with `plt.contourf(..., extend="max")`. – SergioR Apr 30 '20 at 19:54
  • That did make the white disappear, it just shows an extension for anything greater than the upper end of the color bar, which does solve my problem. Can you put that in an answer that way I can close it so other people can see the solution in the future? – WVJoe Apr 30 '20 at 23:05
  • I'm writing it. It's going to include other cases just in case. – SergioR Apr 30 '20 at 23:25

3 Answers3

8

tl;dr

Given the new information, matplotlib couldn't set the right number of levels (see parameters in the documentation) for your data leaving data unplotted. To fix that you need to tell matplotlib to extend the limits with either plt.contourf(..., extend="max") or plt.contourf(..., extend="both")

enter image description here

Extensive answer

There are a few reasons why contourf() is showing white zones with a colormap that doesn't include white.

NaN values

NaN values are never plotted.

enter image description here

Masked data

If you mask data before plotting, it won't appear in the plot. But you should know if you masked your data.

enter image description here

Although, you may have unnoticed mask your data if you use something like Tick locator = LogLocator().

enter image description here

Matplotlib couldn't set the right levels for your data

Sometimes matplotlib doesn't set the right levels, leaving some of your data without plotting.

enter image description here

To fix that you can user plt.contourf(..., extend=EXTENDS) where EXTENDS can be "neither", "both", "min", "max"

enter image description here

Coarse grid

contourf plots whitespace over finite data. Past answers do not correct

Community
  • 1
  • 1
SergioR
  • 1,386
  • 1
  • 7
  • 14
1

One remark, white section in the plot can also occur if the X and Y vectors data points are not equally spaced. In that case best to use function tricontourf().

jto
  • 11
  • 1
0

I was facing the same problem recently, when there was data available even higher/lower than the levels I have set. So, the plt.contourf fills the contours exclusively given by you, and it neglects any other higher or lower values present in your data. I solved this by adding a key word argument extend="both", which for your case would be something like this:

a1 = plt.contourf(np.linspace(1,24,24),np.linspace(1,20,20),np.transpose(data[:,:,15]), extend="both")

or in general form:

a1 = plt.contourf(x,y,variable[:,:,15],extend="both")

By doing this, you're instructing the module to plot the higher(/lower) values according to the highest(/lowest) filled contour. If you want only to extend in the lower or higher range, you can change the keyword argument to

extend="min" or extend ="max"