• matplotlib绘制曲线图


    前言

    工作中经常需要使用matplotlib库绘制曲线图,在此记录,以备后用。

    一、绘制单图

    将一条或多条曲线绘制在一张图上。下面代码以绘制两条曲线为例。

    import matplotlib.pyplot as plt
    
    # 显示设置
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False   # 负号显示
    plt.rcParams['font.family'] = 'Times New Roman'  # 字体
    plt.rcParams['font.style'] = 'normal'  # 正体normal, 斜体italic
    plt.rcParams['font.size'] = 14  # 字号
    
    fig, ax = plt.subplots(figsize=(8, 6))
    ax.plot(x1, y, label="label1", color='r',linewidth=1.5)
    ax.plot(x2, y, label="label2", color='g',linewidth=1.5)
    ax.set_xticks([i for i in range(-70, 110, 10)])  # x轴坐标范围
    ax.set_yticks([i for i in range(0, 12)])  # y轴坐标范围
    ax.set_xlabel("x_value")  # x轴标签
    ax.set_ylabel("y_value")  # y轴标签
    ax.set_title("title")     # 标题
    plt.grid(b=True, linestyle="--", alpha=0.5)  # 显示栅格
    plt.legend()  # 显示图例
    plt.savefig(save_path, bbox_inches='tight', pad_inches=0.2)  # 保存
    plt.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    二、绘制子图

    绘制三个子图,并共享y轴。

    plt.figure(figsize=(8, 6))
    fig, ax = plt.subplots(1, 3, sharey='all')
    for i in [2, 1, 0]:
        ax[i].plot(x[i], y[i], label[i], marker="+", color='r', linewidth=1)
        ax[i].set_xticks([i for i in range(0, 110, 20)])  # x轴坐标范围
    	ax[i].set_yticks([i for i in range(0, 12)])  # y轴坐标范围
    	ax[i].set_xlabel("x_value")  # x轴标签
    	ax[i].grid(b=True, linestyle="--", alpha=0.5)  # 显示栅格
    	ax[i].legend()  # 显示图例	
    ax[0].set_ylabel("y_value")  # y轴标签
    plt.suptitle("title")     # 标题
    fig.set_size_inches(14, 7)
    plt.savefig(save_path, bbox_inches='tight', pad_inches=0.2)  # 保存
    plt.clf()
    plt.cla()
    plt.close("all")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    END
  • 相关阅读:
    java学习之spirng的aop
    随手记:uniapp图片展示,剩余的堆叠
    如何压缩图片的大小?这两个方法了解过吗
    pointpillars--kitti训练
    leetcode 762. 二进制表示中质数个计算置位
    淘宝API关键词搜索接口调用示例
    python+django+vue+Elementui人力资源管理系统
    MongoDB数据库
    软件开发基础【信息系统监理师】
    DFP 数据转发协议 规则说明(二)
  • 原文地址:https://blog.csdn.net/weixin_40356612/article/details/134464806