Reputation: 55
I have some problems with color palette on matplotlib.
I want to show only 2 colors on legend because I'm showing a dummy variable (0 and 1).
Code works well with 0 and 1 values (red and blue). The problem is legend (see image).
palette = brewer['RdBu'][8]
If I put 2 (only 2 colors) on the previous code, I receive an error, because the palette contains 8 colors.
How can I change it?
palette = brewer['RdBu'][8]
palette = palette[::-1]
color_mapper = LinearColorMapper(palette = palette, low = 0, high = 1)
tick_labels = {'0': '0', '1': '1'}
color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8,width = 500, height = 20,
border_line_color=None,location = (0,0), orientation = 'horizontal', major_label_overrides = tick_labels)
Thank you in advance!
Upvotes: 0
Views: 779
Reputation: 6337
You can reduce your palette by slicing: For example
palette = brewer['RdBu'][8][:2]
is only two elements long and takes the first and the second item. The pallete below
palette = brewer['RdBu'][8][::7]
is also two elements long and takes the first and the last element (it is only a possible way of selecting these two elements).
Upvotes: 1