pyecharts结合了Python和百度开源的Echarts工具,基于其交互性和便利性得到了众多开发者的认可。拥有如下的特点:
- 可集成至Flask、Django等主流web框架
- 相较于matplotlib等传统绘图库,pyecharts语法更加简洁,更加注重数据的呈现方式而非图形细节
- 包含原生的百度地图,方便绘制地理可视化图形
pyecharts官网github文档: https://github.com/pyecharts/py
层叠组件
from pyecharts import options as opts
from pyecharts.charts import Bar, Line
from pyecharts.faker import Faker
v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
v3 = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
bar = (
Bar()
.add_xaxis(Faker.months)
.add_yaxis("蒸发量", v1)
.add_yaxis("降水量", v2)
.extend_axis(
# 新增y坐标轴配置项: 因为有三个纵轴数据, 包括蒸发量/降水量(单位是ml), 平均温度(单位是°C)
yaxis=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(formatter="{value} °C"), interval=5
)
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Overlap-bar+line"),
# 设置y坐标轴配置项
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value} ml")),
)
)
# 新增折线图
line = Line().add_xaxis(Faker.months).add_yaxis("平均温度", v3, yaxis_index=1)
# 使用层叠组件组合图形
bar.overlap(line)
bar.render("overlap_bar_line.html")
# 不同类别岗位的学历要求分析
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType
# 职位类别 学历要求
import pandas as pd
df = pd.read_excel("job_data.xlsx")
# 职位类别 学历要求分类聚合
df_op = df.groupby(by=['职位类别', "学历要求"])["学历要求"].count()
# 职位索引
idxs = df_op.index
# 数据整理
content = {}
education_set = set()
for idx in idxs:
line = {}
job = idx[0]
education = idx[1]
counts = df_op[idx]
# 职位作为key,学历作为value
if job in content:
content[job][education] = counts
else:
content[job] = {education: counts}
education_set.add(education)
# 整理成字典格式
for k, v in content.items():
line = []
for ed in education_set:
line.append(v[ed] if ed in v else 0)
content[k] = line
# 整理df格式
df = pd.DataFrame(list(content.values()), columns=list(education_set))
x_lable = content.keys()
content_dic = {}
for i in range(len(list(education_set))):
for lable in x_lable:
data = content[lable]
if list(education_set)[i] in content_dic:
content_dic[list(education_set)[i]].append({"value":int(data[i])})
else:
content_dic[list(education_set)[i]] = [{"value":int(data[i])}]
c = (
# 设置主题: 默认是黑红风格, 其他风格大部分还不如黑红风格好看
Bar(init_opts=opts.InitOpts(height = "500px" , width = "900px"))
# 新增x轴数据, 这里有五列柱状图
.add_xaxis(
list(x_lable)
)
# .add_yaxis(list(education_set)[0], content_dic[list(education_set)[0]], stack="stack1", category_gap="50%")
# .add_yaxis(list(education_set)[1], content_dic[list(education_set)[1]], stack="stack1", category_gap="50%")
# .add_yaxis(list(education_set)[2], content_dic[list(education_set)[2]], stack="stack1", category_gap="50%")
# .add_yaxis(list(education_set)[3], content_dic[list(education_set)[3]], stack="stack1", category_gap="50%")
# .add_yaxis(list(education_set)[4], content_dic[list(education_set)[4]], stack="stack1", category_gap="50%")
.set_global_opts(
# 旋转坐标轴: 解决坐标轴名字过长的问题
title_opts=opts.TitleOpts(title="不同类别岗位的学历要求分析", subtitle="Bar-分布图"),
xaxis_opts=opts.AxisOpts(name_rotate=-11, axislabel_opts={"rotate": 90}),
datazoom_opts=opts.DataZoomOpts(pos_top=800, pos_bottom = 60),
)
)
for lable in education_set:
# 参数一: 系列名称; 参数二: 系列数据; stack: 数据堆叠; category_gap: 柱间距离
c.add_yaxis(lable, y_axis=content_dic[lable], stack="stack1", category_gap="10%")
c.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
c.render_notebook()
参考 :