设置Matplotlib中特定数据范围对应的离散颜色映射

2024-10-01 15:43:07 发布

您现在位置:Python中文网/ 问答频道 /正文

一些背景

我有一个(50,50)形状的二维数组,数据值的范围是-40~40。
但我想在三个数据范围内绘制数据[<;0]、[0,20]、[>;20]

然后,我需要生成一个对应于这三个部分的颜色映射。在

我现在有一些想法

## ratio is the original 2-d array
binlabel = np.zeros_like(ratio)
binlabel[ratio<0] = 1
binlabel[(ratio>0)&(ratio<20)] = 2
binlabel[ratio>20] = 3

def discrete_cmap(N, base_cmap=None):
    base = plt.cm.get_cmap(base_cmap)
    color_list = base(np.linspace(0, 1, N))
    cmap_name = base.name + str(N)
    return base.from_list(cmap_name, color_list, N)

fig  = plt.figure()
ax = plt.gca()
plt.pcolormesh(binlabel, cmap = discrete_cmap(3, 'jet'))
divider = make_axes_locatable(ax)
cax = divider.append_axes("bottom", size="4%", pad=0.45)
cbar = plt.colorbar(ratio_plot, cax=cax, orientation="horizontal")
labels = [1.35,2,2.65]
loc    = labels
cbar.set_ticks(loc)
cbar.ax.set_xticklabels(['< 0', '0~20', '>20']) 

有更好的方法吗?任何建议都将不胜感激。在


Tags: 数据namebasenppltaxlistcolor
1条回答
网友
1楼 · 发布于 2024-10-01 15:43:07

使用ListedColormapBoundaryNorm对其他问题有不同的答案,但这里有一个替代方法。我忽略了你的颜色条的位置,因为那与你的问题无关。在

您可以用对np.digitize()的调用替换binlabel计算,并通过使用lut参数来替换{}函数。此外,我发现将颜色边界放在索引之间的0.5个中点比缩放到奇数的尴尬分数更容易:

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

ratio = np.random.random((50,50)) * 50.0 - 20.0

fig2, ax2 = plt.subplots(figsize=(5,5))

# Turn the data into an array of N bin indexes (i.e., 0, 1 and 2).
bounds = [0,20]
iratio = np.digitize(ratio.flat,bounds).reshape(ratio.shape)

# Create a colormap containing N colors and a Normalizer that defines where 
# the boundaries of the colors should be relative to the indexes (i.e., -0.5, 
# 0.5, 1.5, 2.5).
cmap = cm.get_cmap("jet",lut=len(bounds)+1)
cmap_bounds = np.arange(len(bounds)+2) - 0.5
norm = mcol.BoundaryNorm(cmap_bounds,cmap.N)

# Plot using the colormap and the Normalizer.
ratio_plot = plt.pcolormesh(iratio,cmap=cmap,norm=norm)
cbar = plt.colorbar(ratio_plot,ticks=[0,1,2],orientation="horizontal")
cbar.set_ticklabels(["< 0","0~20",">20"])

Example discrete color bar

相关问题 更多 >

    热门问题