I am new to bokeh, and want to render a pie chart using bokeh figure.
I used the reference from https://docs.bokeh.org/en/latest/docs/gallery/pie_chart.html in order to create my pie chart figure.
Now, I need to add on each part of the pie chart a label which represent the percentage of this part, and the label position should be align to the center.
I could not find a simple way to do it via the documentation, and try to find ways to do it manually, like this example: Adding labels in pie chart wedge in bokeh
I tried to create a label set and add the layout to the plot but i could not figure out if there is a way to control the label position, size, and font. text_align (right, left, center) does not do the job for me.
Here is my code - this function create and return an html of the pie chart The chart argument contains the relevant data for the chart. in this case its a tuple (size 1), and series[0] contains the name of the series (series.title), list of x values (series.x), and list of y values (series.y)
def render_piechart(self, chart):
"""
Renders PieChart object using Bokeh
:param chart: Pie chart
:return:
"""
series = chart.series[0]
data_dict = dict(zip(series.x, series.y))
data = pd.Series(data_dict).reset_index(name='value').rename(columns={'index': 'Category'})
data['angle'] = data['value'] / data['value'].sum() * 2 * pi
data['color'] = palette[:len(series.x)]
data['percentage'] = data['value'] / data['value'].sum() * 100
data['percentage'] = data['percentage'].apply(lambda x: str(round(x, 2)) + '%')
TOOLTIPS = [('Category', '@Category'), ('Value', '@value'), ('Percentage', '@percentage')]
fig = figure(title=series.title,
plot_width=400 if chart.sizehint == 'medium' else 600,
plot_height=350 if chart.sizehint == 'medium' else 450,
tools='hover', tooltips=TOOLTIPS, x_range=(-0.5, 1.0))
fig.wedge(x=0, y=1, radius=0.45, start_angle=cumsum('angle', include_zero=True),
end_angle=cumsum('angle'), line_color='white', fill_color='color',
legend='Category', source=data)
fig.title.text_font_size = '20pt'
source = ColumnDataSource(data)
labels = LabelSet(x=0, y=1, text='percentage', level='glyph', angle=cumsum('angle', include_zero=True),
source=source, render_mode='canvas')
fig.add_layout(labels)
fig.axis.axis_label = None
fig.axis.visible = False
fig.grid.grid_line_color = None
return bokeh.embed.file_html(fig, bokeh.resources.CDN)
And this is the results: pie chart consist of 3 parts
in the 2 examples - the series title is 'kuku' x and y values for the first example: x=["A", "B", "C"] y=[10, 20, 30]
and for the second example: x=["A", "B", "C", "D", "E", "F", "G", "H", "I"] y=[10, 20, 30, 100, 90, 80, 70, 60, 30 , 40 ,50]
I know that in the past i could do it easily with Donut but it is deprecated. I want to be able to get something like this one: example1 or this: example2