OD1995
OD1995

Reputation: 1777

Make the colour AND marker of bokeh plot scatter points dependent on dataframe values

I've been playing around with bokeh in order to get an interactive scatter plot, with tooltips and interactive legends etc.

Currently I am able to set the colour of the points using the values of a column in the pandas dataframe behind the plot. However I'm wondering if it's possible to set the marker type (diamond, circle, square etc.) as well, using another column in the dataframe?

I appreciate this would mean you'd need a double legend, but hopefully this wouldn't be too much of a problem.

Upvotes: 2

Views: 6413

Answers (1)

bigreddot
bigreddot

Reputation: 34568

This can be accomplished with marker_map and CDS filters:

from bokeh.plotting import figure, show, output_file
from bokeh.sampledata.iris import flowers
from bokeh.transform import factor_cmap, factor_mark

SPECIES = ['setosa', 'versicolor', 'virginica']
MARKERS = ['hex', 'circle_x', 'triangle']

p = figure(title = "Iris Morphology", background_fill_color="#fafafa")
p.xaxis.axis_label = 'Petal Length'
p.yaxis.axis_label = 'Sepal Width'

p.scatter("petal_length", "sepal_width", source=flowers, legend="species", 
          fill_alpha=0.4, size=12,
          marker=factor_mark('species', MARKERS, SPECIES),
          color=factor_cmap('species', 'Category10_3', SPECIES))

show(p)

enter image description here

Upvotes: 6

Related Questions