python - 自定义 matplotlib cmap

标签 python numpy matplotlib plot

我有一些形状为 (12,1) 的归一化直方图数据:

>>> hnorm

   array([[ 0.        ],
       [ 0.        ],
       [ 0.01183432],
       [ 0.0295858 ],
       [ 0.04142012],
       [ 0.04142012],
       [ 0.03550296],
       [ 0.01775148],
       [ 1.        ],
       [ 0.98816568],
       [ 0.56213018],
       [ 0.        ]])

我想以“热图”样式绘制它。我这样做是这样的:

import matplotlib.pyplot as plt
plt.imshow(hnorm, cmap='RdBu',origin='lower')

这有效(除了轴格式)。

enter image description here

但是,我想自定义颜色图以从白色渐变为红色。我尝试过:

import matplotlib.colors as col

cdict = {'red': ((0.00, 0.07, 0.14),
        (0.21, 0.28, 0.35),
        (0.42, 0.49, 0.56),
        (0.63, 0.70, 0.77),
        (0.84, 0.91, 0.99)),
        'green': ((0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0)),
        'blue': ((0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0), 
        (0.0, 0.0, 0.0))}
cmap1 = col.LinearSegmentedColormap('my_colormap',cdict,N=256,gamma=0.75)
plt.imshow(hnorm, cmap=cmap1,origin='lower')

这失败了。知道我做错了什么吗?

最佳答案

askewchan 建议的 cmap 'Reds' 更简单而且 (imo) 也更好看。但我的回答只是为了展示您构建自定义 cmap 的方法如何工作。

在您的颜色字​​典中,您有 5 个条目可以指定颜色。由于您只想使用红色和白色,因此您只需要两个实体。对于白色,必须使用由位置 0.0 处的颜色值 1.0 指定的所有颜色。对于红色,只应在位置 1.0 处使用红色。

您还只为红色元组提供值(0 除外)。这只会为您提供“全”红色和黑色之间的不同深浅的红色(因为您总是将绿色和蓝色设为 0)。

cdict = {'red': ((0.0, 1.0, 1.0),
                 (1.0, 1.0, 1.0)),

        'green': ((0.0, 1.0, 1.0),
                  (1.0, 0.0, 0.0)),

        'blue': ((0.0, 1.0, 1.0),
                 (1.0, 0.0, 0.0))}

my_cmap = mpl.colors.LinearSegmentedColormap('my_colormap', cdict)

plt.imshow(np.random.rand(20,20), cmap=my_cmap, origin='lower', interpolation='none')
plt.colorbar(shrink=.75)

enter image description here

显示两个颜色项如何在 cmap 中“跳跃”的另一个示例:

cdict = {'red': ((0.0, 1.0, 1.0), # full red
                 (0.5, 1.0, 0.0), # full red till, no red after
                 (1.0, 1.0, 1.0)), # full red

        'green': ((0.0, 1.0, 1.0), # full green
                  (0.5, 0.0, 0.0), # no green
                  (1.0, 1.0, 1.0)), # full green

        'blue': ((0.0, 1.0, 1.0), # full blue
                 (0.5, 0.0, 1.0), # no blue till, full blue after
                 (1.0, 1.0, 1.0))} # full blue

my_cmap = mpl.colors.LinearSegmentedColormap('my_colormap', cdict)

plt.imshow(np.random.rand(20,20), cmap=my_cmap, origin='lower', interpolation='none')
plt.colorbar(shrink=.75)

enter image description here

关于python - 自定义 matplotlib cmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18660758/

相关文章:

python - 在第二遍期间无法与对象交互

python - 消除Cython numpy编译警告的方法?

python - 使用 matplotlib 和 seaborn 创建圆形密度图

python - 在Python中绘制条形图

python - 结束 ssh session 后在后台运行 python/matplotlib 的问题

python - 有没有更好的方法来使用 Python 的 typing 模块为复合类型创建类型别名?

python - ValueError : Can not squeeze dim[1], 期望维度为 1,'sparse_softmax_cross_entropy_loss 得到 3

python - 使用 PySerial 读取空字节

python - 如何在 Python 的二维数组中查找值的索引?

python - 如何 reshape 具有不同维度的 3d 数组?