pyecharts

9.0 安装 Pyechart

1
pip3 install pyecharts

案例:

https://gallery.pyecharts.org/

9.1 生成柱形图

1
2
3
4
5
6
7
8
9
from pyecharts.charts import Bar
from pyecharts import options as opts

bar = Bar()
bar.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
bar.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
bar.set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况"))
bar.render_notebook()

9.1.1 把生成的图标转成图片

1
pip3 install snapshot-selenium
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line
from pyecharts.faker import Faker

bar = Bar()
bar.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
bar.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
bar.set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况"))
bar.render_notebook()



# 导入输出图片工具
from pyecharts.render import make_snapshot
# 使用snapshot-selenium 渲染图片
from snapshot_selenium import snapshot

make_snapshot(snapshot, bar.render(), "./bar.png")

9.2 生成条形图

1
2
3
4
5
6
7
8
9
10
from pyecharts.charts import Bar
from pyecharts import options as opts

bar = Bar()
bar.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
bar.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
bar.reversal_axis()
bar.set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况"))
bar.render_notebook()

9.3 生成饼图

1
2
3
4
5
6
7
8
9
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker

pie = Pie()
pie.add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
pie.set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
pie.render_notebook()

9.4 生成折线图

1
2
3
4
5
6
7
8
9
10
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.faker import Faker

line = Line()
line.add_xaxis(Faker.choose())
line.add_yaxis("商家A", Faker.values())
line.add_yaxis("商家B", Faker.values())
line.set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例"))
line.render_notebook()

9.5 生成散点图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import pyecharts.options as opts
from pyecharts.charts import Scatter

data = [
[10.0, 8.04],
[8.0, 6.95],
[13.0, 7.58],
[9.0, 8.81],
[11.0, 8.33],
[14.0, 9.96],
[6.0, 7.24],
[4.0, 4.26],
[12.0, 10.84],
[7.0, 4.82],
[5.0, 5.68],
]
data.sort(key=lambda x: x[0])
x_data = [d[0] for d in data]
y_data = [d[1] for d in data]

