自定义matplotlib cmap [英] Customising matplotlib cmaps

查看:802
本文介绍了自定义matplotlib cmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some normalised histogram data in array of shape (12,1):

>>> hnorm

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

我想以热图"样式绘制此图.我这样做是这样的:

I'd like to plot this in 'heatmap' style. I am doing this like so:

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

这有效(不包括轴格式化).

This works (axis formatting aside).

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

However, I'd like to customise the colormap to fade from white to Red. I have attempted:

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')

这失败.有什么想法我做错了吗?

This fails. Any ideas what I am doing wrong?

推荐答案

askewchan所建议的cmap'Reds'更简单,(imo)外观也更好.但是,我仅回答您的问题,以展示您构建自定义cmap的方法也可以如何工作.

The cmap 'Reds' as askewchan is suggesting is simpler and (imo) also better looking. But i'll answer just to show how your approach of building a custom cmap could also work.

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

In your color dict you have 5 entries at which you specify the color. Since you want to only use red and white you need only two enties. For white, all colors must be used which is specified by color values of 1.0 at position 0.0. For red only red should be used at position 1.0.

您还只为红色元组提供值(非0).这只会给您红色"和黑色"之间不同的红色阴影(因为您总是将绿色和蓝色设为0).

You also only provide values (other than 0) for your red tuple. This will only give you different shades of red between 'full' red and black (since you always have green and blue as 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)

另一个示例显示两个颜色项如何在cmap中允许跳转":

Another example showing how the two color items allow 'jumps' in the 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)

这篇关于自定义matplotlib cmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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