• matplotlib库基本使用方法(一)



    matplotlib功能初探


    为了更好地理解所有基础绘图功能,我们通过天气温度变化的绘图来融合所有的基础API使用

    需求:画出某城市11点到12点1小时内每分钟的温度变化折线图,温度范围在15度~18度

    效果:
    准备数据并画出初始折线图

    import matplotlib.pyplot as plt
    import random
    
    # 画出温度变化图
    # 0.准备x, y坐标的数据
    x = range(60)
    y_shanghai = [random.uniform(15, 18) for i in x]
    
    # 1.创建画布
    plt.figure(figsize=(20, 8), dpi=80)
    
    # 2.绘制折线图
    plt.plot(x, y_shanghai)
    
    # 3.显示图像
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    效果
    在这里插入图片描述

    添加自定义x,y刻度

    # 画出温度变化图
    
    # 0.准备x, y坐标的数据
    x = range(60)
    y_shanghai = [random.uniform(15, 18) for i in x]
    
    # 构造x轴刻度标签
    x_ticks_label = ["11点{}分".format(i) for i in x]
    # 构造y轴刻度
    y_ticks = range(40)
    
    # 修改x,y轴坐标的刻度显示
    plt.xticks(x[::5], x_ticks_label[::5])
    plt.yticks(y_ticks[::5])
    
    # 1.创建画布
    plt.figure(figsize=(20, 8), dpi=80)
    
    # 2.绘制折线图
    plt.plot(x, y_shanghai)
    
    # 3.显示图像
    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

    效果
    在这里插入图片描述

    添加网格显示
    linestytle可以由很多种形式,譬如虚线,实线,alpha代表透明度

    #为了更加清楚地观察图形对应的值
    plt.grid(True, linestyle='--', alpha=0.5)
    
    • 1
    • 2

    添加描述信息
    添加x轴、y轴描述信息及标题,通过fontsize参数可以修改图像中字体的大小

    plt.xlabel("时间")
    plt.ylabel("温度")
    plt.title("中午11点0分到12点之间的温度变化图示", fontsize=20)
    
    • 1
    • 2
    • 3

    图像保存

    # 保存图片到指定路径
    plt.savefig("test.png")
    
    • 1
    • 2

    解决中文乱码问题:
    首先下载这中文包

     链接:https://pan.baidu.com/s/18s4lJwMXKQgGXLmmMoE22Q 
    
     提取码:dv57
    
    • 1
    • 2
    • 3

    查找本地文件存放目录。

    import matplotlib
    # 查找字体存放位置,把ttc格式的字体,放在这个文件夹内
    print(matplotlib.matplotlib_fname())
    # 查找字体缓存位置,放置新字体后,删除这个文件夹
    print(matplotlib.get_cachedir())
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    进入到自己的路径,更改配置文件,建议用vscode打开,用ctrl+f查找定位更改

    # 去掉前面的#     
    font.family         : sans-serif
    # 去掉前面的#,并在冒号后面添加SimHei
    font.sans-serif     : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif  
    # 去掉前面的#,并将True改为False
    axes.unicode_minus  : False
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    同时进入到ttf目录,把下载好的SimHei.ttf粘贴到这个目录。

    cd C:\ProgramData\Anaconda3\Lib\site-packages\matplotlib\mpl-data\fonts\ttf
    
    • 1

    在这里插入图片描述

    进入到C:\Users\Administrator.DESKTOP-I1RC48K.matplotlib目录,删除缓存文件。
    在这里插入图片描述
    如果上述方法都不行,那就暴力解决,每次绘图前加入下面两行

    import matplotlib.pyplot as plt
    
    # 设置字体为黑体,解决Matplotlib中文乱码问题
    plt.rcParams['font.sans-serif']=['SimHei']
    
    # 解决Matplotlib坐标轴负号'-'显示为方块的问题
    plt.rcParams['axes.unicode_minus']=False
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    本节完整代码

    import matplotlib.pyplot as plt 
    import random
    from pylab import mpl
    # 设置显示中文字体
    mpl.rcParams["font.sans-serif"] = ["SimHei"]
    # 设置正常显示符号
    mpl.rcParams["axes.unicode_minus"] = False
    
    x=range(60)
    y_shanghai=[random.uniform(15,18) for i in x]
    plt.figure(figsize=(20,8),dpi=100)
    x_ticks_label=["11点{}分".format(i) for i in x]
    y_ticks=range(46)
    plt.xticks(x[::5],x_ticks_label[::5])
    plt.yticks(y_ticks[::5])
    plt.plot(x,y_shanghai)
    plt.grid(True,linestyle='-',alpha=0.5)
    plt.xlabel("时间")
    plt.ylabel("温蒂")
    plt.title("中午11点0分到12点之间的温度变化图示",fontsize=20)
    plt.savefig("test.png")
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    8.25 学习
    Python中的依赖注入
    三、git信息泄露
    STC系列单片机内部EEPROM 测试程序
    22年国内最牛的Java面试八股文合集(全彩版),不接受反驳
    【牛客刷题】带你在牛客刷题第二弹(简单排序)
    latex cite命令、款式
    第一百五十九回 SliverAppBar组件
    linux私有化部署图片鉴黄docker版及nodejs版并通过java调用参考nsfwjs
    YOLOv5 PyQt5 | PyQt5环境配置及组件介绍 | 1/3
  • 原文地址:https://blog.csdn.net/weixin_42595747/article/details/126675338