3

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}
2
  • please explain 'categorical x and y axis' ,in your plot itself, the y axis('Grade') is numerical
    – Shijith
    Apr 8, 2019 at 7:12
  • @Shijith My apology, I have fixed my code accordingly. Basically, x and y are categorical. And 'count' contains numeral data for determining sizes. Apr 8, 2019 at 7:25

1 Answer 1

3

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

5
  • Thank you so much, this is exactly what I have been looking for. I have adjusted it a bit for my application. It works perfectly. Apr 8, 2019 at 8:59
  • Is that possible to add hover for an exact value of 'count' in each bubble, sir? Like tooltips='$count'? Apr 8, 2019 at 9:14
  • I did it, sir. tooltips='@count' Apr 8, 2019 at 9:34
  • Please find corrected code (the scaling should be applied only to the circle size not to the ColorBar to the right)
    – Tony
    Apr 8, 2019 at 9:40
  • size_scaled = list(map(lambda i: i/10, size)) I created this and added a new column as suggested. Everything seems good. Thank you again. Apr 8, 2019 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.