案例1

# -*- coding: utf-8 -*-
"""
Created on Mon Feb  3 17:17:42 2020

@author: Administrator
"""


'''
01 读取数据 ;;;
'''

df1 = pd.read_excel("C:\Users\Administrator\Desktop\上海餐饮数据.xlsx")
df1_length = len(df1)
df1_columns = df1.columns.tolist()
print('数据量为%i条' % len(df1))
print(df1.head())

'''
02 清洗数据 ;;;
'''
# 筛选数据,清除空值、为0的数据
data1 = df1[['类别','口味','环境','服务','人均消费']]
data1.dropna(inplace = True)
data1 = data1[(data1['口味']>0)&(data1['人均消费']>0)]

# 计算性价比指数
data1['性价比'] = (data1['口味'] + data1['环境'] + data1['服务']) / data1['人均消费']

# 查看异常值
fig,axes = plt.subplots(1,3,figsize = (10,4))
data1.boxplot(column=['口味'],ax = axes[0])
data1.boxplot(column=['人均消费'],ax = axes[1])
data1.boxplot(column=['性价比'],ax = axes[2])

# 创建函数1→ 删除异常值
def f1(data,col):
    q1 = data[col].quantile(q = 0.25)
    q3 = data[col].quantile(q = 0.75) 
    iqr = q3-q1
    t1 = q1 - 3 * iqr
    t2 = q3 + 3 * iqr
    return data[(data[col] > t1)&(data[col]<t2)][['类别',col]]

# 数据异常值处理
data_kw = f1(data1,'口味')
data_rj = f1(data1,'人均消费')
data_xjb = f1(data1,'性价比')

'''
03 数据处理
'''

# 创建函数2 → 标准化指标并排序
def f2(data,col):
    col_name = col + '_norm'
    data_gp = data.groupby('类别').mean()
    data_gp[col_name] = (data_gp[col] - data_gp[col].min())/(data_gp[col].max()-data_gp[col].min())
    data_gp.sort_values(by = col_name, inplace = True, ascending=False)
    return data_gp

# 指标标准化得分
data_kw_score = f2(data_kw,'口味')
data_rj_score = f2(data_rj,'人均消费')
data_xjb_score = f2(data_xjb,'性价比')


# 合并数据
data_final_q1 = pd.merge(data_kw_score,data_rj_score,left_index=True,right_index=True)    # 合并口味、人均消费指标得分
data_final_q1 = pd.merge(data_final_q1,data_xjb_score,left_index=True,right_index=True)       # 合并性价比指标得分


data_final_q1.head()


'''
04 绘制图形
'''
# 制作散点图、柱状图
# x轴为“人均消费”,y轴为“性价比得分”,点的大小为“口味得分”
from bokeh.models import HoverTool
from bokeh.palettes import brewer
from bokeh.models.annotations import BoxAnnotation
from bokeh.layouts import gridplot
# 导入模块

 # 添加size字段
data_final_q1['size'] = data_final_q1['口味_norm'] * 40 
data_final_q1.index.name = 'type'
data_final_q1.columns = ['kw','kw_norm','price','price_norm','xjb','xjb_norm','size']
# 将中文改为英文
# 添加颜色参数

# 创建ColumnDataSource数据
source = ColumnDataSource(data_final_q1)


# 设置标签显示内容
hover = HoverTool(tooltips=[("餐饮类型", "@type"),
                            ("人均消费", "@price"),
                            ("性价比得分", "@xjb_norm"),
                            ("口味得分", "@kw_norm")
                           ]) 
    
# 构建绘图空间  散点图    
result = figure(plot_width=800, plot_height=250,
                title="餐饮类型得分情况" ,
                x_axis_label = '人均消费', y_axis_label = '性价比得分', 
                tools=[hover,'box_select,reset,xwheel_zoom,pan,crosshair']) 


result.circle(x = 'price',y = 'xjb_norm',source = source,
         line_color = 'black',line_dash = [6,4],fill_alpha = 0.6,
        size = 'size')

# 设置人均消费中间价位区间
price_mid = BoxAnnotation(left=40,right=80, fill_alpha=0.1, fill_color='navy')   
result.add_layout(price_mid)

result.title.text_font_style = "bold"
result.ygrid.grid_line_dash = [6, 4]
result.xgrid.grid_line_dash = [6, 4]


# 绘制柱状图
data_type = data_final_q1.index.tolist()# 提取横坐标

kw = figure(plot_width=800, plot_height=250, title='口味得分',x_range=data_type,
           tools=[hover,'box_select,reset,xwheel_zoom,pan,crosshair'])
kw.vbar(x='type', top='kw_norm', source=source,width=0.9, alpha = 0.8,color = 'red')   
kw.ygrid.grid_line_dash = [6, 4]
kw.xgrid.grid_line_dash = [6, 4]
# 柱状图1

price = figure(plot_width=800, plot_height=250, title='人均消费得分',x_range=kw.x_range,
              tools=[hover,'box_select,reset,xwheel_zoom,pan,crosshair'])
price.vbar(x='type', top='price_norm', source=source,width=0.9, alpha = 0.8,color = 'green') 
price.ygrid.grid_line_dash = [6, 4]
price.xgrid.grid_line_dash = [6, 4]
# 柱状图2
    
p = gridplot([[result],[kw], [price]])
# 组合图表
show(p)

  

原文地址:https://www.cnblogs.com/hero799/p/12256529.html