在 matplotlib 轮廓图中同时使用 set_under 和 set_bad [英] Having both set_under and set_bad working in matplotlib contourf plot

查看:52
本文介绍了在 matplotlib 轮廓图中同时使用 set_under 和 set_bad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个matplotlib outlinef图,该图的所有值都在白色的指定值以下(包括零),而所有nan值(表示缺失的数据)在黑色.我似乎无法让 nan 值与低于/零值的颜色不同.问题的一个简化示例是:

将 numpy 导入为 np导入matplotlib.pyplot作为plt将cmplotlib.cm导入为cm# 为等高线图生成一些随机数据Z = np.random.rand(10,10)Z[0:3,0:3] = np.nan # set_bad 的一些错误值Z [0:3,7:10] = 0#set_under的一些零值x = np.arange(10)y = np.arange(10)X,Y = np.meshgrid(x, y)# 屏蔽坏数据:Z_masked = np.ma.array(Z,mask=np.isnan(Z))#获取颜色图并设置底色和不良颜色colMap = cm.gist_rainbowcolMap.set_bad(color='black')colMap.set_under(color='white')# 创建等高线图plt.figure(figsize=(10, 9))轮廓图 = plt.contourf(X,Y,Z_masked,cmap = colMap,vmin = 0.2)plt.colorbar(contourPlot)plt.show()

使用这个我得到下面链接的图,其中 nan 值(左下角)和零值(右下角)都是白色的 - 我不知道为什么 nan 值不是黑色的.

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.

Generated Figure

解决方案

The key is the example pointed to by @ViníciusAguia that notes that contourf simply does not draw anything where the data is invalid. If you had flipped the black and white in your example it would have looked like it worked!

A way to get what you want is to set the facecolor on your axes to the color you want for 'bad':

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
plt.ion()

# 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
# this has no effect see last comment block in
# https://matplotlib.org/examples/pylab_examples/contourf_demo.html
# colMap.set_bad(color='black')
colMap.set_under(color='white')

# Create the contour plot
fig, ax = plt.subplots()
contourPlot = ax.contourf(X,Y,Z_masked,cmap = colMap,vmin = 0.2, extend='both')
fig.colorbar(contourPlot)
# This is effectively 'bad' for contourf
ax.set_facecolor('k')
plt.show()

这篇关于在 matplotlib 轮廓图中同时使用 set_under 和 set_bad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