Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add legend inside Python's Bokeh circle plot

Tags:

python

plot

bokeh

I have 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)

Which produces this plot

enter image description here

How can I add legend based on the color of the circle to the plot? Where:

  • setosa : red
  • versicolor : green
  • virginica : blue
like image 362
scamander Avatar asked Apr 29 '18 03:04

scamander


People also ask

How do I hide my legend in bokeh?

Legends added to Bokeh plots can be made interactive so that clicking or tapping on the legend entries will hide or mute the corresponding glyph in a plot. These modes are activated by setting the click_policy property on a Legend to either "hide" or "mute" .


2 Answers

The easiest way that comes to mind is to define a ColumnDataSource to your plot to which you pass your data and from there reference the column with the "species" data from your dataframe.

Here is your code rework using this solution:

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

from bokeh.plotting import (ColumnDataSource, 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']]
flowers['colors'] = colors

source = ColumnDataSource(flowers)

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

p.circle("petal_length", "petal_width",
         color='colors', fill_alpha=0.2, size=10, legend='species',source=source)
p.legend.location = "top_left"

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

show(p)

And this is what you should obtain. I additionally put the legend to the right so it is not inserted on top of the plot:

Plot with solution

like image 171
josecoto Avatar answered Nov 06 '22 02:11

josecoto


You can use for-loop to plot it:

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'


for specie in colormap.keys():
    df = flowers[flowers['species']==specie]
    p.circle(df["petal_length"], df["petal_width"],
             color=colormap[specie], fill_alpha=0.2, size=10, legend=specie)

p.legend.location = "top_left"


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

show(p)

enter image description here

like image 34
R.yan Avatar answered Nov 06 '22 02:11

R.yan