如何让仪表盘结论先行
# 导入工具包
import pandas as pd
import numpy as np
import pandas_bokeh
# 导入工具包
import pandas as pd
import numpy as np
import pandas_bokeh
data = pd.read_excel('./data/青岛短租数据整理.xlsx',index_col=0)
data = pd.read_excel('./data/青岛短租数据整理.xlsx',index_col=0)
data.head()
result1 = data.groupby('房间数').agg({'价格':'mean','名称':'count'}).round(0)
result1.columns=['均价','房源数量']
result1.head()
data.价格.describe()
p1 = result1[['均价']].plot_bokeh.bar(title='不同房间数均价',
zooming=False,
panning=False,
toolbar_location=None,
color='#8ac4d0',
legend=None,
show_figure=False,
)
p2 = result1[['房源数量']].plot_bokeh.pie(title='不同房间数房源数量',
zooming=False,
panning=False,
toolbar_location=None,
colormap=['#8ac4d0','#28527a','#f4d160'],
show_figure=False,
)
p3 = data[['价格']].plot_bokeh.hist(title='价格分布',
xlim=(0,1200), # x轴的刻度
bins=np.linspace(0, 1200, 25),
zooming=False,
panning=False,
legend=None,
toolbar_location=None,
color='#8ac4d0',
show_figure=False,
alpha=1,
)
data["size"] = data["价格"] / 50
p4 = data.plot_bokeh.map(x="经度_re",
y="纬度_re",
hovertool_string="""<h2> @{名称} </h2>
<h3> 价格: @{价格} </h3>""",
tile_provider="CARTODBPOSITRON",
size="size",
show_colorbar=True,
color='#8ac4d0',
legend=None,
title="房源分布",
toolbar_location=None,
show_figure=False,
)
组合
p1.plot_width = 300
p2.plot_width = 300
p3.plot_width = 1100
p4.plot_width = 1100
p1.axis.axis_line_color=None
p1.axis.minor_tick_line_color=None
p1.axis.major_tick_line_color=None
p3.axis.axis_line_color=None
p3.axis.minor_tick_line_color=None
p3.axis.major_tick_line_color=None
p4.axis.axis_line_color=None
p4.axis.minor_tick_line_color=None
p4.axis.major_tick_line_color=None
p4.axis.major_label_text_color= None
layout = pandas_bokeh.column(pandas_bokeh.row(p1,p3),
pandas_bokeh.row(p2,p4))
pandas_bokeh.output_file('青岛短租分布V1.html')
pandas_bokeh.show(layout)
添加结论
from bs4 import BeautifulSoup
soup = BeautifulSoup(open('青岛短租分布V1.html'),'lxml')
# 结论内容
bt = '青岛短租价格分析'
jl1 = '青岛短租均价300元, 主要集中在150~300元之间,'
jl2 = '1. 房间数量为1的房源最多'
jl3 = '2. 主要集中在沿海风景较好的地段'
jl4 = '数据源: 短租网; 更新日期: 2021-3-12'
html = """
<h3>{0}</h3>
<p style="color:#8ac4d0;line-height: 0em">{1}</p>
<p style="font-size:8px";>{2}</p>
<p style="font-size:8px";>{3}</p>
<p style="font-size:6px;font-family:楷体">{4}</p>
""".format(bt,jl1,jl2,jl3,jl4) + str(soup)
# 字体样式更多设置 font-family:Calibri 或者 '"微软雅黑"' 字体 color:red 颜色 font-weight: bold
# text-indent:2em 缩进 line-height: 2em 行间距;text-align:center 字体居中
# 保存数据
with open("青岛短租分布V2.html" , "w",encoding="utf-8") as f:
f.write(html)
# 预览文件
import webbrowser
webbrowser.open("青岛短租分布V2.html")
扫码关注微信, 赠送《pandas数据读取与清洗》视频及课程代码!