Giovanni Pontonio
Giovanni Pontonio

Reputation: 55

Colormaps in Bokeh

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).enter image description here

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

Answers (1)

mosc9575
mosc9575

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

Related Questions