关于pyecharts
pyecharts是一个用于生成echart(百度开源的数据可视化javascript库)图表的类库。
pyecharts 分为 v0.5.x 和 v1.x 两个大版本,版本不兼容,本篇所有的案例基于v1.6.2。
C:\Users\XXX>pip show pyecharts
Name: pyecharts
Version: 1.6.2
Summary: Python options, make charting easier
Home-page: https://github.com/pyecharts/pyecharts
Author: chenjiandongx
Author-email: chenjiandongx@qq.com
License: MIT
Location: c:\users\xxx\appdata\local\programs\python\python38\lib\site-packages
Requires: simplejson, jinja2, prettytable
Required-by:
柱状图
# 柱状图
import random
import pyecharts.options as opts
from pyecharts.charts import Bar
x_vals = ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
bar = (
Bar()
.add_xaxis(x_vals)
.add_yaxis('商家A', [random.randint(10, 100) for _ in range(6)])
.add_yaxis('商家B', [random.randint(10, 100) for _ in range(6)])
.add_yaxis('商家C', [random.randint(10, 100) for _ in range(6)])
.add_yaxis('商家D', [random.randint(10, 100) for _ in range(6)])
.set_series_opts(label_opts=opts.LabelOpts(is_show=True, font_size=14),
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(y=40, name="达标线=40")]))
.set_global_opts(title_opts=opts.TitleOpts(title='柱状图示例-销量', subtitle='四个商家'),
xaxis_opts=opts.AxisOpts(name='商品'),
yaxis_opts=opts.AxisOpts(name='单位:件'))
)
bar.render('柱状图.html')
堆叠柱状图
# 柱状堆叠图
import pyecharts.options as opts
from pyecharts.charts import Bar
goods = ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
bar = (
Bar()
.add_xaxis(goods)
.add_yaxis('商家A', [random.randint(10, 100) for _ in range(6)], stack='stack1')
.add_yaxis('商家B', [random.randint(10, 100) for _ in range(6)], stack='stack1')
.add_yaxis('商家C', [random.randint(10, 100) for _ in range(6)], stack='stack1')
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title='柱状堆叠图示例-商品销量'),
xaxis_opts=opts.AxisOpts(name='品类'),
yaxis_opts=opts.AxisOpts(name='销量(单位:件)'))
)
bar.render('柱状堆叠图.html')
条形图
# 条形图
x_vals1 = ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
x_vals2 = ['POLO', '篮球鞋', '羽绒服', '皮鞋', '领带', '睡衣']
x_vals3 = ['羽毛球服', '羽毛球鞋', '护腕', '护膝', '护踝', '毛巾']
y_vals = [random.randint(10, 100) for _ in range(18)]
bar = Bar().add_xaxis(x_vals1 + x_vals2 + x_vals3)
bar.add_yaxis('商家A', y_vals,
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_='average'),
opts.MarkPointItem(type_='max'),
opts.MarkPointItem(type_='min')],
symbol_size=80)
)
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position='right'))
bar.set_global_opts(title_opts=opts.TitleOpts(title='条形图示例-商品销量', subtitle='条目较多条形图比较好看点'))
bar.reversal_axis() #翻转XY轴,将柱状图转换为条形图
bar.render('条形图.html')
直方图
# 直方图
# 直方图
import random
import pyecharts.options as opts
from pyecharts.charts import Bar
x_vals = ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
xlen = len(x_vals)
# 设置成两种颜色
y_vals = []
for idx, item in enumerate(x_vals):
if idx % 2 == 0:
y_vals.append(
opts.BarItem(
name = item,
value = random.randint(10, 100),
itemstyle_opts = opts.ItemStyleOpts(color="#749f83"),
)
)
else:
y_vals.append(
opts.BarItem(
name = item,
value = random.randint(10, 100),
itemstyle_opts = opts.ItemStyleOpts(color="#d48265"),
)
)
bar_histogram = (
Bar()
.add_xaxis(x_vals)
.add_yaxis('商家A', y_vals, category_gap=0)
# .add_yaxis('商家A', [random.randint(10, 100) for _ in range(6)], category_gap=0)
.set_series_opts(label_opts=opts.LabelOpts(is_show=True, font_size=14))
.set_global_opts(title_opts=opts.TitleOpts(title='直方图示例-选择赠品', subtitle=''),
xaxis_opts=opts.AxisOpts(name='赠品类型'),
yaxis_opts=opts.AxisOpts(name='选择相应赠品的人数'))
)
bar_histogram.render('直方图.html')
帕累托图(复合图)
帕累托图用户分析定类数据,是排序的直方图
# 帕累托图--# 左边纵坐标表示频数,右边纵坐标表示频率.分析线表示累积频率
import random
from pyecharts import options as opts
from pyecharts.charts import Bar, Line
import pandas as pd
# 随机颜色, from faker
def rand_color() -> str:
return random.choice(
[
"#c23531",
"#2f4554",
"#61a0a8",
"#d48265",
"#749f83",
"#ca8622",
"#bda29a",
"#6e7074",
"#546570",
"#c4ccd3",
"#f05b72",
"#444693",
"#726930",
"#b2d235",
"#6d8346",
"#ac6767",
"#1d953f",
"#6950a1",
]
)
df_origin = pd.DataFrame({'categories':['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],'sales': [random.randint(10, 100) for _ in range(6)]})
print(df_origin)
# 按销量降序排列
df_sorted = df_origin.sort_values(by='sales' , ascending=False)
print(df_sorted)
# 折线图x轴
x_line_categories = [*range(7)]
# 折线图y轴--向下累积频率
cum_percent = df_sorted['sales'].cumsum() / df_sorted['sales'].sum() * 100
cum_percent = cum_percent.append(pd.Series([0])) # 添加起始频率0
cum_percent = cum_percent.sort_values(ascending=True)
print(df_sorted.categories.values.tolist())
print(cum_percent.values.tolist())
def pareto_bar() -> Bar:
line = (
Line()
.add_xaxis(x_line_categories)
.add_yaxis("累计百分比",
cum_percent.values.tolist(),
xaxis_index=1,
yaxis_index=1, # 使用次y坐标轴,即bar中的extend_axis
label_opts=opts.LabelOpts(is_show=False),
is_smooth=True,
)
)
bar = (
Bar()
.add_xaxis(df_sorted.categories.values.tolist())
.add_yaxis('销售额', df_sorted.sales.values.tolist(), category_gap=0)
# .add_yaxis('总额百分比', cum_percent.values.tolist())
.extend_axis(xaxis=opts.AxisOpts(is_show=False, position='top'))
.extend_axis(yaxis=opts.AxisOpts(axistick_opts=opts.AxisTickOpts(is_inside=True), # 刻度尺朝内
axislabel_opts=opts.LabelOpts(formatter='{value}%'), position='right') )
.set_series_opts(label_opts=opts.LabelOpts(is_show=True, font_size=14))
.set_global_opts(title_opts=opts.TitleOpts(title='帕累托图示例-销售额', subtitle=''),
xaxis_opts=opts.AxisOpts(name='商品类型', type_='category'),
yaxis_opts=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(formatter="{value} 件")
)
)
)
bar.overlap(line)
return bar
pareto_bar().render('帕累托图.html')
饼图
# 饼图
from pyecharts import options as opts
from pyecharts.charts import Page, Pie
pie = (
Pie()
.add('鼠标选中分区后的tip',
[list(z) for z in zip(['20{}年第{}季'.format(year,season)
for year in [19, 20] # count 2
for season in range(1,5)] # count 2
,[random.randint(2, 10) for _ in range(8)])]) # count 8
.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}: {c}万套'))
.set_global_opts(title_opts=opts.TitleOpts(title='饼图实例-近两年季度销售'),
legend_opts=opts.LegendOpts(is_show=False))
)
pie.render('饼图.html')
圆环图
from pyecharts.charts import Pie
pie = (
Pie()
.add(
'鼠标选中分区后的tip',
[list(z) for z in zip(['20{}年第{}季'.format(year,season)
for year in [19, 20] # count 2
for season in range(1,5)] # count 2
,[random.randint(2, 10) for _ in range(8)])],
radius=['50%', '75%'], #设置内径外径
label_opts=opts.LabelOpts(is_show=True)
)
.set_global_opts(title_opts=opts.TitleOpts(title='圆环图示例-近两年季度销售'),
legend_opts=opts.LegendOpts(is_show=False))
)
pie.render('圆环图.html')
玫瑰图
# 玫瑰图
from pyecharts.charts import Pie
pie = (
Pie()
.add(
'鼠标选中分区后的tip',
[list(z) for z in zip(['20{}年第{}季'.format(year,season)
for year in [19, 20] # count 2
for season in range(1,5)] # count 2
,[random.randint(0, 10) for _ in range(8)])],
radius=['10%', '75%'], #设置内径外径
# rosetype='radius' 圆心角展现数据百分比,半径展现数据大小
# rosetype='area' 圆心角相同,为通过半径展现数据大小
rosetype='radius',
label_opts=opts.LabelOpts(is_show=True)
)
.set_global_opts(title_opts=opts.TitleOpts(title='玫瑰图示例-近两年季度销售'),
legend_opts=opts.LegendOpts(is_show=False))
)
pie.render('玫瑰图.html')
下一节
以OJ分析为例,编写折线图、散点图、箱线图、雷达图、词云图
注:建议有空的话可以去学习官方的demo :https://github.com/pyecharts/pyecharts/tree/master/example