Bokeh h-bar chart by sourcing data from pandas data frame

I have a data frame, named, Score

	Source_of_crisis	Score_sum	Risk_bucket
0	Credit	7.3348	Red
1	Credit sentiment	4.0008	Green
2	Deposits	4.8750	Green
3	Equity market	5.1106	Yellow
4	FTP / Profitability	4.6250	Green
5	Interest rate	4.0012	Green
6	International economics	4.0000	Green
7	News	4.0000	Green

I want to:

  1. plot horizontal bar chart in Jupyter notebook
  2. Same in streamlit

Code for notebook:

# Dependencies
from bokeh.plotting import figure, show
from bokeh.io import output_notebook

x = Score['Source_of_crisis']
y = Score['Score_sum']
p = figure(
     title='simple chart',
     x_axis_label='x',
     y_axis_label='y')
p.hbar(x, y, legend_label='Trend', line_width=2)
output_notebook()
show(p)

Result: a frame is being created with a title and legend label. No figure inside.

What am I doing wrong?

Secondly, how would I do the same in streamlit?

Hi @anix_anirban,

I think what you are trying to do is this,

import pandas as pd

Score = pd.DataFrame({"Source_of_crisis": ["abc", "def", "ghi"], "Score_sum": [1, 2, 3]})
from bokeh.plotting import figure, show
from bokeh.io import output_notebook


x = Score['Source_of_crisis']
y = Score['Score_sum']
p = figure(
     title='simple chart',
     y_range=x.values,
     x_axis_label='x',
     y_axis_label='y')

p.yaxis.ticker = CategoricalTicker()
p.hbar(right=y, y=x, height=0.5)
output_notebook()
show(p)

It will show you this,

When dealing with categorical Data you need to tell bokeh what all values are there.
Look into this link for more, https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html

Also to show it in streamlit just do

st.bokeh_chart(p)

Instead of

output_notebook()
show()

Hope it helps!

1 Like