Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colour palette creation with python bokeh

I'm struggling to get my head around how to generate a colour palette for a bokeh.scatter plot from the 3rd column in a dataset. Here's an example:

from bokeh.plotting import figure, output_file, show
import pandas as pd

data_headers = ['eastings', 'northings', 'obs']
data_points = pd.read_csv('~/Documents/ds/data.csv', header=None, names=data_headers)

my_color_map = [(20, green_val, blue_val) for blue_val, green_val in zip(data_points.obs, 255-data_points.obs)]

output_file("scatter.html")

p=figure(x_range=(0, 5), y_range=(0, 10))
p.scatter(x=data_points.eastings, y=data_points.northings, size=5, fill_color=my_color_map)
show(p)

The data set is:

1,3,123
1,4,97
1,5,83
1,6,192
2,3,126
2,3.5,97
2,4.6,102
2,5.8,45

And the colour map is a reasonable spread thus:

>>> my_color_map
[(20, 132, 123), (20, 158, 97), (20, 172, 83), (20, 63, 192), (20, 129, 126), (20, 158, 97), (20, 153, 102), (20, 210, 45)]

The code works fine apart from it doesn't fill the circles. I can hardwire it with fill_color(100,100,100) to satisfy myself that I can change the colours in bulk but I want to make the colour a function of the 3rd column.

I'm trying to learn how to do this manually (i.e. just populate the green and blue channels in the RGB tuple) but I think my ultimate aim would be to plot the colour of a point at x,y using a brewer palette (etc) from booked.palettes to colour the point accordingly.

I can also get it working in matplotlib but the aim here is to see how bokeh works! Thanks for any help :-)

like image 931
DHBI Avatar asked Apr 27 '26 14:04

DHBI


1 Answers

Passing a list of color tuples doesn't work at the moment. Instead, you can turn your color definitions into strings and pass the list of strings to fill_color

from bokeh.plotting import figure, show, output_notebook
import pandas as pd
output_notebook()

TESTDATA=StringIO("""1,3,123
1,4,97
1,5,83
1,6,192
2,3,126
2,3.5,97
2,4.6,102
2,5.8,45
""")

data_headers = ['eastings', 'northings', 'obs']
data_points = pd.read_csv(TESTDATA, header=None, names=data_headers)

my_color_map = ["rgb({!s},{!s},{!s})".format(20, green_val, blue_val) for blue_val, green_val in zip(data_points.obs, 255-data_points.obs)]

p=figure(x_range=(0, 5), y_range=(0, 10))
p.scatter(x=data_points.eastings, y=data_points.northings, size=25, fill_color=my_color_map)
show(p)
like image 178
Jake Avatar answered Apr 29 '26 05:04

Jake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!