Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate Bubble Chart with Bokeh

I want to create a bubble chart using Bokeh based on categorical x-axis and y-axis and use count as their sizes.

Here's the data frame that I have, and I successfully created with Seaborn:

enter image description here

This is a brief version from Seaborn that I created

import pandas as pd
import seaborn as sns

d = {'T_range': ['0-50', '0-50', '0-50', '0-50',
                 '51-60', '51-60', '51-60', '51-60',
                 '61-70', '61-70', '61-70', '61-70'],
     'Subject': ['English', 'Maths', 'Chinese', 'Arts',
                 'English', 'Maths', 'Chinese', 'Arts',
                'English', 'Maths', 'Chinese', 'Arts'],
     'count': [603, 240, 188, 89,
               220, 118, 112, 43,
              123, 2342, 32, 212]}

df_test = pd.DataFrame(data=d)

sns.set(rc={'figure.figsize':(15, 10)})
ax = sns.scatterplot(x='T_range', y='Subject', size='count', hue='Subject',
                    sizes=(100, 5000), legend=None, data=df_test)

display(df_test)

# Show result
ax


I would like to know how to achieve the same thing by using Bokeh. Thank you in advance.


Solved

Thanks to responders. I have managed to generate a graph exactly what I wanted. I have adjusted it a bit to suit application as follows:

x = df[range_name].tolist()
y = df[group_name].tolist()
size = list(map(lambda i: i/10, df['count'].tolist()))

d = {'{}'.format(range_name): x,
     '{}'.format(group_name): y,
     'count': size}
like image 421
N. Arunoprayoch Avatar asked Oct 23 '25 14:10

N. Arunoprayoch


1 Answers

This is how you can do the same in Bokeh v1.0.4. Run in Terminal using: python app.py

import pandas as pd
from bokeh.plotting import show, figure
from bokeh.models import ColumnDataSource, LinearColorMapper, ColorBar, BasicTicker, PrintfTickFormatter, HoverTool
from bokeh.palettes import Viridis256
from bokeh.transform import transform

scale = 10
d = {'T_range': ['0-50', '0-50', '0-50', '0-50',
                 '51-60', '51-60', '51-60', '51-60',
                 '61-70', '61-70', '61-70', '61-70'],
     'Subject': ['English', 'Maths', 'Chinese', 'Arts',
                 'English', 'Maths', 'Chinese', 'Arts',
                'English', 'Maths', 'Chinese', 'Arts'],
     'count': [603, 240, 188, 89,
               220, 118, 112, 43,
              123, 2342, 32, 212],
     'count_scaled': [603 / scale, 240 / scale, 188 / scale, 89 / scale,
           220 / scale, 118 / scale, 112 / scale, 43 / scale,
          123 / scale, 2342 / scale, 32 / scale, 212 / scale]}

df = pd.DataFrame(data = d)
source = ColumnDataSource(df)
p = figure(x_range = df['T_range'].unique(), y_range = df['Subject'].unique())

color_mapper = LinearColorMapper(palette = Viridis256, low = df['count'].min(), high = df['count'].max())
color_bar = ColorBar(color_mapper = color_mapper,
                     location = (0, 0),
                     ticker = BasicTicker())
p.add_layout(color_bar, 'right')
p.scatter(x = 'T_range', y = 'Subject', size = 'count_scaled', legend = None, fill_color = transform('count', color_mapper), source = source)
p.add_tools(HoverTool(tooltips = [('Count', '@count')]))
show(p)

Result:

enter image description here

like image 70
Tony Avatar answered Oct 25 '25 05:10

Tony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!