Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is part of my contour plot showing white?

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()
like image 462
WVJoe Avatar asked Oct 14 '25 04:10

WVJoe


2 Answers

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

like image 65
SergioR Avatar answered Oct 16 '25 18:10

SergioR


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().

like image 45
jto Avatar answered Oct 16 '25 17:10

jto