如何使用Bokeh生成气泡图

4
我希望使用Bokeh创建一个气泡图,基于分类的x轴和y轴,并使用计数作为它们的大小。
这是我拥有的数据框,并且我已经成功地用Seaborn创建了它:

enter image description here

这是我创建的 Seaborn 的简短版本

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

我希望了解如何使用Bokeh实现相同的事情。提前感谢你。 已解决 感谢回答者。我已经成功生成了一张完全符合我的要求的图表。我稍微调整了一下以适应应用程序,具体如下:
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}

请解释“分类的x和y轴”,在您的图中,y轴(“成绩”)是数字。 - Shijith
@Shijith,我很抱歉,我已经相应地修复了我的代码。基本上,x和y是分类数据。而“count”包含数字数据,用于确定大小。 - N. Arunoprayoch
1个回答

4

以下是在 Bokeh v1.0.4 中执行相同操作的方法。可以使用终端运行: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)

结果:

这里输入图像描述


该图片显示了一个网页截图。

非常感谢,这正是我一直在寻找的。我稍微调整了一下以适应我的应用程序。它完美地工作了。 - N. Arunoprayoch
先生,是否可以为每个气泡中的“count”精确值添加悬停效果?例如工具提示 ='$ count'? - N. Arunoprayoch
我完成了,先生。tooltips='@count' - N. Arunoprayoch
请找到已更正的代码(缩放应仅应用于圆圈大小,而不是右侧的 ColorBar)。 - Tony
size_scaled = list(map(lambda i: i/10, size)) 我创建了这个并按建议添加了一个新列。一切看起来都很好。再次感谢。 - N. Arunoprayoch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接