美文网首页
实战|学会使用Boken实现数据可视化(五)

实战|学会使用Boken实现数据可视化(五)

作者: python与数据分析 | 来源:发表于2021-01-05 15:17 被阅读0次

【业务场景】分析各个城市餐饮行业情况,要求制作一个选项卡,实现联动分析。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore') 
from bokeh.io import output_notebook, show, curdoc
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Select
from bokeh.layouts import row
import matplotlib as mpl
import matplotlib.dates as mdate
import datetime as dt
mpl.rcParams['font.family'] = 'SimHei'

data=pd.read_csv('./foods.csv',encoding='gbk')
#------------------------------------------------------------------
# 创建下拉小部件: select
city = list(data['city'].unique())
select1=Select(options=city,value='北京')
data_leixing=data[data.city == '北京']
data_leixing_a = data_leixing.groupby('type').size().sort_values(ascending=False).head(10)
data_leixing_b=pd.DataFrame(data=data_leixing_a,columns=['shuliang'])
data_leixing_b['ind']=data_leixing_b.index
# 创建数据源: source
source1 = ColumnDataSource(data={
    'x': data_leixing_b['ind'],
    'y': data_leixing_b['shuliang']
})
TOOLTIPS = [
    ("城市", "@x"),
    ("数量", " @y")
]
p1 = figure(title='餐饮业的统计图',x_range=data_leixing_a.index.to_list(),plot_width = 620, plot_height = 500,
                  x_axis_label = '城市', y_axis_label = '数量',tooltips=TOOLTIPS)
p1.vbar('x', width=0.5, bottom=0, top='y',source=source1, color='#BCEE68')
# 定义回调函数: update_plot
def update_plot1(attr, old, new):
    yr = select1.value
    data_leixing=data[data.city == yr]
    data_leixing_a = data_leixing.groupby('type').size().sort_values(ascending=False).head(10)
    data_leixing_b = pd.DataFrame(data=data_leixing_a, columns=['shuliang'])
    data_leixing_b['ind'] = data_leixing_b.index
    source1.data={
            'x': data_leixing_b['ind'],
            'y': data_leixing_b['shuliang']
        }
    p1.title.text = '%s餐饮业统计图' % yr
# update_plot 回调附加到 select 的 'value' 属性
select1.on_change('value', update_plot1)
# 创建布局并添加到当前文档
layout1 = row(select1,p1)
#-------------------------------------------------------------
# 创建下拉小部件: select
types = list(data['type'].unique())
select2=Select(options=types, value='西餐')
data_qudao=data[data.type == '西餐']
data_qudao_a = data_qudao.groupby('city').size().sort_values(ascending=False).head(10)
data_qudao_b=pd.DataFrame(data=data_qudao_a,columns=['shuliang'])
data_qudao_b['ind']=data_qudao_b.index
# 创建数据源: source
source2 = ColumnDataSource(data={
    'x': data_qudao_b['ind'],
    'y': data_qudao_b['shuliang']
})
TOOLTIPS = [
    ("类型", "@x"),
    ("数量", " @y")
]
p2 = figure(title='餐饮类型统计图',x_range=data_qudao_a.index.to_list(),plot_width = 620, plot_height = 500,
                  x_axis_label = '类型', y_axis_label = '数量',tooltips=TOOLTIPS)

p2.vbar('x', width=0.5, bottom=0, top='y',source=source2, color='#BCD2EE')
# show(p2)
# 定义回调函数: update_plot
def update_plot2(attr, old, new):
    yr = select2.value
    data_qudao=data[data.type == yr]
    data_qudao_a = data_qudao.groupby('city').size().sort_values(ascending=False).head(10)
    data_qudao_b = pd.DataFrame(data=data_qudao_a, columns=['shuliang'])
    data_qudao_b['ind'] = data_qudao_b.index
    source2.data={
            'x': data_qudao_b['ind'],
            'y': data_qudao_b['shuliang']
        }
    p2.title.text = '%s类型统计图' % yr
select2.on_change('value', update_plot2)
layout2 = row(select2,p2)

from bokeh.models.widgets import Panel
tab1 = Panel(child=layout1, title='城市')
tab2 = Panel(child=layout2,title='类型')
from bokeh.models.widgets import Tabs
layout = Tabs(tabs=[tab1,tab2])
curdoc().add_root(layout)

下面我们启动bokeh服务:

bokeh serve --show aa.py

希望本文的内容对大家的学习或者工作能带来一定的帮助,每天进步一点点,加油。

相关文章

网友评论

      本文标题:实战|学会使用Boken实现数据可视化(五)

      本文链接:https://www.haomeiwen.com/subject/lpwvoktx.html