Search code examples
python-3.xbokeh

Bokeh: Unable to plot legend


I am not sure how to add legend to my plots based on updates to the Bokeh Library. Here is my code -

import numpy as np
import pandas as pd
url = 'https://raw.githubusercontent.com/Deepakgthomas/Lemonade_Sales/main/Lemonade_Lab8.csv'
lemon = pd.read_csv(url)
from bokeh.models import ColumnDataSource
source_Q4 = ColumnDataSource(lemon)
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
output_notebook()
p = figure(title = "Lemon and Orange Sales by Temperature")
p.circle("Temperature", "Lemon", source = source_Q4, color = "green", size = 8, legend = dict(value = "Lemon"))
p.triangle("Temperature", "Lemon", source = source_Q4, color = "orange", size = 8, legend = dict(value = "Orange"))
p.legend.location = "top_left"
show(p)

However, this gives me the warning -

"BokehDeprecationWarning: 'legend' keyword is deprecated, use explicit 'legend_label', 'legend_field', or 'legend_group' keywords instead
BokehDeprecationWarning: 'legend' keyword is deprecated, use explicit 'legend_label', 'legend_field', or 'legend_group' keywords instead"

Solution

  • As the Warning states, use legend_label instead of legend. For more information, check the user guide.

    import numpy as np
    import pandas as pd
    url = 'https://raw.githubusercontent.com/Deepakgthomas/Lemonade_Sales/main/Lemonade_Lab8.csv'
    lemon = pd.read_csv(url)
    from bokeh.models import ColumnDataSource
    source_Q4 = ColumnDataSource(lemon)
    from bokeh.io import output_notebook, show
    from bokeh.plotting import figure
    output_notebook()
    p = figure(title = "Lemon and Orange Sales by Temperature")
    p.circle("Temperature", "Lemon", source = source_Q4, color = "green", size = 8, legend_label = "Lemon")
    p.triangle("Temperature", "Orange", source = source_Q4, color = "orange", size = 8, legend_label = "Orange")
    p.legend.location = "top_left"
    
    show(p)