简体   繁体   English

自定义Matplotlib ColorBar用于散点图

[英]Custom Matplotlib ColorBar for Scatterplot

I current have a few scatter plots each in their own sub plot. 我目前在自己的子图中有一些散点图。 The way the data is formatted is: 数据格式化的方式是:

ID, X, Y, Colors, Area
0, x1, y1, r, 1
1, x2, y2, g, 1
2, x3, y3, r, 3
3, x4, y4, b, 4
....
n, xn, yn, g, 3

So when I plot a scatterplot it looks like ax.scatter(X, Y, color=Colors) Thus each point is either colored red, green, or blue, accordingly. 因此,当我绘制散点图时,它看起来像ax.scatter(X, Y, color=Colors)因此,每个点都相应地被着色为红色,绿色或蓝色。 And I have a different scatter plot for each Area . 而且我对每个Area都有不同的散点图。

I am trying to create a custom colorbar on the side to show the three colors (red, green, and blue), and an associated label with each color. 我正在尝试在侧面创建自定义颜色条,以显示三种颜色(红色,绿色和蓝色)以及每种颜色的关联标签。 In this case it is an income bracket. 在这种情况下,这是一个收入等级。

So far googling has led me to look into creating a custom colormap, and then plotting a colorbar in its own subplot using that colormap. 到目前为止,谷歌搜索使我开始研究创建自定义颜色图,然后使用该颜色图在其自己的子图中绘制颜色栏。 However I have never used a custom colormap before I am at a little bit of a loss when going through the matplotlib example. 但是,在进行matplotlib示例时,我有点不知所措,我从未使用过自定义颜色图。

So far I have this, but I'm pretty sure it is just random whatnots (all it gives me is a grayscale colormap in the ax3 subplot): 到目前为止,我已经知道了,但是我可以肯定这只是随机的whatnots(它给我的是ax3子图中的灰度色图):

cdict1 = {'red': ((0,0,0),(1,1,1)),
        'green': ((0,0,0),(1,1,1)),
        'blue': ((0,0,0),(1,1,1))}
rgbtest = LinearSegmentedColormap('test', cdict1)
plt.register_cmap(name='test', data=cdict1)
cmap = plt.get_cmap('test')
matplotlib.colorbar.ColorbarBase(ax3, cmap=rgbtest)

Any suggestions? 有什么建议么? I hope this makes sense. 我希望这是有道理的。 Thank you. 谢谢。 Cheers 干杯

I ended up going with this. 我结束了这个。 I'm not sure if it is the best way - if you have alternate suggestions perhaps I can learn from them for future use! 我不确定这是否是最好的方法-如果您有其他建议,也许我可以向他们学习以供将来使用!

cmap = matplotlib.colors.ListedColormap(['green', 'blue', 'red'])
bounds=[0,125,200,400]
cax = inset_axes(ax3, width="8%", height='70%', loc=4)
cbar = matplotlib.colorbar.ColorbarBase(cax, cmap=cmap, boundaries=bounds)
cax.yaxis.set_ticks_position('left')
cbar.ax.set_yticklabels(['0', '125', '200', '200+'])
cax.yaxis.set_label_position('left')
cbar.set_label('Income (,000s)')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM