用Python pyecharts v1.x 绘制图形(一):柱状图、柱状堆叠图、条形图、直方图、饼图、圆环图、玫瑰图

关于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: [email protected]
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 <= xlen / 2:
        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 = (xlen + 1 - idx) * 10,
                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')

饼图

在这里插入图片描述

# 饼图
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

发布了46 篇原创文章 · 获赞 13 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/shineych/article/details/104204330