• Pyecharts绘图笔记



    官网:https://pyecharts.org/#/zh-cn/intro

    柱状图

    代码

    from pyecharts.charts import Bar,Line,Grid
    from pyecharts import options as opts
    
    x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
    y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
    
    bar = (
        Bar(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add_xaxis(x)
        .add_yaxis('test1',y1)
        .add_yaxis('test2',y2)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test',pos_left='center'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_right='1px'),
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
        #滑动
        datazoom_opts=opts.DataZoomOpts(is_show=True, type_="slider",range_start=20,range_end=60)) #图例位置
        )
    
    
    bar.render('test.html')
    
    • 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

    在这里插入图片描述
    opts.InitOpts是设置图大小,set_global_opts设置全局变量。

    堆叠柱状图

    from pyecharts.charts import Bar
    from pyecharts import options as opts
    
    x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
    y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
    
    bar = (
        Bar(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add_xaxis(x)
        .add_yaxis('test1',y1,stack="stack1")
        .add_yaxis('test2',y2,stack="stack1")
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test',pos_left='center'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_right='1px'),
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
        #滑动
        datazoom_opts=opts.DataZoomOpts(is_show=True, type_="slider",range_start=20,range_end=60)) #图例位置
        )
    
    
    bar.render('bar_stack.html')
    
    • 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

    在这里插入图片描述
    主要在add_yaxis()中加入stack=“stack1”

    翻转柱状图

    from pyecharts.charts import Bar
    from pyecharts import options as opts
    
    x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
    y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
    
    bar = (
        Bar(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add_xaxis(x)
        .add_yaxis('test1',y1)
        .add_yaxis('test2',y2)
        #翻转
        .reversal_axis()
        #数字显示右边
        .set_series_opts(label_opts=opts.LabelOpts(position="right"))
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test',pos_left='center'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_right='1px'),
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='sh',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='动物名',name_location='middle', name_gap=40),
        
        ))
    
    
    bar.render('barh.html')
    
    • 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

    在这里插入图片描述
    #翻转
    .reversal_axis()
    #数字显示右边
    .set_series_opts(label_opts=opts.LabelOpts(position=“right”))

    直方图

    from pyecharts.charts import Bar,Line,Grid
    from pyecharts import options as opts
    
    x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
    y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
    
    bar = (
        Bar(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add_xaxis(x)
        .add_yaxis('test1',y1,category_gap=0)
        #.add_yaxis('test2',y2,category_gap=0)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test',pos_left='center'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_right='1px'),
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
        
        ))
    
    
    bar.render('zft.html')
    
    • 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

    在这里插入图片描述
    将add_yaxis中加入category_gap=0或category_gap='0%'即可,category_gap是空值柱子的宽度。

    折线图

    折线图将Bar变为Line

    from pyecharts.charts import Bar,Line,Grid
    from pyecharts import options as opts
    
    x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
    y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
    
    line = (
        Line(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add_xaxis(x)
        .add_yaxis('test1',y1)
        .add_yaxis('test2',y2)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test',pos_left='center'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_right='1px'),
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
        
        ))
    
    
    line.render('line.html')
    
    • 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

    在这里插入图片描述

    显示

    from pyecharts.charts import Line
    from pyecharts import options as opts
    
    x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
    y_data = [820, 932, 901, 934, 1290, 1330, 1320]
    
    l = (
        Line(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add_xaxis(x_data)
        .add_yaxis("test", y_data,
                   #显示symbol
                   is_symbol_show=True,  
                   #symbol大小
                   symbol_size = 10,  
                   #显示symbol上的数字
                   label_opts=opts.LabelOpts(is_show=True))
        .set_global_opts(
            #标题名
            title_opts=opts.TitleOpts(title='test',pos_left='center'),
            #显示图例,右边
            legend_opts=opts.LegendOpts(pos_right='1px'),
            #横坐标
            xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":45},name='日期',name_location='middle',name_gap=40),
            #纵坐标
            yaxis_opts=opts.AxisOpts(
                name='sh',name_location='middle', name_gap=40,
                splitline_opts=opts.SplitLineOpts(is_show=True), #显示纵坐标横线
                axisline_opts=opts.AxisLineOpts(is_show=False))  #不显示显示纵坐标竖线
            )
        #设置折线
        .set_series_opts(
            linestyle_opts=opts.LineStyleOpts(
                    # 是否显示
                    is_show= True,
                    #线宽。
                    width = 2,
                    #颜色
                    color='red'))
        .render("ltest.html")
        )
    
    • 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

    在这里插入图片描述

    散点图

    散点图将Bar变为Scatter

    from pyecharts.charts import Bar,Line,Scatter
    from pyecharts import options as opts
    
    x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
    y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
    
    scatter = (
        Scatter(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add_xaxis(x)
        .add_yaxis('test1',y1)
        .add_yaxis('test2',y2)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test',pos_left='center'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_right='1px'),
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
        
        ))
    
    
    scatter.render('scatter.html')
    
    • 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

    在这里插入图片描述

    箱型图

    from pyecharts import options as opts
    from pyecharts.charts import Boxplot
    x = ["expr1", "expr2"]
    y1 = [
        [850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980],
        [960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790],
    ]
    y2 = [
        [890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920],
        [890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870],
    ]
    
    
    boxplot = (
        Boxplot(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add_xaxis(x)
        .add_yaxis('test1',Boxplot.prepare_data(y1))
        .add_yaxis('test2',Boxplot.prepare_data(y2))
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test',pos_left='center'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_right='1px'),
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='te',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
        
        ))
    
    
    boxplot.render('boxplot.html')
    
    • 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

    在这里插入图片描述

    其中,Boxplot.prepare_data(y1),将y1中数据转换为嵌套的 [min, Q1, median (or Q2), Q3, max],其中Q1是下四分位数,Q3是上四分位数

    面积图

    from pyecharts.charts import Line
    from pyecharts import options as opts
     
    x = ['1月','2月','3月','4月','5月','6月']
    a=[12,9,6,13,10,12]
    b=[8,6,10,7,9,11]
    c=[11,12,9,12,13,9]
    area = (
        Line()
        .add_xaxis(x)
        .add_yaxis('a', a, is_smooth=True, areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
        .add_yaxis('b', b, is_smooth=True, areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
        .add_yaxis('c', c, is_smooth=True, areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
        .set_global_opts(title_opts=opts.TitleOpts(title="面积图"))
    )
    area.render("面积图.html")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    仪表盘

    from pyecharts import options as opts
    from pyecharts.charts import Gauge
    
    gauge = (
        Gauge(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add("", [("完成率", 88.6)],detail_label_opts=opts.GaugeDetailOpts(offset_center=[0,"25%"]))
        .set_global_opts(
        title_opts=opts.TitleOpts(title="gg"),
        )
        
        
    )
    
    gauge.render("gauge.html")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述
    其中detail_label_opts=opts.GaugeDetailOpts(offset_center=[0,“25%”])是将轮盘内数据项标签移动

    饼状图

    from pyecharts import options as opts
    from pyecharts.charts import Pie
    
    x = ['鸡','鸭','鱼','鹅','猪']
    y1 = [16,10,6,18,26]
    d = [(i,j) for i,j in zip(x,y1)]
    pie = (
        Pie(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add("", d)
        .set_global_opts(title_opts=opts.TitleOpts(title='test',pos_left='left'))
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}  占比{d}%"))
        
    )
    pie.render("pie1.html")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述
    set_series_opts(label_opts=opts.LabelOpts(formatter=“{b}: {c} 占比{d}%”))中{b}是name,{c}数量,{d}%占比

    圆环图

    from pyecharts import options as opts
    from pyecharts.charts import Pie
    
    x = ['鸡','鸭','鱼','鹅','猪']
    y1 = [16,10,6,18,26]
    d = [(i,j) for i,j in zip(x,y1)]
    pie = (
        Pie(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add("",d,
             radius=['50%','70%'],#第一个内半径,第二个外半径 圆环图的重要步骤!!!!
             center=['50%','50%'], #第一个横坐标,第二个纵坐标
             
             )
        .set_global_opts(title_opts=opts.TitleOpts(title='test'))
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}  占比{d}%"))
        
    )
    pie.render("pie2.html")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述
    radius=[‘50%’,‘70%’] 只要第一个不是0或0%,就说明有了内半径,就可以绘制圆环图了。

    玫瑰图

    from pyecharts import options as opts
    from pyecharts.charts import Pie
    
    x = ['鸡','鸭','鱼','鹅','猪']
    y1 = [16,10,6,18,26]
    d = [(i,j) for i,j in zip(x,y1)]
    pie = (
        Pie(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add("",d,
             radius=['50%','70%'],#第一个内半径,第二个外半径 圆环图的重要步骤!!!!
             center=['25%','50%'], #第一个横坐标,第二个纵坐标
             rosetype='radius'
             )
        .add("",d,
             radius=['50%','70%'],#第一个内半径,第二个外半径 圆环图的重要步骤!!!!
             center=['75%','50%'], #第一个横坐标,第二个纵坐标
             rosetype='area'
             )
        .set_global_opts(title_opts=opts.TitleOpts(title='test'))
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}{d}%"))
        
    )
    pie.render("pie3.html")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述
    center=[‘75%’,‘50%’], #第一个横坐标,第二个纵坐标,是饼图的中心坐标,rosetype两种形式。

    层叠图

    需要用到层叠组件overlap

    from pyecharts.charts import Bar,Line,Grid
    from pyecharts import options as opts
    
    x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
    y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
    y3 = [i*2 for i in range(11)]
    
    bar = (
        Bar(init_opts = opts.InitOpts(width='1550px',height='750px'))
        .add_xaxis(x)
        .add_yaxis('test1',y1)
        .add_yaxis('test2',y2)
        #插入一个右边的坐标
        .extend_axis(
            yaxis=opts.AxisOpts(
                axislabel_opts=opts.LabelOpts(formatter="{value} yuan"), interval=4
            )
        )
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test',pos_left='center'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_right='1px'),
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40,axislabel_opts=opts.LabelOpts(formatter="{value} ge")),
      
         #图例位置
        ))
    
    #折线图
    line = (
            Line(init_opts = opts.InitOpts(width='1550px',height='750px'))
            .add_xaxis(x)
            .add_yaxis("jiage", y3, yaxis_index=1)) #yaxis_index=1是使用右边的纵坐标
    bar.overlap(line)
    bar.render('zh.html')
    
    • 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

    在这里插入图片描述
    相当于画两个图,用overlap进行层叠

    水平组合图grid

    from pyecharts.charts import Bar,Line,Grid,Scatter
    from pyecharts import options as opts
    
    x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
    y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
    
    bar = (
        Bar()
        .add_xaxis(x)
        .add_yaxis('test1',y1)
        .add_yaxis('test2',y2)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test1'),
      
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
    
        ))
    
    line = (
        Line()
        .add_xaxis(x)
        .add_yaxis('test1',y1)
        .add_yaxis('test2',y2)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test2',pos_left='5%'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_right='1px'),
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
    
        ))
    
    
    
    grid = (
        Grid(init_opts = opts.InitOpts(width='1850px',height='500px'))
        
        .add(line, grid_opts=opts.GridOpts(pos_left="55%"))
        .add(bar, grid_opts=opts.GridOpts(pos_right="55%"))
        
    )
    
    grid.render('t.html')
    
    • 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

    在这里插入图片描述

    水平组合图grid1

    from pyecharts.charts import Bar, Line, Grid, Scatter, Pie
    from pyecharts import options as opts
    
    x1 = ['鸡', '鸭', '鱼', '鹅', '猪']
    y1 = [16, 10, 6, 18, 26]
    y2 = [12, 18, 12, 8, 20]
    
    bar = (
        Bar(init_opts=opts.InitOpts(width='800px',height='400px'))
            .add_xaxis(x1)
            .add_yaxis('柱状图1', y1)
            # 全局配置
            .set_global_opts(
            # 标题名
            title_opts=opts.TitleOpts(title='柱状图',pos_top='20%', pos_left='40%'),
            # 显示图例,
            legend_opts=opts.LegendOpts(pos_top='20%', pos_left='60%'),
    
        ))
    
    
    
    data = [(i, j) for i, j in zip(x1, y1)]
    
    pie = (
        Pie(init_opts=opts.InitOpts(width='5%', height='5%'))
            .add("", data, center=['25%', '60%'], radius=['30%', '40%'])  #center是调整饼图位置
            # 全局配置
            .set_global_opts(
            # 标题名
            title_opts=opts.TitleOpts(title='饼状图', pos_top='10%'),
            # 显示图例,
            legend_opts=opts.LegendOpts(pos_top='35%', pos_left='18%'))
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}  占比{d}%"))
    
    )
    
    grid = (
        Grid(init_opts=opts.InitOpts(width='1550px',height='750px'))
    
    
            .add(bar, grid_opts=opts.GridOpts(pos_left="55%",pos_top='30%'))
    
            .add(pie, grid_opts=opts.GridOpts(pos_right="55%"))
    )
    
    grid.render('t11.html')
    
    
    • 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

    在这里插入图片描述

    垂直组合图grid

    from pyecharts.charts import Bar,Line,Grid,Scatter
    from pyecharts import options as opts
    
    x = ['鸡','鸭','鱼','鹅','猪',"衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    y1 = [16,10,6,18,26,5, 20, 36, 10, 75, 90]
    y2 = [12,18,12,8,20,5, 20, 36, 10, 75, 90]
    
    bar = (
        Bar()
        .add_xaxis(x)
        .add_yaxis('test1',y1)
        .add_yaxis('test2',y2)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test1'),
        #显示图例,
        legend_opts=opts.LegendOpts(pos_top='1px'),
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
    
        ))
    
    line = (
        Line()
        .add_xaxis(x)
        .add_yaxis('test1',y1)
        .add_yaxis('test2',y2)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='test2',pos_left='5%'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_top="55%"),
        #横坐标
        xaxis_opts=opts.AxisOpts(axislabel_opts={'font_size':15,'interval': 0,"rotate":90},name='动物名',name_location='middle',name_gap=40),
        #纵坐标
        yaxis_opts=opts.AxisOpts( name='sh',name_location='middle', name_gap=40),
    
        ))
    
    
    
    grid = (
        Grid(init_opts = opts.InitOpts(width='1100px',height='900px'))
        
        .add(line, grid_opts=opts.GridOpts(pos_top="55%"))
        .add(bar, grid_opts=opts.GridOpts(pos_bottom="55%"))
        
    )
    
    grid.render('t1.html')
    
    • 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

    在这里插入图片描述

    水平垂直组合图gird

    from pyecharts.charts import Bar,Line,Grid,Scatter
    from pyecharts import options as opts
    
    x1 = ['鸡','鸭','鱼','鹅','猪']
    y1 = [16,10,6,18,26]
    y2 = [12,18,12,8,20]
    
    bar = (
        Bar()
        .add_xaxis(x1)
        .add_yaxis('柱状图1',y1)
        .add_yaxis('柱状图2',y2)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='柱状图'),
        #显示图例,
        legend_opts=opts.LegendOpts(pos_top='5%',pos_left='20%'),
        
        ))
    
    line = (
        Line()
        .add_xaxis(x1)
        .add_yaxis('折线图test',y1,
                   #显示symbol
                   is_symbol_show=True,  
                   #symbol大小
                   symbol_size = 20,  
                   #显示symbol上的数字
                   label_opts=opts.LabelOpts(is_show=True))
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='折线图',pos_left='50%'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_top='5%',pos_left='75%'))
        #设置折线
        .set_series_opts(
            linestyle_opts=opts.LineStyleOpts(
                    # 是否显示
                    is_show= True,
                    #线宽。
                    width = 2,
                    #颜色
                    color='orange'))
    
        )
    
    scatter = (
        Scatter()
        .add_xaxis(x1)
        .add_yaxis('散点图test',y1,symbol_size=40,color='red')
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='散点图',pos_left='50%',pos_top='50%'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_top='55%',pos_left='75%'),
        ))
    
    
    bar1 = (
        Bar()
        .add_xaxis(x1)
        .add_yaxis('堆积柱状图test1',y1,stack="stack1")
        .add_yaxis('堆积柱状图test2',y2,stack="stack1")
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='堆积柱状图',pos_top='50%'),
        #显示图例,
        legend_opts=opts.LegendOpts(pos_top='55%',pos_left='18%'),
        
        ))
    
    
    grid = (
        Grid(init_opts = opts.InitOpts(width='1800px',height='920px'))
        
        .add(line, grid_opts=opts.GridOpts(pos_bottom="60%", pos_left="60%"))  
        .add(bar, grid_opts=opts.GridOpts(pos_bottom="60%", pos_right="60%"))
        .add(scatter, grid_opts=opts.GridOpts(pos_top="60%", pos_left="60%"))
        .add(bar1, grid_opts=opts.GridOpts(pos_top="60%", pos_right="60%"))
    )
    
    grid.render('t1.html')
    
    • 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

    在这里插入图片描述

    水平垂直组合图-加入饼状图grid

    from pyecharts.charts import Bar,Line,Grid,Scatter,Pie
    from pyecharts import options as opts
    
    x1 = ['鸡','鸭','鱼','鹅','猪']
    y1 = [16,10,6,18,26]
    y2 = [12,18,12,8,20]
    
    
    
    
    bar = (
        Bar()
        .add_xaxis(x1)
        .add_yaxis('柱状图1',y1)
        .add_yaxis('柱状图2',y2)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='柱状图'),
        #显示图例,
        legend_opts=opts.LegendOpts(pos_top='5%',pos_left='20%'),
        
        ))
    
    line = (
        Line()
        .add_xaxis(x1)
        .add_yaxis('折线图test',y1,
                   #显示symbol
                   is_symbol_show=True,  
                   #symbol大小
                   symbol_size = 20,  
                   #显示symbol上的数字
                   label_opts=opts.LabelOpts(is_show=True))
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='折线图',pos_left='50%'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_top='5%',pos_left='75%'))
        #设置折线
        .set_series_opts(
            linestyle_opts=opts.LineStyleOpts(
                    # 是否显示
                    is_show= True,
                    #线宽。
                    width = 2,
                    #颜色
                    color='orange'))
    
        )
    
    scatter = (
        Scatter()
        .add_xaxis(x1)
        .add_yaxis('散点图test',y1,symbol_size=40,color='red')
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='散点图',pos_left='50%',pos_top='50%'),
        #显示图例,右边
        legend_opts=opts.LegendOpts(pos_top='55%',pos_left='75%'),
        ))
    
    data = [(i,j) for i,j in zip(x1,y1)]
    
    pie = (
        Pie(init_opts = opts.InitOpts(width='5%',height='5%'))
        .add("",data,center=['25%','80%'],radius=['0%','40%'])
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='饼状图',pos_top='50%'),
        #显示图例,
        legend_opts=opts.LegendOpts(pos_top='55%',pos_left='18%'))
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}  占比{d}%"))
        
        )
    
    
    grid = (
        Grid(init_opts = opts.InitOpts(width='1800px',height='920px'))
        
        .add(line, grid_opts=opts.GridOpts(pos_bottom="60%", pos_left="60%"))  
        .add(bar, grid_opts=opts.GridOpts(pos_bottom="60%", pos_right="60%"))
        .add(scatter, grid_opts=opts.GridOpts(pos_top="60%", pos_left="60%"))
        .add(pie, grid_opts=opts.GridOpts(pos_top="60%", pos_right="60%"))
    )
    
    grid.render('t11.html')
    
    • 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

    在这里插入图片描述
    加入饼状图,需要在饼状图中加入center和radius参数,分别代表饼图所在的横纵坐标,和饼图的内外半径,最后布局的时候,饼图不能放在第一位,否则会报错。

    组合图Page用法之简单布局

    from pyecharts.charts import Bar,Line,Scatter,Pie,Page
    from pyecharts import options as opts
    
    x1 = ['鸡','鸭','鱼','鹅','猪']
    y1 = [16,10,6,18,26]
    y2 = [12,18,12,8,20]
    
    
    
    
    bar = (
        Bar()
        .add_xaxis(x1)
        .add_yaxis('柱状图1',y1)
        .add_yaxis('柱状图2',y2)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='柱状图'),
        
        
        ))
    
    line = (
        Line()
        .add_xaxis(x1)
        .add_yaxis('折线图test',y1,
                   #显示symbol
                   is_symbol_show=True,  
                   #symbol大小
                   symbol_size = 20,  
                   #显示symbol上的数字
                   label_opts=opts.LabelOpts(is_show=True))
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='折线图'))
        
        #设置折线
        .set_series_opts(
            linestyle_opts=opts.LineStyleOpts(
                    # 是否显示
                    is_show= True,
                    #线宽。
                    width = 2,
                    #颜色
                    color='orange'))
    
        )
    
    scatter = (
        Scatter()
        .add_xaxis(x1)
        .add_yaxis('散点图test',y1,symbol_size=40,color='red')
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='散点图')
        
        ))
    
    
    bar1 = (
        Bar()
        .add_xaxis(x1)
        .add_yaxis('堆积柱状图test1',y1,stack="stack1")
        .add_yaxis('堆积柱状图test2',y2,stack="stack1")
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='堆积柱状图'),
      
        
        ))
    
    data = [(i,j) for i,j in zip(x1,y1)]
    data1 = [(i,j) for i,j in zip(x1,y2)]
    pie = (
        Pie()
        .add("",data)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='饼状图'),
       )
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}  占比{d}%"))
        
        )
    
    pie1 = (
        Pie()
        .add("",data1,radius=['30%','70%'])
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='圆形图'),
       )
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}  占比{d}%"))
        
        )
    
    
    #Page.SimplePageLayout是将图形放在中间,缩小图形会排成一行
    #Page.DraggablePageLayout是一种拖拽布局,可随意拖拽图形,
    
    
    page = (
            Page(layout=Page.SimplePageLayout)   
            .add(
                bar,
                line,
                scatter,
                bar1,
                pie,
                pie1)
            )
    
    page.render('t11.html')
    
    • 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

    #Page.SimplePageLayout是将图形放在中间,缩小图形会排成一行
    在这里插入图片描述
    缩小后:
    在这里插入图片描述

    组合图Page用法之拖动布局

    from pyecharts.charts import Bar,Line,Scatter,Pie,Page
    from pyecharts import options as opts
    
    x1 = ['鸡','鸭','鱼','鹅','猪']
    y1 = [16,10,6,18,26]
    y2 = [12,18,12,8,20]
    
    
    
    
    bar = (
        Bar()
        .add_xaxis(x1)
        .add_yaxis('柱状图1',y1)
        .add_yaxis('柱状图2',y2)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='柱状图'),
        
        
        ))
    
    line = (
        Line()
        .add_xaxis(x1)
        .add_yaxis('折线图test',y1,
                   #显示symbol
                   is_symbol_show=True,  
                   #symbol大小
                   symbol_size = 20,  
                   #显示symbol上的数字
                   label_opts=opts.LabelOpts(is_show=True))
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='折线图'))
        
        #设置折线
        .set_series_opts(
            linestyle_opts=opts.LineStyleOpts(
                    # 是否显示
                    is_show= True,
                    #线宽。
                    width = 2,
                    #颜色
                    color='orange'))
    
        )
    
    scatter = (
        Scatter()
        .add_xaxis(x1)
        .add_yaxis('散点图test',y1,symbol_size=40,color='red')
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='散点图')
        
        ))
    
    
    bar1 = (
        Bar()
        .add_xaxis(x1)
        .add_yaxis('堆积柱状图test1',y1,stack="stack1")
        .add_yaxis('堆积柱状图test2',y2,stack="stack1")
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='堆积柱状图'),
      
        
        ))
    
    data = [(i,j) for i,j in zip(x1,y1)]
    data1 = [(i,j) for i,j in zip(x1,y2)]
    pie = (
        Pie()
        .add("",data)
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='饼状图'),
       )
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}  占比{d}%"))
        
        )
    
    pie1 = (
        Pie()
        .add("",data1,radius=['30%','70%'])
        #全局配置
        .set_global_opts(
        #标题名
        title_opts=opts.TitleOpts(title='圆形图'),
       )
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}  占比{d}%"))
        
        )
    
    
    #Page.SimplePageLayout是将图形放在中间,缩小图形会排成一行
    #Page.DraggablePageLayout是一种拖拽布局,可随意拖拽图形,
    
    
    page = (
            Page(layout=Page.DraggablePageLayout)   
            .add(
                bar,
                line,
                scatter,
                bar1,
                pie,
                pie1)
            )
    
    page.render('t111.html')
    
    • 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

    在这里插入图片描述
    在这里插入图片描述
    点击save config,会生成一个chart_config.json文件,
    再输入以下代码:

    from pyecharts.charts import Page
    
    Page.save_resize_html("t111.html",   # 之前保存的HTML文件名称
                          cfg_file="chart_config.json",  # 保存的json文件
                          dest="new_t111.html")  # 新的HTML文件名称
    
    • 1
    • 2
    • 3
    • 4
    • 5

    生成new_t111.html
    在这里插入图片描述
    随意缩放大小都不会改变其布局。

  • 相关阅读:
    lightGBM+158个技术因子实证A股十年数据:年化24%,回撤10%
    鸿蒙DevEco Service开发准备与使用
    idleTASK,分析。
    vcomp120.dll丢失的详细解决方法,全面分享5个解决方法分享
    非自交任意多边形与矩形框的交集面积计算方法
    Alins - 化繁为简、极致优雅的WebUI框架
    vulnhub-xxe lab: 1
    使用Git下载大语言模型
    Flink PostgreSQL CDC配置和常见问题
    Go命令大全:全面解析与实践
  • 原文地址:https://blog.csdn.net/qq_33267306/article/details/126885101