Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having both set_under and set_bad working in matplotlib contourf plot

Tags:

I am trying to produce a matplotlib contourf plot that has all values under a specified value in white (including zero) and that has all nan values (representing missing data) in black. I can't seem to get the nan values to be a different color then the under/zero values.A simplified example of the problem is:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# Generate some random data for a contour plot
Z = np.random.rand(10,10)
Z[0:3,0:3] = np.nan # some bad values for set_bad
Z[0:3,7:10] = 0 # some zero values for set_under
x = np.arange(10)
y = np.arange(10)
X,Y = np.meshgrid(x, y)

# Mask the bad data:
Z_masked = np.ma.array(Z,mask=np.isnan(Z))

# Get the colormap and set the under and bad colors
colMap = cm.gist_rainbow
colMap.set_bad(color='black')
colMap.set_under(color='white')

# Create the contour plot
plt.figure(figsize=(10, 9))
contourPlot = plt.contourf(X,Y,Z_masked,cmap = colMap,vmin = 0.2)
plt.colorbar(contourPlot)
plt.show()

Using this I get the figure linked below, where both the nan values (bottom left) and zero values (bottom right) are white - I'm not sure why the nan values are not black.

buggy figure

Generated Figure

like image 926
Shawn Avatar asked May 12 '17 17:05

Shawn


1 Answers

Since the masked regions are not filled at all, you might simply set the background of the plot to the color you would want to attribute to the "bad" values, plt.gca().set_facecolor("black").

Complete example:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# Generate some random data for a contour plot
Z = np.random.rand(10,10)
Z[0:3,0:3] = np.nan # some bad values for set_bad
Z[0:3,7:10] = 0 # some zero values for set_under
x = np.arange(10)
y = np.arange(10)
X,Y = np.meshgrid(x, y)

# Mask the bad data:
Z_masked = np.ma.array(Z,mask=np.isnan(Z))

# Get the colormap and set the under and bad colors
colMap = cm.gist_rainbow
colMap.set_under(color='white')

# Create the contour plot
plt.figure(figsize=(10, 9))
plt.gca().set_facecolor("black")
contourPlot = plt.contourf(X,Y,Z_masked,cmap = colMap,vmin = 0.2)
plt.colorbar(contourPlot)
plt.show()

enter image description here

like image 155
ImportanceOfBeingErnest Avatar answered Sep 25 '22 10:09

ImportanceOfBeingErnest