我想在matplotlib色彩映射中区分NaN。然后:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
# create a (4,5) matrix with values ranging from 0 to 19
np_data = np.arange(20).reshape((4,5)).astype(float)
# add a row with NaNs in the middle
np_data = np.insert(np_data,2,[np.nan for x in range(0,5)],axis=0)
# mask invalid data (NaNs)
np_data = np.ma.masked_invalid(np_data)
# get figure and ax objects from plot
fig, ax = plt.subplots()
# Draw an "X" on transparent values (masked values)
ax.patch.set(hatch='x', edgecolor='blue')
# get a predefined color scheme
reds_cm = plt.get_cmap("Reds")
# Plot heatmap, add a colorbar and show it
heatmap = ax.pcolor(np_data, cmap=reds_cm)
cbar = fig.colorbar(heatmap)
plt.show()
现在NaN很容易在情节中识别出来。
现在,我希望能够轻松区分NaNs,0和其他值。
如果我现在屏蔽了0,我就无法告诉NaN和0分开。
如何区分色彩映射中的两组值?在这种情况下,一方面是NaN,另一方面是0。
答案 0 :(得分:2)
如果您想告诉appart色彩映射的第一个或最后一个值,以下解决方案是一个很好的方法。您可以修改色彩映射,使这些值很容易变成不同的颜色
reds_cm = plt.get_cmap("name of colormap")
# init colormap such that its members are available
reds_cm._init()
# set the first value to black
reds_cm._lut[0,: ] = (0,0,0,1) #this is an RGBA tuple
# set the last value to lightgreen
reds_cm._lut[-4:,: ] = np.array([149,238,58,255])/255.
这是一个完整的解决方案。
import numpy as np
import matplotlib.pyplot as plt
# create a (4,5) matrix with values ranging from 0 to 19
np_data = np.arange(20).reshape((4,5)).astype(float)
# add a row with NaNs in the middle
np_data = np.insert(np_data,2,[np.nan for x in range(0,5)],axis=0)
# mask invalid data (NaNs)
np_data = np.ma.masked_invalid(np_data)
# get figure and ax objects from plot
fig, ax = plt.subplots()
# Draw an "X" on transparent values (masked values)
ax.patch.set(hatch='x', edgecolor='blue')
# get a predefined color scheme
reds_cm = plt.get_cmap("Reds")
# init colormap such that its members are available
reds_cm._init()
# set the first value to black
reds_cm._lut[0,: ] = (0,0,0,1)
# set the last value to lightgreen
reds_cm._lut[-4:,: ] = np.array([149,238,58,255])/255.
# Plot heatmap, add a colorbar and show it
heatmap = ax.pcolor(np_data, cmap=reds_cm)
cbar = fig.colorbar(heatmap)
plt.show()
答案 1 :(得分:0)
我在一个不相关的问题中找到了来自@unutbu的this answer。我调整了他对我的问题的答案,并解决了新的舱口也包含在NaN细胞中的问题。为了避免这种情况,只需在屏蔽numpy数组之前获取值为0的单元格(我会评论他的答案,在上下文中指出这一点,但我没有所需的声誉)。我只包括从我的问题改变的代码。
# (previous imports)
# Import to add patches to "non transparent" cells
import matplotlib.patches as mpatches
# (generate np_data)
# Get mask positions of 0 values before masking NaNs so NaN cells aren't included
cells_with_0 = np_data == 0
# mask invalid data (NaNs)
np_data = np.ma.masked_invalid(np_data)
# (get color scheme, plot heatmap, plot colorbar)
#set the background color as gray so the transparent values (NaNs here) use that color
ax.patch.set_facecolor((0.6, 0.6, 0.6, 1.0))
# Draw an "X" on transparent values (masked values)
ax.patch.set(hatch='x', edgecolor='black')
# Put an x over cells which have value 0
for j, i in np.column_stack(np.where(cells_with_0)):
ax.add_patch(
mpatches.Rectangle(
(i, j), # (x,y)
1, # width
1, # height
fill=False,
edgecolor='blue',
snap=False,
hatch='x' # the more slashes, the denser the hash lines
))
plt.show()