Matplotlib离散色条 [英] Matplotlib discrete colorbar

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

问题描述

我正在尝试为matplotlib中的散点图制作离散的颜色条

I am trying to make a discrete colorbar for a scatterplot in matplotlib

我有我的x,y数据,并且每个点都有一个整数标记值,我想用一种独特的颜色来表示它,例如

I have my x, y data and for each point an integer tag value which I want to be represented with a unique colour, e.g.

plt.scatter(x, y, c=tag)

标签通常是一个介于0到20之间的整数,但是确切的范围可能会更改

typically tag will be an integer ranging from 0-20, but the exact range may change

到目前为止,我刚刚使用了默认设置,例如

so far I have just used the default settings, e.g.

plt.colorbar()

给出连续的颜色范围.理想情况下,我想要一组n种离散颜色(在本示例中为n = 20).更好的做法是将标记值设为0产生灰色,而使值1-20变为彩色.

which gives a continuous range of colours. Ideally i would like a set of n discrete colours (n=20 in this example). Even better would be to get a tag value of 0 to produce a gray colour and 1-20 be colourful.

我找到了一些烹饪书"脚本,但是它们非常复杂,我不能认为它们是解决看似简单的问题的正确方法

I have found some 'cookbook' scripts but they are very complicated and I cannot think they are the right way to solve a seemingly simple problem

推荐答案

通过使用BoundaryNorm作为散点的规范化器,您可以非常轻松地创建自定义离散色条. (在我的方法中)古怪的位使0显示为灰色.

You can create a custom discrete colorbar quite easily by using a BoundaryNorm as normalizer for your scatter. The quirky bit (in my method) is making 0 showup as grey.

对于图像,我经常使用cmap.set_bad()并将数据转换为numpy掩码数组.使灰色为0会容易得多,但我无法将其与散点图或自定义cmap一起使用.

For images i often use the cmap.set_bad() and convert my data to a numpy masked array. That would be much easier to make 0 grey, but i couldnt get this to work with the scatter or the custom cmap.

作为替代方案,您可以从头开始制作自己的cmap,或者读出现有的cmap并仅覆盖某些特定条目.

As an alternative you can make your own cmap from scratch, or read-out an existing one and override just some specific entries.

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

fig, ax = plt.subplots(1, 1, figsize=(6, 6))  # setup the plot

x = np.random.rand(20)  # define the data
y = np.random.rand(20)  # define the data
tag = np.random.randint(0, 20, 20)
tag[10:12] = 0  # make sure there are some 0 values to show up as grey

cmap = plt.cm.jet  # define the colormap
# extract all colors from the .jet map
cmaplist = [cmap(i) for i in range(cmap.N)]
# force the first color entry to be grey
cmaplist[0] = (.5, .5, .5, 1.0)

# create the new map
cmap = mpl.colors.LinearSegmentedColormap.from_list(
    'Custom cmap', cmaplist, cmap.N)

# define the bins and normalize
bounds = np.linspace(0, 20, 21)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

# make the scatter
scat = ax.scatter(x, y, c=tag, s=np.random.randint(100, 500, 20),
                  cmap=cmap, norm=norm)

# create a second axes for the colorbar
ax2 = fig.add_axes([0.95, 0.1, 0.03, 0.8])
cb = plt.colorbar.ColorbarBase(ax2, cmap=cmap, norm=norm,
    spacing='proportional', ticks=bounds, boundaries=bounds, format='%1i')

ax.set_title('Well defined discrete colors')
ax2.set_ylabel('Very custom cbar [-]', size=12)

我个人认为使用20种不同的颜色很难读取特定的值,但这当然取决于您.

I personally think that with 20 different colors its a bit hard to read the specific value, but thats up to you of course.

这篇关于Matplotlib离散色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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