from pyecharts import options as opts
from pyecharts.charts import Pie
defPie_Base():# 商品售卖比列 火车
v1 =['盐田区','罗湖区','福田区','南山区','宝安区','龙岗区','龙华区','光明区','坪山区','大鹏新区']# 深圳各区二手房数量 1265 5406 5598 5488 4206 11055 4089 486 958 343 总38894# 各区二手房占比深圳二手房比例
v2 =[3.3,14,14.4,14,10.8,28,11,1,2.5,1]
c =(
Pie().add("",[list(z)for z inzip(v1, v2)])# 玫瑰图 --- 列表循环处理.set_global_opts(title_opts=opts.TitleOpts(title="深圳市二手房各区占比"), legend_opts=opts.LegendOpts(pos_left="80%")).set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}%")))return c
Pie_Base().render("饼状图.html")
3.2 饼状图效果图
4. 多维散点图
4.1 源码
from pyecharts.charts import Scatter
from pyecharts import options as opts
import pandas as pd
from pyecharts.commons.utils import JsCode
defreadFile():
df = pd.read_csv('深圳二手房.csv', encoding='utf-8')# 给文件添加列名
df.columns =['houseTitle','houseArea','housePos','houseInfo','totalPrice','unitPrice','followInfo']
df['totalPrice']= df['totalPrice'].str.replace('万','').astype("float")
temp = df.groupby(['houseArea'])['totalPrice'].mean().reset_index()
temp =[(row["houseArea"],round(row["totalPrice"],1))for _, row in temp.iterrows()]print(temp)return temp
defScatter_Base():
temp = readFile()
df = pd.DataFrame({"平均房价":[x[1]for x in temp],"小区":[x[0]for x in temp]})# 数据排序
df.sort_values("平均房价", inplace=True, ascending=True)
c =(
Scatter().add_xaxis(df.平均房价.values.tolist())# 传入两个数据链进行组合 通过这个组合返回js回调函数.add_yaxis("平均房价", df[["平均房价","小区"]].values.tolist(),
label_opts=opts.LabelOpts(
formatter=JsCode(# 自定义的js代码 返回一个自定义的标签选项"function(params){return params.value[2];}"))).set_global_opts(
title_opts=opts.TitleOpts(title="深圳二手房平均房价多维散点图"),
xaxis_opts=opts.AxisOpts(
type_="value",# 设置数值类型 连续型
min_=300.0),))return c
Scatter_Base().render("01 - 深圳二手房平均房价多维散点图.html")
4.2 多维散点图效果图
5. 玫瑰图
5.1 源码
from pyecharts.charts import Pie
from pyecharts import options as opts
defPie_RoseType():# 按照对应的年度和季度的数值 通过玫瑰图进行显示
c =(
Pie().add("",[list(z)for z inzip(["201{}年/{}季度".format(y, z)for y inrange(3)for z inrange(1,5)],[4.88,5.88,6.88,7.88,5.88,7.88,9.88,8.88,9.88,5.88,4.88,6.88])],# 内径和外径的设置
radius=["0%","75%"],
rosetype="radius",
label_opts=opts.LabelOpts(is_show=True),).set_global_opts(title_opts=opts.TitleOpts(title="年季度玫瑰图显示"), legend_opts=opts.LegendOpts(pos_left="80%")))return c
Pie_RoseType().render("02 - 玫瑰图.html")
5.2 玫瑰图效果图
6.折线图
6.1源码
from pyecharts import options as opts
from pyecharts.charts import Line
import pandas as pd
defreadFile():
df = pd.read_csv('深圳二手房.csv', encoding='utf-8')# 给文件添加列名
df.columns =['houseTitle','houseArea','housePos','houseInfo','totalPrice','unitPrice','followInfo']
df['totalPrice']= df['totalPrice'].str.replace('万','').astype("float")
temp = df.groupby(['houseArea'])['totalPrice'].mean().reset_index()
temp =[(row["houseArea"],round(row["totalPrice"],1))for _, row in temp.iterrows()]print(temp)return temp
defLineOne():
temp = readFile()
line = Line()
line.add_xaxis([x[0]for x in temp])
line.add_yaxis("平均房价",[x[1]for x in temp]# 设置y轴数据列项)
line.set_global_opts(# x轴的列项名 倾斜 40°显示
xaxis_opts=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(rotate=45),),
yaxis_opts=opts.AxisOpts(name="平均房价(单位/万)"),# y轴的设置名称
title_opts=opts.TitleOpts(title="深圳二手房平均房价折线图"))
line.render("03 - 深圳二手房平均房价折线图.html")
LineOne()