如何使用散景生成气泡图

时间:2019-04-08 06:40:00

标签: python seaborn bokeh

我想使用Bokeh根据分类的x轴和y轴创建气泡图,并使用count作为其大小。

这是我拥有的数据框,并且我是用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


我想知道如何通过使用散景来实现相同的目的。预先谢谢你。


已解决

感谢响应者。我已经设法生成了我想要的图形。我已经对其进行了一些调整,以适应以下应用:

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}

1 个答案:

答案 0 :(得分:0)

这是在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)

结果:

enter image description here