• python数据分析-matplotlib绘制折线图


    1.绘制一天内温度的折线图

    1.1 折线图基础绘制

    一天之内的温度从凌晨2点开始,每隔2小时测量一次温度,12次温度数据为

    y = [15, 13, 14, 5, 17, 20, 25, 26, 24, 22, 18, 15]
    
    • 1

    现在需要将温度作为纵坐标,时间作为横坐标,绘制折线图
    首先需要生成横坐标,这里可以通过实现,实际就是从2开始,一直到24

    x = range(2, 25, 2)
    
    • 1

    可以简单看一下里面的数据

    for x in range(2, 25,2):
        print(x)
    
    • 1
    • 2

    在这里插入图片描述
    通过plot可以确认横坐标和纵坐标,show进行展示

    # 绘图
    plt.plot(x, y)#这里x和y的数量需要是相等的
    
    # 展示图形
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    输出的结果就是一张图
    在这里插入图片描述

    1.2 调整大小分辨率

    展示的图片可以进行大小、分辨率的修改以及进行保存,通过figsize设置长宽,dpi设置分辨率

    # 设置图片大小
    plt.figure(figsize=(20, 8),dpi=80)
    
    • 1
    • 2

    加上这一段后,再生成结果就会发现图片的大小发生了改变
    在这里插入图片描述

    1.3 图片保存

    如果需要对这张图片进行保存,可以加一句

    # 保存图片
    plt.savefig('./1.1matplotlib基础绘图.png')  # 保存为svg矢量图可以避免放大后产生锯齿
    
    • 1
    • 2

    执行后就会在当前文件夹里生成一张图片
    在这里插入图片描述

    1.4 修改坐标轴刻度

    如果要修改x轴的刻度,可以通过将x的值作为刻度

    # 设置x轴的刻度
    plt.xticks(x)
    
    • 1
    • 2

    这样跑出来的结果就是
    在这里插入图片描述
    修改y轴的刻度,可以加上

    # 设置y轴的刻度
    plt.yticks(range(min(y),max(y)+1))
    
    • 1
    • 2

    这时就会发现y轴的刻度也发生了改变
    在这里插入图片描述
    不过目前坐标轴上只有数字,难以直观看出来具体时间和温度单位,所以需要将x轴上的数字改成时间,可以通过在xticks里增加参数来实现

    x_change = ["{}:00".format(i) for i in range(2, 25, 2)]
    plt.xticks(x, x_change)
    
    • 1
    • 2

    xticks(x, x_change)的作用就是用x_change来替代原来的x,这时候绘制出来的折线图里,横坐标就更加贴近生活了
    在这里插入图片描述

    1.5 增加中文图表描述信息

    如果需要对表格以及坐标轴增加注释,可以通过实现

    plt.xlabel("时间")
    plt.ylabel("温度(℃)")
    plt.title("一天的温度变化情况")
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    但执行之后却会出现乱码的情况,这是因为matplotlib默认并不支持中文,所以需要提前获取这台电脑的中文字体,需要先导入对应包,并定义参数my_font

    from matplotlib import font_manager
    my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")
    
    • 1
    • 2

    再修改上面添加的代码

    plt.xlabel("时间", fontproperties=my_font)
    plt.ylabel("温度(℃)", fontproperties=my_font)
    plt.title("一天的温度变化情况", fontproperties=my_font)
    
    • 1
    • 2
    • 3

    这时候就可以正常绘制出带有中文的图表了
    在这里插入图片描述

    1.6 增加辅助网格线

    如果觉得上面的图看的不够清楚,可以增加网格线进行辅助使用

    # 增加辅助网格线
    plt.grid()
    
    • 1
    • 2

    在这里插入图片描述
    完整代码如下

    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    
    x = range(2, 25, 2)
    y = [15, 13, 14, 5, 17, 20, 25, 26, 24, 22, 18, 15]
    
    my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")
    
    # 设置图片大小
    plt.figure(figsize=(20, 8),dpi=80)
    
    # 绘图
    plt.plot(x, y)
    # 设置x轴的刻度
    #plt.xticks(x)
    x_change = ["{}:00".format(i) for i in range(2, 25, 2)]
    plt.xticks(x, x_change)
    # 设置y轴的刻度
    plt.yticks(range(min(y),max(y)+1))
    
    plt.xlabel("时间", fontproperties=my_font)
    plt.ylabel("温度(℃)", fontproperties=my_font)
    plt.title("一天的温度变化情况", fontproperties=my_font)
    
    # 增加辅助网格线
    plt.grid()
    
    # 保存图片
    #plt.savefig('./1.1matplotlib基础绘图.png')  # 保存为svg矢量图可以避免放大后产生锯齿
    # 展示图形
    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
    • 27
    • 28
    • 29
    • 30
    • 31

    2.分别绘制两天温度的折线图

    上面说的只是一条折线的绘图,matplotlib也可以实现两条折线的绘图,方式基本一样,只是需要增加部分代码

    # 绘制图形,设置标签、颜色、线条样式
    plt.plot(x, y1, label='第一天', color="#F08080")
    plt.plot(x, y2, label='第二天', color="blue", linestyle="--")
    
    • 1
    • 2
    • 3
    # 添加图例
    # plt.legend(prop=my_font, loc="upper left")
    plt.legend(prop=my_font, loc="best")
    
    • 1
    • 2
    • 3

    最终实现的绘图结果还是符合预期的
    在这里插入图片描述
    完整代码如下

    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    
    x = range(2, 25, 2)
    y1 = [15, 13, 14, 5, 17, 20, 25, 26, 24, 22, 18, 15]
    y2 = [11, 13, 17, 10, 7, 23, 28, 6, 14, 12, 18, 25]
    
    my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")
    
    # 设置图片大小
    plt.figure(figsize=(20, 8),dpi=80)
    
    # 绘制图形,设置标签、颜色、线条样式
    plt.plot(x, y1, label='第一天', color="#F08080")
    plt.plot(x, y2, label='第二天', color="blue", linestyle="--")
    # 设置x轴的刻度
    #plt.xticks(x)
    x_change = ["{}:00".format(i) for i in range(2, 25, 2)]
    plt.xticks(x, x_change)
    # 设置y轴的刻度
    plt.yticks(range(min(y1),max(y2)+1))
    
    # 添加图例
    # plt.legend(prop=my_font, loc="upper left")
    plt.legend(prop=my_font, loc="best")
    
    plt.xlabel("时间", fontproperties=my_font)
    plt.ylabel("温度(℃)", fontproperties=my_font)
    plt.title("两天的温度变化情况", fontproperties=my_font)
    
    # 增加辅助网格线
    #plt.grid()
    
    # 保存图片
    #plt.savefig('./1.1matplotlib基础绘图.png')  # 保存为svg矢量图可以避免放大后产生锯齿
    # 展示图形
    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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    3. 散点图

    绘制散点图代码如下

    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    
    my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")  # 设置中文字体
    y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23]
    y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]
    x_3 = range(1,32)
    x_10 = range(51,82)
    # 设置图形大小
    plt.figure(figsize=(20, 8), dpi=80)
    #使用scatter方法绘制散点图,和之前绘制折线图的唯一区别
    plt.scatter(x_3,y_3,label="3月份")
    plt.scatter(x_10,y_10,label="10月份")
    #添加图例
    plt.legend(loc="best",prop=my_font)
    # 标题
    plt.title('3月份与10月份气温差异图',fontproperties=my_font)
    # 刻度
    _x = list(x_3)+list(x_10)
    _xticks3 = [f'3月{i}日' for i in x_3]
    _xticks10 = [f'10月{i-50}日' for i in x_10]
    _xticks = _xticks3+_xticks10
    plt.xticks(_x[::3], _xticks[::3],fontproperties=my_font,rotation=45)
    # 标签
    plt.xlabel('日期',fontproperties=my_font)
    plt.ylabel('温度',fontproperties=my_font)
    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
    • 27

    效果如图
    在这里插入图片描述

    4. 条形图

    4.1 纵向条形图

    纵向条形图代码如下

    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    
    my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")
    a = ["战狼2", "速度与激情8", "功夫瑜伽", "西游伏妖篇", "变形金刚5:\n最后的骑士", "摔跤吧!爸爸", "加勒比海盗5:\n死无对证", "金刚:骷髅岛", "极限特工:\n终极回归", "生化危机6:\n终章",
         "乘风破浪", "神偷奶爸3", "智取威虎山", "大闹天竺", "金刚狼3:\n殊死一战", "蜘蛛侠:\n英雄归来", "悟空传", "银河护卫队2", "情圣", "新木乃伊", ]
    
    b = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88,
         6.86, 6.58, 6.23]
    
    # 设置图形大小
    plt.figure(figsize=(20, 15), dpi=80)
    plt.bar(a, b, width=.3)
    plt.xticks(a, fontproperties=my_font,rotation=45)
    _yticks = [f'{i}亿' for i in b]
    plt.yticks(b[::5],_yticks[::5], fontproperties=my_font)
    plt.grid(alpha=0.4)
    plt.title('影片票房', fontproperties=my_font)
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    执行结果如图
    在这里插入图片描述
    这里需要注意,因为有些电影名太长,如果直接展示会超出图片范围,所以这里在逻辑上对名称进行了换行\n

    a = ["战狼2", "速度与激情8", "功夫瑜伽", "西游伏妖篇", "变形金刚5:\n最后的骑士", "摔跤吧!爸爸", "加勒比海盗5:\n死无对证", "金刚:骷髅岛", "极限特工:\n终极回归", "生化危机6:\n终章",
         "乘风破浪", "神偷奶爸3", "智取威虎山", "大闹天竺", "金刚狼3:\n殊死一战", "蜘蛛侠:\n英雄归来", "悟空传", "银河护卫队2", "情圣", "新木乃伊", ]
    
    • 1
    • 2

    如果觉得这种换行的方式比较麻烦,那就可以考虑使用横向条形图

    4.2 横向条形图

    大致逻辑和纵向条形图差不多,只不过从bar改成了barh,横纵坐标轴互换,
    完整代码如下

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    
    my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")
    a = ["战狼2", "速度与激情8", "功夫瑜伽", "西游伏妖篇", "变形金刚5:最后的骑士", "摔跤吧!爸爸", "加勒比海盗5:死无对证", "金刚:骷髅岛", "极限特工:终极回归", "生化危机6:终章",
         "乘风破浪", "神偷奶爸3", "智取威虎山", "大闹天竺", "金刚狼3:殊死一战", "蜘蛛侠:英雄归来", "悟空传", "银河护卫队2", "情圣", "新木乃伊", ]
    
    b = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88,
         6.86, 6.58, 6.23]
    
    # 设置图形大小
    plt.figure(figsize=(20, 15), dpi=80)
    plt.barh(range(len(a)), b, height=.5,color='orange')
    
    _yticks = [f'{i}亿' for i in b]
    plt.yticks(range(len(a)), a, fontproperties=my_font)
    plt.xticks(b[::5], _yticks[::5], fontproperties=my_font)
    plt.grid(alpha=0.5)
    plt.title('影片票房', fontproperties=my_font)
    
    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

    执行结果如下
    在这里插入图片描述

    4.3 多段条形图

    如果需要每部电影在不同日期的票房,可以通过多段条形图来实现,
    完整代码如下

    # coding=utf-8
    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    
    my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")
    
    a = ["猩球崛起3:终极之战","敦刻尔克","蜘蛛侠:英雄归来","战狼2"]
    b_16 = [15746,312,4497,319]
    b_15 = [12357,156,2045,168]
    b_14 = [2358,399,2358,362]
    #设置图形大小
    plt.figure(figsize=(20,8),dpi=80)
    
    bar_width = 0.2
    x_14 = list(range(len(a)))
    x_15 =[i+bar_width for i in x_14]
    x_16 =[i+bar_width for i in x_15]
    
    
    plt.bar(x_14,b_16,width=bar_width,label='9月14日')
    plt.bar(x_15,b_15,width=bar_width,label='9月15日')
    plt.bar(x_16,b_14,width=bar_width,label='9月16日')
    
    plt.legend(prop=my_font)  # 必须设置label
    plt.xticks(x_15,a,fontproperties=my_font)
    
    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
    • 27

    执行效果如图
    在这里插入图片描述

    5. 直方图

    和上面提到的绘图方法类似,主要不同点在于,绘制直方图主要使用的是hist而不是bar
    完整代码如下

    # coding=utf-8
    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    
    a = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124,
         101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111, 78, 132, 124, 113, 150, 110, 117, 86,
         95, 144, 105, 126, 130, 126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136, 123, 117, 119, 105, 137,
         123, 128, 125, 104, 109, 134, 125, 127, 105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114, 105, 115,
         132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134, 156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,
         123, 107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133, 112, 114, 122, 109, 106, 123, 116, 131, 127,
         115, 118, 112, 135, 115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154, 136, 100, 118, 119, 133, 134,
         106, 129, 126, 110, 111, 109, 141, 120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126, 114, 140, 103,
         130, 141, 117, 106, 114, 121, 114, 133, 137, 92, 121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113, 134,
         106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110, 105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146,
         133, 101, 131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111, 111, 133, 150]
    
    # 计算组距
    d = 3
    num_bins = (max(a) - min(a)) // d
    print(num_bins)
    # 设置图形的大小
    plt.figure(figsize=(20, 8), dpi=80)
    # 绘制直方图
    plt.hist(a, num_bins)
    
    # 设置x轴的刻度
    plt.xticks(range(min(a), max(a) + d, d))
    # 设置网格
    plt.grid()
    
    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
    • 27
    • 28
    • 29
    • 30
    • 31

    在这里插入图片描述

  • 相关阅读:
    最新测试技术:使用状态迁移法设计自动化驾驶测试用例
    Solon 1.6.25 发布,轻量级应用开发框架
    新手如何学习挖漏洞?看这篇就够了【网络安全】
    Sychronized和ReentrantLock锁 面试题
    【JVM技术专题】针对于Java类加载器系统研究指南 「入门篇」
    Google Earth Engine(GEE)—— LULC 土地分类
    HNU小学期工训-STC15单片机模型大作业实验报告
    【真送礼物】1 分钟 Serverless 极速部署盲盒平台,自己部署自己抽!
    算法---拥有最多糖果的孩子
    保护花朵(选择性贪心)C++详解
  • 原文地址:https://blog.csdn.net/weixin_44999258/article/details/128103342