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. And I have a different scatter plot for each 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.

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

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归因
不隶属于 StackOverflow
scroll top