• Python之Matplot——01.堆叠柱状图的绘制


    1.Matplotlib是python的一个绘图库,可以方便的绘制各种图标,是数据可视化的利器。

    2.本文我就给大家介绍一下条形图或者说柱状图的绘制

    3.代码如下:

    《1》首先导入模块

    1 import matplotlib.pyplot as plt

    《2》准备数据

    labels = ['G1', 'G2', 'G3', 'G4', 'G5']
    
    men_means = [20, 35, 30, 35, 27]
    women_means = [25, 32, 34, 20, 25]
    men_std = [2, 3, 4, 1, 2]
    women_std = [3, 5, 2, 3, 3]

    《3》设置宽度

    width = 0.35  # 条形图的宽度

    《4》获取子图对象

    fig,ax = plt.subplots()

    《5》绘制第一层柱状图和第二层柱状图

    ax.bar(labels, men_means,width,yerr=men_std,label='Men')
    ax.bar(labels,women_means,width,yerr=women_std,bottom=men_means,label='Women')

    《6》设置标题和标签

    ax.set_ylabel('Scores')
    ax.set_title('Scores by group and gender')

    《7》图例和显示

    ax.legend()
    
    plt.show()

    4.全部代码如下

    import matplotlib.pyplot as plt
    #条形图的绘制
    labels = ['G1', 'G2', 'G3', 'G4', 'G5']
    
    men_means = [20, 35, 30, 35, 27]
    women_means = [25, 32, 34, 20, 25]
    men_std = [2, 3, 4, 1, 2]
    women_std = [3, 5, 2, 3, 3]
    width = 0.35  # 条形图的宽度
    fig,ax = plt.subplots()
    ax.bar(labels, men_means,width,yerr=men_std,label='Men')
    ax.bar(labels,women_means,width,yerr=women_std,bottom=men_means,label='Women')
    
    ax.set_ylabel('Scores')
    ax.set_title('Scores by group and gender')
    ax.legend()
    
    plt.show()

    5.效果展示

  • 相关阅读:
    队列的链式存储结构实现
    堆栈的链式存储实现
    使用C#改变windows系统本地时间
    oracle 多数值录入校验(分隔符“/”)
    oracle中in和exists的区别
    redis安装 windows版(图形化安装)
    Oracle 返回结果集
    饿了么4年 + 阿里2年:研发路上的一些总结与思考
    Oracle 获取各类时间
    Oracle表中已有数据,修改字段长度
  • 原文地址:https://www.cnblogs.com/huipengbo/p/13977687.html
Copyright © 2020-2023  润新知