Matplotlib 离散颜色条 [英] Matplotlib discrete colorbar
问题描述
我正在尝试为 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屋!