• 数据分析1-matplotlib


    数据分析是用适当的方法对收集来的大量数据进行分析,帮助人们做出判断,以便采取适当的行动.
    在这里插入图片描述
    matplotlib:最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建。
    在这里插入图片描述

    matplotlib折线图绘制

    1.基本绘制,仅图像

    内含保存图像,设置图片大小

    #coding=utf-8
    from matplotlib import pyplot as plt
    x=range(2,26,2)#数据在x轴的位置,是一个可迭代对象
    y=[15,13,14,5,17,20,26,26,24,22,18,14]#数据在y轴的位置,是一个可迭代对象
    #分别是(2,15)(4,13)……
    
    #设置图片大小
    fig =plt.figure(figsize=(20,8),dpi=80)
    
    plt.plot(x,y) #传入X,Y形成对应坐标,通过plot绘制出折线图
    
    #保存
    plt.savefig("./t1.png")
    
    plt.show()#绘制图片
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    2.设置图片刻度

    #coding=utf-8
    from matplotlib import pyplot as plt
    x=range(2,26,2)#数据在x轴的位置,是一个可迭代对象
    y=[15,13,14,5,17,20,26,26,24,22,18,14]#数据在y轴的位置,是一个可迭代对象
    #分别是(2,15)(4,13)……
    
    #设置图片大小
    fig =plt.figure(figsize=(20,8),dpi=80)
    
    plt.plot(x,y) #传入X,Y形成对应坐标,通过plot绘制出折线图
    
    #保存
    # plt.savefig("./t1.png")
    
    # #设置x,y轴的刻度
    plt.xticks(range(2,26,2))
    plt.yticks(range(min(y),max(y)+1,2))
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    3.为x,y轴添加单位

    例,做成10点到12点的气温图
    在这里插入图片描述

    plt.xticks(fontproperties="STSong")#解决中文不显示问题
    
    • 1
    #coding=utf-8
    from matplotlib import pyplot as plt
    import random
    
    x=range(0,120)
    y=[random.randint(20,35) for i in range(120)]
    
    plt.figure(figsize=(20,8),dpi=80)
    plt.plot(x,y)
    
    #调整x轴的刻度
    
    _xtick_labels=["10点{}分".format(i) for i in range(60)]
    _xtick_labels+=["11点{}分".format(i-60) for i in range(60,120)]
    #取步长,数字和字符串一一对应,数据的长度一样
    plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=45)
    plt.xticks(fontproperties="STSong")#解决中文不显示问题
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    #coding=utf-8
    from matplotlib import pyplot as plt
    import random
    import matplotlib
    from matplotlib import font_manager
    
    x=range(0,120)
    y=[random.randint(20,35) for i in range(120)]
    
    plt.figure(figsize=(20,8),dpi=80)
    plt.plot(x,y)
    
    #调整x轴的刻度
    my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\STXINWEI.TTF")
    _xtick_labels=["10点{}分".format(i) for i in range(60)]
    _xtick_labels+=["11点{}分".format(i-60) for i in range(60,120)]
    #取步长,数字和字符串一一对应,数据的长度一样
    plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=45,fontproperties=my_font)
    # plt.xticks(fontproperties="STSong")#解决中文不显示问题
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

    4.设置图形信息

    # 添加描述信息
    plt.xlabel("时间",fontproperties=my_font)
    plt.ylabel("温度单位(℃)",fontproperties=my_font)
    plt.title("10点到12点每分钟温度情况",fontproperties=my_font)
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    在这里插入图片描述

    #coding=utf-8
    from matplotlib import pyplot as plt
    import matplotlib
    from matplotlib import font_manager
    
    x=range(11,31,1)
    y=[1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
    plt.figure(figsize=(15,7),dpi=60)
    plt.plot(x,y)
    
    my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\STXINWEI.TTF")
    
    _xtick_labels=["{}岁".format(i) for i in range(11,31)]
    plt.xticks(list(x)[::1],_xtick_labels[::1],rotation=45,fontproperties=my_font)
    
    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

    在这里插入图片描述
    在这里插入图片描述

    总结

    在这里插入图片描述
    https://matplotlib.org/stable/plot_types/index.html

    在这里插入图片描述
    在这里插入图片描述

    matplotlib散点图

    #coding:utf-8
    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    
    my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\STXINWEI.TTF" )
    
    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月份")
    
    # 调整x轴刻度
    _x =list(x_3)+list(x_10)
    _xtick_labels = ["3月{}日".format(i) for i in x_3]
    _xtick_labels += ["10月{}日".format(i - 50) for i in x_10]
    plt.xticks(_x[::3],_xtick_labels[::3],fontproperties=my_font,rotation=45)
    
    # 添加图例
    plt.legend(loc="upper right", prop=my_font)
    
    # 添加描述信息
    plt.xlabel("时间", fontproperties=my_font)
    plt.ylabel("温度", fontproperties=my_font)
    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
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    在这里插入图片描述

    matplotlib条形图

    #coding:utf-8
    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    
    my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/STXIHEI.TTF")
    plt.figure(figsize=(8, 8), dpi=80)
    a = ["战狼2", "速度与激情8", "功夫瑜伽", "西游伏妖篇", "乘风破浪", "变形精钢","生化危机","摔跤吧爸爸"]
    b = [56.01, 17.53, 14.20, 5.66,7.23,7.55, 7.32, 8.88]
    # 绘制条形图
    plt.barh(range(len(a)), b,height=0.3,color="orange")
    # 设置字符串到x轴
    plt.yticks(range(len(a)), a, fontproperties=my_font)
    plt.grid(alpha=0.3)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述
    在这里插入图片描述

    #coding=utf-8
    from matplotlib import pyplot as plt
    from matplotlib import font_manager
    my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/STXIHEI.TTF")
    
    a=["猩球崛起","段克尔克","蜘蛛侠3","战狼2"]
    b_16=[15746,312,4497,319]
    b_15=[12357,456,1244,154]
    b_14=[154,564,13213,4655]
    
    x_14=list(range(len(a)))
    x_15=[i+0.2 for i in x_14]
    x_16=[i+0.2*2 for i in x_14]
    
    plt.figure(figsize=(20,8),dpi=80)
    plt.bar(range(len(a)),b_14,width=0.2,label="9月14日")
    plt.bar(x_15,b_15,width=0.2,label="9月15日")
    plt.bar(x_16,b_16,width=0.2,label="9月16日")
    
    #设置x刻度
    plt.xticks(x_15,a,fontproperties=my_font)
    plt.legend(prop=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

    在这里插入图片描述

  • 相关阅读:
    【Hello Algorithm】认识一些简单的递归
    php权限调整强制用户退出的解决方案
    LeetCode:3. 无重复字符的最长子串
    vulfocus——solr远程命令执行(CVE-2019-17558)
    Selenium之入门
    前端下载超大文件的完整方案
    清华大牛终于把「图解计算机网络、操作系统」学习笔记总结出来了
    浏览器交互:Cookies、事件、浏览历史
    bp神经网络对数据的要求,bp神经网络参数有哪些
    gitea的简单介绍
  • 原文地址:https://blog.csdn.net/m0_62497122/article/details/126715507