• Matplotlib数据可视化基础


    Matplotlib 可能是 Python 2D-绘图领域使用最广泛的套件。它能让使用者很轻松地将数据图形化,并且提供多样化的输出格式。这里将会探索 matplotlib 的常见用法。

    image-20220701100342629

    1.引例

    In [ ]:

    import matplotlib.pyplot as plt
    import numpy as np

    In [ ]:

    image-20220701100445914

    #创建画布,指定画布大小
    plt.figure(figsize=(4,4))

    Out[ ]:

    <Figure size 288x288 with 0 Axes>
    <Figure size 288x288 with 0 Axes>

    In [ ]:

    # 生成0-9的十个整数数据
    np.arange(10)

    Out[ ]:

    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

    In [ ]:

    image-20220701100501993

    x=np.arange(10)
    # 设置图像标题
    plt.title('lines')
    #绘制图线一
    plt.plot(x,np.sin(x))
    #绘制图线二
    plt.plot(x,np.cos(x))
    #设置区分图线标志
    plt.legend(['sin','cos'])
    #保存图片
    plt.savefig('./tmp/tmp.png')
    #展示图片
    plt.show()

    img

    2.数据导入

    In [ ]:

    data=np.load('./data/国民经济核算季度数据.npz',allow_pickle=True)

    In [ ]:

    data.files

    Out[ ]:

    ['columns', 'values']

    In [ ]:

    data['columns']

    Out[ ]:

    array(['序号', '时间', '国内生产总值_当季值(亿元)', '第一产业增加值_当季值(亿元)', '第二产业增加值_当季值(亿元)',
    '第三产业增加值_当季值(亿元)', '农林牧渔业增加值_当季值(亿元)', '工业增加值_当季值(亿元)',
    '建筑业增加值_当季值(亿元)', '批发和零售业增加值_当季值(亿元)', '交通运输、仓储和邮政业增加值_当季值(亿元)',
    '住宿和餐饮业增加值_当季值(亿元)', '金融业增加值_当季值(亿元)', '房地产业增加值_当季值(亿元)',
    '其他行业增加值_当季值(亿元)'], dtype=object)

    In [ ]:

    data['values']

    Out[ ]:

    array([[1, '2000年第一季度', 21329.9, ..., 1235.9, 933.7, 3586.1],
    [2, '2000年第二季度', 24043.4, ..., 1124.0, 904.7, 3464.9],
    [3, '2000年第三季度', 25712.5, ..., 1170.4, 1070.9, 3518.2],
    ...,
    [67, '2016年第三季度', 190529.5, ..., 15472.5, 12164.1, 37964.1],
    [68, '2016年第四季度', 211281.3, ..., 15548.7, 13214.9, 39848.4],
    [69, '2017年第一季度', 180682.7, ..., 17213.5, 12393.4, 42443.1]],
    dtype=object)

    3.绘制散点图

    image-20220701100538399

    In [ ]:

    #绘制散点图
    plt.scatter(range(69),data['values'][:,2])
    plt.show()

    img

    In [ ]:

    #多种数据散点图
    for i in [3,4,5]:
    plt.scatter(range(69),data['values'][:,i])
    plt.legend(['1','2','3'])
    plt.show()

    img

    4.绘制折线图

    In [ ]:

    #绘制折线图
    plt.plot(range(69),data['values'][:,2])
    plt.show()

    img

    In [ ]:

    #多种数据绘制折线图
    l=['r','g','b'] #颜色
    m=['o','*','D'] #标记样式
    for j,i in enumerate([3,4,5]):
    plt.plot(range(69),data['values'][:,i],c=l[j],marker=m[j],alpha=0.5)
    plt.legend(['1','2','3'])
    plt.show()

    img

    5.绘制直方图

    image-20220701100622144

    In [ ]:

    #直方图
    data['values'][68,3:6]

    Out[ ]:

    array([8654.0, 70004.5, 102024.2], dtype=object)

    In [ ]:

    #赋值绘图数据
    num=data['values'][68,3:6]

    In [ ]:

    #绘制直方图添加数据
    plt.bar(range(len(num)),num)
    #给x轴标注刻度和取值
    plt.xticks(range(len(num)),['1','2','3'])
    plt.show()

    img

    6.绘制饼图

    image-20220701100658753

    In [ ]:

    #设置绘图区域为正方形
    plt.figure(figsize=(4,4))
    plt.pie(num,autopct='%.2f %%',explode=[0.1,0,0],labels=['1','2','3'],labeldistance=1.4)
    plt.show()

    img

    7.绘制箱线图

    In [ ]:

    #设置单个数据绘制箱线图
    num=data['values'][:,3]
    plt.boxplot(num)
    plt.show()

    img

    In [ ]:

    #设置多个箱线图绘制在一张图中
    num=(list(data['values'][:,3]),list(data['values'][:,4]),list(data['values'][:,5]))
    plt.boxplot(num)
    plt.show()

    img

    5.参考文章

    Matplotlib

    【创作不易,望点赞收藏,若有疑问,请留言,谢谢】

  • 相关阅读:
    一篇五分生信临床模型预测文章代码复现——Figure 4-6 临床模型构建(二)
    LeetCode刷题笔记【24】:贪心算法专题-2(买卖股票的最佳时机II、跳跃游戏、跳跃游戏II)
    如何在Web应用中添加一个JavaScript Excel查看器
    【c语言】数据在内存中的存储
    Cholesterol-PEG-Amine,CLS-PEG-NH2,胆固醇-聚乙二醇-氨基脂质衍生物试剂供应
    LeetCode //C - 18. 4Sum
    巨神奇,2013年的老Mac,竟直接装上macOS Ventura 13.1 Beta版
    HTTP的详细介绍
    手机NFC录入门禁数据,实现手机开门
    华为晟思Mindspore初体验:全场景统一式框架与函数式微分编程
  • 原文地址:https://www.cnblogs.com/dongxuelove/p/16433576.html