如何使用 imshow 将 NaN 值绘制为特殊颜色?

发布于 2024-08-28 02:25:03 字数 549 浏览 7 评论 0原文

我试图在 matplotlib 中使用 imshow 将数据绘制为热图,但某些值是 NaN。我希望将 NaN 渲染为颜色图中未找到的特殊颜色。

示例:

import numpy as np
import matplotlib.pyplot as plt
f = plt.figure()
ax = f.add_subplot(111)
a = np.arange(25).reshape((5,5)).astype(float)
a[3,:] = np.nan
ax.imshow(a, interpolation='nearest')
f.canvas.draw()

生成的图像出人意料地全是蓝色(jet 颜色图中最低的颜色)。但是,如果我像这样进行绘图:

ax.imshow(a, interpolation='nearest', vmin=0, vmax=24)

--然后我会得到更好的东西,但是 NaN 值绘制为与 vmin 相同的颜色...是否有一种优雅的方法可以将 NaN 设置为使用特殊颜色绘制(例如:灰色或透明)?

I am trying to use imshow in matplotlib to plot data as a heatmap, but some of the values are NaNs. I'd like the NaNs to be rendered as a special color not found in the colormap.

example:

import numpy as np
import matplotlib.pyplot as plt
f = plt.figure()
ax = f.add_subplot(111)
a = np.arange(25).reshape((5,5)).astype(float)
a[3,:] = np.nan
ax.imshow(a, interpolation='nearest')
f.canvas.draw()

The resultant image is unexpectedly all blue (the lowest color in the jet colormap). However, if I do the plotting like this:

ax.imshow(a, interpolation='nearest', vmin=0, vmax=24)

--then I get something better, but the NaN values are drawn the same color as vmin... Is there a graceful way that I can set NaNs to be drawn with a special color (eg: gray or transparent)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁把谁当真 2024-09-04 02:25:03

嗯,看来我可以使用屏蔽数组来做到这一点:

masked_array = np.ma.array (a, mask=np.isnan(a))
cmap = matplotlib.cm.jet
cmap.set_bad('white',1.)
ax.imshow(masked_array, interpolation='nearest', cmap=cmap)

这应该足够了,尽管我仍然愿意接受建议。 :]

Hrm, it appears I can use a masked array to do this:

masked_array = np.ma.array (a, mask=np.isnan(a))
cmap = matplotlib.cm.jet
cmap.set_bad('white',1.)
ax.imshow(masked_array, interpolation='nearest', cmap=cmap)

This should suffice, though I'm still open to suggestions. :]

归途 2024-09-04 02:25:03

使用较新版本的 Matplotlib,不再需要使用屏蔽数组。

例如,让我们生成一个数组,其中每个第 7 个值都是 NaN:

arr = np.arange(100, dtype=float).reshape(10, 10)
arr[~(arr % 7).astype(bool)] = np.nan

.cm.get_cmap()matplotlib v3.7.0 中被替换为 .colormaps.get_cmap('viridis')

设置颜色为.set_bad

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

arr = np.arange(100, dtype=float).reshape(10, 10)
arr[~(arr % 7).astype(bool)] = np.nan

cmap = mpl.colormaps.get_cmap('viridis')  # viridis is the default colormap for imshow
cmap.set_bad(color='red')

plt.imshow(arr, cmap=cmap)

plot result


.cm.get_cmap() 已弃用

我们可以修改当前颜色图并绘制包含以下行的数组:

current_cmap = mpl.cm.get_cmap()
current_cmap.set_bad(color='red')
plt.imshow(arr)

With newer versions of Matplotlib, it is not necessary to use a masked array anymore.

For example, let’s generate an array where every 7th value is a NaN:

arr = np.arange(100, dtype=float).reshape(10, 10)
arr[~(arr % 7).astype(bool)] = np.nan

.cm.get_cmap() is replaced by .colormaps.get_cmap('viridis') in matplotlib v3.7.0

Set the color with .set_bad.

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

arr = np.arange(100, dtype=float).reshape(10, 10)
arr[~(arr % 7).astype(bool)] = np.nan

cmap = mpl.colormaps.get_cmap('viridis')  # viridis is the default colormap for imshow
cmap.set_bad(color='red')

plt.imshow(arr, cmap=cmap)

plot result


.cm.get_cmap() is deprecated

We can modify the current colormap and plot the array with the following lines:

current_cmap = mpl.cm.get_cmap()
current_cmap.set_bad(color='red')
plt.imshow(arr)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文