scatter = Scatter(init_opts=opts.InitOpts(width="1600px", height="1000px"))
scatter.add_xaxis(xaxis_data=x_data)
scatter.add_yaxis(
series_name="",
y_axis=y_data,
symbol_size=20,
label_opts=opts.LabelOpts(is_show=False),
)
scatter.set_series_opts()
scatter.set_global_opts(
xaxis_opts=opts.AxisOpts(
type_="value", splitline_opts=opts.SplitLineOpts(is_show=True)
),
yaxis_opts=opts.AxisOpts(
type_="value",
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
tooltip_opts=opts.TooltipOpts(is_show=False),
)
scatter.render_notebook()

9.6 生成气泡图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from pyecharts import options as opts
from pyecharts.charts import Scatter
from pyecharts.faker import Faker

scatter = Scatter()
scatter.add_xaxis(Faker.choose())
scatter.add_yaxis("商家A", Faker.values())
scatter.set_global_opts(
title_opts=opts.TitleOpts(title="Scatter-显示分割线"),
xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
visualmap_opts=opts.VisualMapOpts(type_="size", max_=150, min_=20),
)
scatter.render_notebook()

9.7 生成雷达图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import pyecharts.options as opts
from pyecharts.charts import Radar

v1 = [[4300, 10000, 28000, 35000, 50000, 19000]]
v2 = [[5000, 14000, 28000, 31000, 42000, 21000]]
radar = Radar(init_opts=opts.InitOpts(width="1280px", height="720px", bg_color="#CCCCCC"))
radar.add_schema(
schema=[
opts.RadarIndicatorItem(name="销售(sales)", max_=6500),
opts.RadarIndicatorItem(name="管理(Administration)", max_=16000),
opts.RadarIndicatorItem(name="信息技术(Information Technology)", max_=30000),
opts.RadarIndicatorItem(name="客服(Customer Support)", max_=38000),
opts.RadarIndicatorItem(name="研发(Development)", max_=52000),
opts.RadarIndicatorItem(name="市场(Marketing)", max_=25000),
],
splitarea_opt=opts.SplitAreaOpts(
is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
),
textstyle_opts=opts.TextStyleOpts(color="#fff"),
)
radar.add(
series_name="预算分配(Allocated Budget)",
data=v1,
linestyle_opts=opts.LineStyleOpts(color="#CD0000"),
)
radar.add(
series_name="实际开销(Actual Spending)",
data=v2,
linestyle_opts=opts.LineStyleOpts(color="#5CACEE"),
)
radar.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
radar.set_global_opts(
title_opts=opts.TitleOpts(title="基础雷达图"), legend_opts=opts.LegendOpts()
)
radar.render_notebook()

9.8 生成面积图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import pyecharts.options as opts
from pyecharts.charts import Line

x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
y_data = [820, 932, 901, 934, 1290, 1330, 1320]


line = Line()
line.add_xaxis(xaxis_data=x_data)
line.add_yaxis(
series_name="",
y_axis=y_data,
symbol="emptyCircle",
is_symbol_show=True,
label_opts=opts.LabelOpts(is_show=False),
areastyle_opts=opts.AreaStyleOpts(opacity=1, color="#C67570"),
)
line.set_global_opts(
tooltip_opts=opts.TooltipOpts(is_show=False),
yaxis_opts=opts.AxisOpts(
type_="value",
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
)
# 设置 boundary_gap 的时候一定要放在最后一个配置项里, 不然会被覆盖
line.render_notebook()

9.9 生成仪表板图

1
2
3
4
5
6
7
from pyecharts import options as opts
from pyecharts.charts import Gauge

gauge = Gauge()
gauge.add("", [("完成率", 66.6)])
gauge.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-基本示例"))
gauge.render_notebook()

9.10 生成词云图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import pyecharts.options as opts
from pyecharts.charts import WordCloud


data = [
("生活资源", "999"),
("供热管理", "888"),
("供气质量", "777"),
("生活用水管理", "688"),
("一次供水问题", "588"),
("交通运输", "516"),
("城市交通", "515"),
("环境保护", "483"),
("房地产管理", "462"),
("城乡建设", "449"),
("社会保障与福利", "429"),
("社会保障", "407"),
("文体与教育管理", "406"),
("公共安全", "406"),
("公交运输管理", "386"),
("出租车运营管理", "385"),
("供热管理", "375"),
("市容环卫", "355"),
("自然资源管理", "355"),
("粉尘污染", "335"),
("噪声污染", "324"),
("土地资源管理", "304"),
("物业服务与管理", "304"),
("医疗卫生", "284"),
("粉煤灰污染", "284"),
("占道", "284"),
("供热发展", "254"),
("农村土地规划管理", "254"),
("生活噪音", "253"),
("供热单位影响", "253"),
("城市供电", "223"),
("房屋质量与安全", "223"),
("大气污染", "223"),
("房屋安全", "223"),
("文化活动", "223"),
("拆迁管理", "223"),
("公共设施", "223"),
("供气质量", "223"),
("供电管理", "223"),
("燃气管理", "152"),
("教育管理", "152"),
("医疗纠纷", "152"),
("执法监督", "152"),
("设备安全", "152"),
("政务建设", "152"),
("县区、开发区", "152"),
("宏观经济", "152"),
("教育管理", "112"),
("社会保障", "112"),
("生活用水管理", "112"),
("物业服务与管理", "112"),
("分类列表", "112"),
("农业生产", "112"),
("二次供水问题", "112"),
("城市公共设施", "92"),
("拆迁政策咨询", "92"),
("物业服务", "92"),
("物业管理", "92"),
("社会保障保险管理", "92"),
("低保管理", "92"),
("文娱市场管理", "72"),
("城市交通秩序管理", "72"),
("执法争议", "72"),
("商业烟尘污染", "72"),
("占道堆放", "71"),
("地上设施", "71"),
("水质", "71"),
("无水", "71"),
("供热单位影响", "71"),
("人行道管理", "71"),
("主网原因", "71"),
("集中供热", "71"),
("客运管理", "71"),
("国有公交(大巴)管理", "71"),
("工业粉尘污染", "71"),
("治安案件", "71"),
("压力容器安全", "71"),
("身份证管理", "71"),
("群众健身", "41"),
("工业排放污染", "41"),
("破坏森林资源", "41"),
("市场收费", "41"),
("生产资金", "41"),
("生产噪声", "41"),
("农村低保", "41"),
("劳动争议", "41"),
("劳动合同争议", "41"),
("劳动报酬与福利", "41"),
("医疗事故", "21"),
("停供", "21"),
("基础教育", "21"),
("职业教育", "21"),
("物业资质管理", "21"),
("拆迁补偿", "21"),
("设施维护", "21"),
("市场外溢", "11"),
("占道经营", "11"),
("树木管理", "11"),
("农村基础设施", "11"),
("无水", "11"),
("供气质量", "11"),
("停气", "11"),
("市政府工作部门(含部门管理机构、直属单位)", "11"),
("燃气管理", "11"),
("市容环卫", "11"),
("新闻传媒", "11"),
("人才招聘", "11"),
("市场环境", "11"),
("行政事业收费", "11"),
("食品安全与卫生", "11"),
("城市交通", "11"),
("房地产开发", "11"),
("房屋配套问题", "11"),
("物业服务", "11"),
("物业管理", "11"),
("占道", "11"),
("园林绿化", "11"),
("户籍管理及身份证", "11"),
("公交运输管理", "11"),
("公路(水路)交通", "11"),
("房屋与图纸不符", "11"),
("有线电视", "11"),
("社会治安", "11"),
("林业资源", "11"),
("其他行政事业收费", "11"),
("经营性收费", "11"),
("食品安全与卫生", "11"),
("体育活动", "11"),
("有线电视安装及调试维护", "11"),
("低保管理", "11"),
("劳动争议", "11"),
("社会福利及事务", "11"),
("一次供水问题", "11"),
]


word_cloud = WordCloud()
word_cloud.add(series_name="热点分析", data_pair=data, word_size_range=[6, 66])
word_cloud.set_global_opts(
title_opts=opts.TitleOpts(
title="热点分析", title_textstyle_opts=opts.TextStyleOpts(font_size=23)
),
tooltip_opts=opts.TooltipOpts(is_show=True),
)
word_cloud.render_notebook()

##9.11 生成组合图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line
from pyecharts.faker import Faker

bar = Bar()
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A", Faker.values())
bar.add_yaxis("商家B", Faker.values())
bar.set_global_opts(title_opts=opts.TitleOpts(title="Grid-Bar"))

line = Line()
line.add_xaxis(Faker.choose())
line.add_yaxis("商家A", Faker.values())
line.add_yaxis("商家B", Faker.values())
line.set_global_opts(
title_opts=opts.TitleOpts(title="Grid-Line", pos_top="48%"),
legend_opts=opts.LegendOpts(pos_top="48%"),
)

grid = Grid()
grid.add(bar, grid_opts=opts.GridOpts(pos_bottom="60%"))
grid.add(line, grid_opts=opts.GridOpts(pos_top="60%"))
grid.render_notebook()

视频课程

西瓜视频 https://www.ixigua.com/6851086471017792004/