• matplotlib绘图


    🐳 我正在和鲸社区参加“商业数据分析训练营活动” https://www.heywhale.com/home/competition/6487de6649463ee38dbaf58b ,以下是我的学习笔记:

    学习主题:matplotlib绘图

    日期:2023.9.13

    关键概念/知识点:

    • 创建图形和子图
    • 自定义图形样式
    • 添加标题、轴标签和图例
    • 直方图
    • 散点图
    • 气泡图
    • 柱形图

    掌握的新函数/方法:

    • plt.figure()
    • fig.add_subplot()
    • ax.plot(x,y,linestyle = ‘–’, color = ‘red’, marker = ‘o’)
    • plt.title(‘Title’)
    • plt.xlabel(‘X-label’)
    • plt.ylabel(‘Y-label’)
    • ax.legend([‘Line 1’, ‘Line 2’])
    • ax.hist(x, bins)
    • ax.scatter(x, y)
    • ax.scatter(x, y, s = df[‘Income’])
    • ax.bar(x, y)
    • ax.plot(x, y)

    代码举例

    创建图形和子图:
    matplotlib中最基本的绘图单位是图形(Figure)和子图(Axes)。图形可以看作是一个画布,而子图则是画布上的绘图区域。

    # 创建一个名为fig的画布,并向画布添加一个子图ax  
    # add_subplot() 中传递的参数 1, 1, 1 表示该图形对象应包含一个子图,该子图应在 1x1 的网格中排列,并且此子图是该网格中的第一个子图  
    fig = plt.figure()  
    ax = fig.add_subplot(1, 1, 1)  
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    自定义图形样式:

    import matplotlib.pyplot as plt 
    fig = plt.figure()  
    ax = fig.add_subplot(1, 1, 1) 
    sales = [100, 150, 120, 180, 210, 170]  
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']  
    # linestyle:表示折线图中线条的样式,例如'-'表示实线,'--'表示虚线  
    # color:表示线条或散点的颜色,可以使用颜色名称(如'red')或十六进制RGB值(如'#FF0000')  
    # marker:表示散点图中数据点的形状,例如'o'表示圆点、's'表示正方形 
    ax.plot(months, sales, linestyle = '--', color = 'red', marker = 'o')  
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述
    添加标题、轴标签:

    import matplotlib.pyplot as plt 
    fig = plt.figure()  
    ax = fig.add_subplot(1, 1, 1) 
    sales = [100, 150, 120, 180, 210, 170]  
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']  
    ax.plot(months, sales, linestyle = '--', color = 'red', marker = 'o') 
    # 添加标题
    plt.title("某公司上半年销售额变化折线图")
    # 添加x轴标签
    plt.xlabel("月份")
    #添加y轴标签
    plt.ylabel("销售额")
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述
    员工年龄分布直方图:

    # 载入pandas、matplotlib、numpy库  
    import pandas as pd  
    import matplotlib.pyplot as plt  
    import numpy as np  
    # 导入数据  
    df = pd.read_excel('/home/mw/input/data_analysis8875/al3-1.xls')  
    fig = plt.figure()  
    ax = fig.add_subplot(1, 1, 1) 
    
    # 在子图上绘制直方图
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)  
    ax.hist(df['Age'],bins=[25,30,35,40,45])
    
    # 设置图形标题  
    plt.title('某公司员工年龄分布直方图')  
    # 设置x轴标签  
    plt.xlabel('年龄')  
    # 设置y轴标签  
    plt.ylabel('人数')  
    
    # 向plt.xticks()传入参数range(25, 50, 5)表示x轴数值范围从2550,每隔5个单位显示一个数值
    plt.xticks(range(25,50,5))
    
    # 调用plt.show()函数显示图形
    plt.show()
    
    • 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

    在这里插入图片描述
    员工年龄与销售情况散点图 :

    import pandas as pd  
    import matplotlib.pyplot as plt  
    import numpy as np  
    
    df = pd.read_excel('/home/mw/input/data_analysis8875/al3-1.xls') 
    
    # 创建一个图形对象  
    fig = plt.figure()  
    
    # 向图形对象添加一个子图  
    ax = fig.add_subplot(1,1,1)  
    
    # 在子图上绘制散点图  
    ax.scatter(df['Age'],df['Sales'])  
    
    # 设置图形标题  
    plt.title('某公司员工年龄与销售额散点图')  
    
    # 设置x轴、y轴标签  
    plt.xlabel('年龄')  
    plt.ylabel('销售额')  
    
    # 显示图形  
    plt.show()  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述
    员工年龄与销售情况气泡图 :
    scatter( )函数中的s参数提供了一种便捷的方法,可以用来对点的大小进行控制,进而实现用点的大小来反映收入的大小。

    # 导入数据
    import pandas as pd  
    import matplotlib.pyplot as plt  
    import numpy as np  
    df = pd.read_excel('/home/mw/input/data_analysis8875/al3-1.xls')  
    
    # 创建一个图形对象并添加子图 
    fig = plt.figure()   
    ax = fig.add_subplot(1,1,1)  
    
    # 在子图上绘制气泡图 
    ax.scatter(df['Age'],df['Sales'],s = df['Income'])  
    
    # 设置图形标题  
    plt.title('某公司员工年龄与销售额及收入气泡图')  
    
    # 设置x轴、y轴标签  
    plt.xlabel('年龄')  
    plt.ylabel('销售额')  
    
    # 显示图形  
    plt.show()  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述
    员工性别销售情况柱形图 :

    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    # 使用groupby()函数按性别Gender对员工进行分组,将分组后数据命名为gender_group  
    df = pd.read_excel('/home/mw/input/data_analysis8875/al3-1.xls')  
    gender_group = df.groupby('Gender')  
    
    # 对分组后数据进行操作,将销售量Sales进行加总,保存为var  
    var = gender_group['Sales'].sum()  
    
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    
    # 对性别进行分组后,var中index代表性别属性,values代表销售额  
    ax.bar(var.index,var.values)
    
    plt.title('某公司员工性别与销售情况柱形图')
    plt.xlabel('性别')
    plt.ylabel('销售额')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    关键总结:

    通过强大的Python语言和神奇的matplotlib库,将枯燥的商业数据变成绚丽多彩的图形,让数据跃然纸上,为企业决策提供更直观、更清晰的依据。学会运用matplotlib库为绘制折线图、柱状图、散点图等图形,利用可视化的图形去展示数据,解决例如某公司员工年龄分布、年龄与销售情况以及性别与销售情况等一系列问题。

    问题/困惑:

    在分组或者多条件情形下,绘制图形。

    下一步计划:

    通过练习掌握matplotlib库,绘制出各种多样的图形。

    参考资料/相关资源链接:

    Pandas文档:https://pandas.pydata.org/docs/
    matplotlib文档:https://www.matplotlib.org.cn/intro/

  • 相关阅读:
    python-docx 使用xml为docx不同的章节段落设置不同字体
    <C++>构造_拷贝_析构_运算符重载函数_const成员函数
    栈和队列基础
    程序地址空间
    typeScript--[interface接口实现类的定义,函数定义,可索引定义]
    5个例子学会Pandas中的字符串过滤
    【MY杂记】- SpringBoot 配置全局 Json 序列化与反序列化
    1行Python代码实现:PDF转Word。
    大屏UI设计-看这一篇就够了
    中国便携式热疗设备行业运营态势与应用趋势预测报告2022-2028年
  • 原文地址:https://blog.csdn.net/weixin_44336912/article/details/132856564