• Python可视化数据分析10、Matplotlib库


    Python可视化数据分析10、Matplotlib库

    📋前言📋

    💝博客:【红目香薰的博客_CSDN博客-计算机理论,2022年蓝桥杯,MySQL领域博主】💝

    ✍本文由在下【红目香薰】原创,首发于CSDN✍

    🤗2022年最大愿望:【服务百万技术人次】🤗

    💝Python初始环境地址:【Python可视化数据分析01、python环境搭建】💝 


    环境需求

    环境:win10

    开发工具:PyCharm Community Edition 2021.2

    数据库:MySQL5.6

    目录

    Python可视化数据分析10、Matplotlib库

    📋前言📋

    环境需求

    前置环境

    前言

    绘制直线

    plot函数

    绘制柱状图

    堆积柱状图

    绘制并列柱状图

    绘制直方图

    绘制饼图

    绘制分裂式饼图

    绘制散点图

    绘制3D图像

    3D曲面图

    3D散点图

    3D条状图


    前置环境

    1. pip3 config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple
    2. pip3 config list
    3. pip3 install --upgrade pip
    4. pip3 install numpy
    5. pip3 install matplotlib

    东西比较大引入的慢一些,别急。

    前言

    Matplotlib是Python中最常用的可视化工具之一,可以非常方便地创建海量2D图表和一些基本的3D图表。
    Matplotlib首次发表于2007年,在开源和社区的推动下,现在基于Python的各个科学计算领域都得到了广泛应用。
    Matplotlib中应用最广的模块是pyplot模块,pyplot模块中的每个绘图函数都可以对图形进行一些更改。

    绘制直线

    1. import numpy as np
    2. from matplotlib import pyplot as plt
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. x = np.arange(1, 11)
    6. y = 2 * x
    7. plt.title("我是标题")
    8. plt.xlabel("我是X轴")
    9. plt.ylabel("我是Y轴")
    10. plt.plot(x, y)
    11. plt.show()

    不规律数值

    1. import numpy as np
    2. from matplotlib import pyplot as plt
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. x = np.arange(1, 11)
    6. y = 2 * x
    7. plt.title("我是标题")
    8. plt.xlabel("我是X轴")
    9. plt.ylabel("我是Y轴")
    10. plt.plot((1, 5, 1, 5, 7, 5, 5, 9, 1, 5), y)
    11. plt.show()

    plot函数

    plot()函数可以传入多个参数,其中第3个参数表示线条的颜色以及类型,第4个参数表示线条的宽度

    字符

    含义

    -

    实线样式

    --

    短横线样式

    -.

    点划线样式

    :

    虚线样式

    .

    点标记

    ,

    像素标记

    o

    圆标记

    v

    倒三角标记

    ^

    正三角标记

    <

    左三角标记

    >

    右三角标记

    1

    下箭头标记

    2

    上箭头标记

    3

    左箭头标记

    4

    右箭头标记

    s

    正方形标记

    p

    五边形标记

    *

    星形标记

    h

    六边形标记1

    'H'

    六边形标记2

    +

    加号标记

    x

    X标记

    D

    菱形标记

    'd'

    窄菱形标记

    |

    竖直线标记

    _

    水平线标记

    b

    蓝色

    g

    绿色

    r

    红色

    c

    青色

    m

    品红色

    y

    黄色

    k

    黑色

    w

    白色

    1. import numpy as np
    2. from matplotlib import pyplot as plt
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. x = np.arange(1, 11)
    6. y = 2 * x
    7. plt.title("我是标题")
    8. plt.xlabel("我是X轴")
    9. plt.ylabel("我是Y轴")
    10. plt.plot(x, y, "^m")
    11. plt.show()

    绘制柱状图

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. x = ['2018年', '2019年', '2020年', '2021年', '2022年']
    6. y = np.random.randint(0, 100, 5)
    7. plt.bar(x, y)
    8. plt.title("产量变化")
    9. plt.show()

    堆积柱状图

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. x = ['2018年', '2019年', '2020年', '2021年', '2022年']
    6. y1 = np.random.randint(10, 20, 5)
    7. y2 = np.random.randint(10, 20, 5)
    8. plt.bar(x, y1)
    9. plt.bar(x, y2, bottom=y1)
    10. plt.ylabel("产量(万亿吨)")
    11. plt.legend(labels=["粮食", "小麦"], loc="upper left")
    12. plt.show()

    绘制并列柱状图

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. x1 = np.arange(5)
    6. y1 = np.random.randint(10, 20, 5)
    7. y2 = np.random.randint(10, 20, 5)
    8. bar_width = 0.35
    9. plt.bar(x1, y1, bar_width)
    10. plt.bar(x1 + bar_width, y2, bar_width)
    11. plt.ylabel("产量(万亿吨)")
    12. tick_label = ['2018年', '2019年', '2020年', '2021年', '2022年']
    13. plt.xticks(x1 + bar_width / 2, tick_label)
    14. plt.legend(labels=["粮食", "小麦"], loc="upper left")
    15. plt.show()

    绘制直方图

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. import random
    4. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    5. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    6. a = [random.randint(80, 150) for i in range(250)]
    7. print(a)
    8. print(max(a) - min(a))
    9. # 计算组数
    10. d = 3 # 组距
    11. num_bins = (max(a) - min(a)) // d
    12. # 设置图形大小
    13. plt.figure(figsize=(20, 8), dpi=80)
    14. plt.hist(a, num_bins)
    15. # 设置x轴刻度
    16. plt.xticks(range(min(a), max(a) + d, d))
    17. # 设置网格
    18. plt.grid(alpha=0.4)
    19. plt.show()

    绘制饼图

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. labels =['2018年', '2019年', '2020年', '2021年', '2022年']
    6. y = np.random.rand(5)
    7. plt.pie(y,
    8. labels=labels,
    9. autopct="%3.1f%%",
    10. startangle=45, # 第一个饼片旋转角度
    11. shadow=True,
    12. pctdistance=0.8,
    13. labeldistance=1.2)
    14. plt.show()

      

    绘制分裂式饼图

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. labels =['2018年', '2019年', '2020年', '2021年', '2022年']
    6. y = np.random.rand(5)
    7. plt.pie(y,
    8. explode=(0.1, 0.1, 0.1, 0.1, 0.1), # 边缘偏离直径的百分比
    9. labels=labels,
    10. autopct="%3.1f%%",
    11. startangle=45, # 第一个饼片旋转角度
    12. shadow=True,
    13. pctdistance=0.8,
    14. labeldistance=1.2)
    15. plt.show()

    绘制散点图

    散点图又称为散点分布图,它以一个特征为横坐标,以另一个特征为纵坐标,利用坐标点(散点)的分布形态反映特征间的统计关系。
    散点图可以提供两类关键信息:
    特征之间是否存在数值或者数量的关联趋势,关联趋势是线性的还是非线性的
    如果某一个点或者某几个点偏离大多数点,则这些点就是离群值,可以进一步分析这些离群值是否在建模分析中产生很大的影响

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    4. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    5. x = np.arange(0, 100)
    6. y = np.random.normal(1, 20, 100)
    7. plt.scatter(x, y, label='坐标点', color='k', s=25, marker="o")
    8. plt.xlabel('X轴')
    9. plt.ylabel('Y轴')
    10. plt.title('标题')
    11. plt.legend()
    12. plt.show()

    绘制3D图像

    3D曲面图

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. from mpl_toolkits.mplot3d import Axes3D
    4. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    5. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    6. fig = plt.figure() # 使用figure对象
    7. ax = Axes3D(fig) # 创建3D轴对象
    8. X = np.arange(-2, 2, 0.1) # X坐标数据
    9. Y = np.arange(-2, 2, 0.1) # Y坐标数据
    10. X, Y = np.meshgrid(X, Y) # 计算3维曲面分格线坐标
    11. # 用于计算X/Y对应的Z值
    12. def f(x, y):
    13. return (1 - y ** 5 + x ** 5) * np.exp(-x ** 2 - y ** 2)
    14. # plot_surface()函数绘制对应的曲面
    15. ax.plot_surface(X, Y, f(X, Y), rstride=1, cstride=1)
    16. plt.show() # 显示图形

    3D散点图

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. from mpl_toolkits.mplot3d import Axes3D
    4. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    5. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    6. xs = np.random.randint(30, 40, 100)
    7. ys = np.random.randint(20, 30, 100)
    8. zs = np.random.randint(10, 20, 100)
    9. xs2 = np.random.randint(50, 60, 100)
    10. ys2 = np.random.randint(30, 40, 100)
    11. zs2 = np.random.randint(50, 70, 100)
    12. xs3 = np.random.randint(10, 30, 100)
    13. ys3 = np.random.randint(40, 50, 100)
    14. zs3 = np.random.randint(40, 50, 100)
    15. fig = plt.figure()
    16. ax = Axes3D(fig)
    17. ax.scatter(xs, ys, zs)
    18. ax.scatter(xs2, ys2, zs2, c='r', marker='^')
    19. ax.scatter(xs3, ys3, zs3, c='g', marker='*')
    20. ax.set_xlabel('X label')
    21. ax.set_ylabel('Y label')
    22. ax.set_zlabel('Z label')
    23. plt.show()

    3D条状图

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. from mpl_toolkits.mplot3d import Axes3D
    4. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
    5. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    6. x = np.arange(8)
    7. y = np.random.randint(0, 10, 8)
    8. y2 = y + np.random.randint(0, 3, 8)
    9. y3 = y2 + np.random.randint(0, 3, 8)
    10. y4 = y3 + np.random.randint(0, 3, 8)
    11. y5 = y4 + np.random.randint(0, 3, 8)
    12. clr = ['red', 'green', 'blue', 'black', 'white', 'yellow', 'orange', 'pink']
    13. fig = plt.figure()
    14. ax = Axes3D(fig)
    15. ax.bar(x, y, 0, zdir='y', color=clr)
    16. ax.bar(x, y2, 10, zdir='y', color=clr)
    17. ax.bar(x, y3, 20, zdir='y', color=clr)
    18. ax.bar(x, y4, 30, zdir='y', color=clr)
    19. ax.bar(x, y5, 40, zdir='y', color=clr)
    20. ax.set_xlabel('X label')
    21. ax.set_ylabel('Y label')
    22. ax.set_zlabel('Z label')
    23. plt.show()

  • 相关阅读:
    beyond compare如何设置只比较实际内容?(使用关联规则比较)
    栈和队列题目练习
    红队信息收集自动化工具-水泽(ShuiZe)
    网络中使用最多的图片格式有哪些
    系统升级丨酷雷曼4大功能优化,轻松完成VR全景制作
    跟我学Python图像处理丨关于图像金字塔的图像向下取样和向上取样
    猿创征文|vue中使用Axios最佳实践
    太原元宇宙3D交互展厅搭建让观众能够与企业进行更加紧密的沟通和交流
    Unity 顶点vertices,uv,与图片贴图,与mesh
    RHCE-day1
  • 原文地址:https://blog.csdn.net/feng8403000/article/details/126030496