littleworth
littleworth

Reputation: 5169

How to draw a circle plot the LinearColorMapper using python Bokeh

With the following code,

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

colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'}
colors = [colormap[x] for x in flowers['species']]

p = figure(title = "Iris Morphology")
p.xaxis.axis_label = 'Petal Length'
p.yaxis.axis_label = 'Petal Width'

p.circle(flowers["petal_length"], flowers["petal_width"],
         color=colors, fill_alpha=0.2, size=10)

output_file("iris.html", title="iris.py example")

show(p)

I can make a circle plot where I color the species:

enter image description here

But what I want to do is to color all the point based on range of value in petal_length.

I tried this code but fail:

from bokeh.models import LinearColorMapper
exp_cmap = LinearColorMapper(palette='Viridis256', low = min(flowers["petal_length"]), high = max(flowers["petal_length"]))

p.circle(flowers["petal_length"], flowers["petal_width"], 
         fill_color = {'field'  : flowers["petal_lengh"], 'transform' : exp_cmap})

output_file("iris.html", title="iris.py example")

show(p)

And also in the final desired plot, how can I put the color bar that show the range of values and the assigned value. Something like this:

enter image description here

I'm using Python 2.7.13.

Upvotes: 3

Views: 4281

Answers (2)

bigreddot
bigreddot

Reputation: 34618

The colormapper transform refers to a column name and does not accept actual literal lists of data. So all the data needs to be in a Bokeh ColumDataSource and the plotting funcs all need to refer to the column names. Fortunately this is straightforward:

p.circle("petal_length", "petal_width", source=flowers, size=20,
         fill_color = {'field': 'petal_length', 'transform': exp_cmap})

enter image description here

Directions for legends outside the plot area are documented here:

https://docs.bokeh.org/en/latest/docs/user_guide/styling.html#outside-the-plot-area

Upvotes: 2

Tony
Tony

Reputation: 1290

To answer your first part, there was a small typo (petal_lengh instead of petal_length) but more importantly, using the bokeh.ColumnDataSource will solve your problem (I tried to do it without CDS and only got column errors):

from bokeh.plotting import figure, show, output_file
from bokeh.sampledata.iris import flowers  
from bokeh.models import LinearColorMapper
from bokeh.models import ColumnDataSource

p = figure(title = "Iris Morphology")
p.xaxis.axis_label = "Petal Length"
p.yaxis.axis_label = "Petal Width"

source = ColumnDataSource(flowers)

exp_cmap = LinearColorMapper(palette="Viridis256", 
                             low = min(flowers["petal_length"]), 
                             high = max(flowers["petal_length"]))

p.circle("petal_length", "petal_width", source=source, line_color=None,
        fill_color={"field":"petal_length", "transform":exp_cmap})

# ANSWER SECOND PART - COLORBAR
# To display a color bar you'll need to import 
# the `bokeh.models.ColorBar` class and pass it your mapper.
from bokeh.models import ColorBar
bar = ColorBar(color_mapper=exp_cmap, location=(0,0))
p.add_layout(bar, "left")

show(p)

enter image description here

See also: https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/color_data_map.py

Upvotes: 5

Related Questions