Check Test
Check Test

Reputation: 3

How to add extra label / chart name to bokeh pie chart

I want to add a label say "countries" in bottom left corner of this chart [Not in title].

pie chart

i tried labelset and other methods but not able to find one suited to my problem.

from math import pi

import pandas as pd

from bokeh.io import output_file, show
from bokeh.palettes import Category20c
from bokeh.plotting import figure
from bokeh.transform import cumsum

output_file("pie.html")

x = {
    'United States': 157,
    'United Kingdom': 93,
    'Japan': 89,
    'China': 63,
    'Germany': 44,
    'India': 42,
    'Italy': 40,
    'Australia': 35,
    'Brazil': 32,
    'France': 31,
    'Taiwan': 31,
    'Spain': 29
}

data = pd.Series(x).reset_index(name='value').rename(columns={'index':'country'})
data['angle'] = data['value']/data['value'].sum() * 2*pi
data['color'] = Category20c[len(x)]

p = figure(plot_height=350, title="Pie Chart", toolbar_location=None,
           tools="hover", tooltips="@country: @value", x_range=(-0.5, 1.0))

p.wedge(x=0, y=1, radius=0.4,
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
        line_color="white", fill_color='color', legend_field='country', source=data)

p.axis.axis_label=None
p.axis.visible=False
p.grid.grid_line_color = None

show(p)

Upvotes: 0

Views: 117

Answers (1)

Tony
Tony

Reputation: 8297

Here are two options (tested on Bokeh v2.1.1):

p.wedge(x=0, y=0,...)
p.text(x=[0],y=[0],text=['Countries'],x_offset=-180,y_offset=140)
label=Label(x=0,y=0,x_units='screen',y_units='screen',text='Countries')
p.add_layout(label)

Result: enter image description here

Upvotes: 1

Related Questions