• matplotlib绘制折线图


    1.一个简单的折线图

    1. # 从matplotlib导入pyplot
    2. from matplotlib import pyplot as plt
    3. # squares是要绘制的数据
    4. squares = [1, 4, 9, 16, 25]
    5. # 下面的函数plt.plot将数据绘制到二维坐标系下。
    6. # 如果只提供一个维度的数据例如squares,会被认为是Y坐标。
    7. # 默认的横坐标是 [ i for i in range(len(squares)) ]
    8. plt.plot(squares)
    9. # 显示绘制的曲线图
    10. plt.show()

     运行上面的脚本,会在一个窗口中看到下图

     2.设置线的粗线,图的标题,X/Y轴标签, 坐标轴刻度大小

    1. from matplotlib import pyplot as plt
    2. squares = [1, 4, 9, 16, 25]
    3. # 参数linewidth:设置线的粗细
    4. plt.plot(squares, linewidth=5)
    5. # 折线图的标题
    6. plt.title("Square Numbers", fontsize=24)
    7. # 折线图的横轴标签
    8. plt.xlabel("Value", fontsize=14)
    9. # 折线图的纵轴标签
    10. plt.ylabel("Square value", fontsize=14)
    11. # 刻度标记大小
    12. plt.tick_params(axis="both", labelsize=14)
    13. plt.show()

    运行结果

    3.修改默认的横坐标数据

    上面的折线图使用了默认的X坐标值,导致起始点对应的横坐标是0,纵坐标是1,不太合理(0的平方并不是1),接下来通过指定正确的横纵坐标值,来绘制正确的折线图。

    1. from matplotlib import pyplot as plt
    2. input_values = [1, 2, 3, 4, 5]
    3. squares = [1, 4, 9, 16, 25]
    4. # 这里分别传入参数X轴 Y轴数据
    5. plt.plot(input_values, squares, linewidth=5)
    6. plt.title("Square Numbers", fontsize=24)
    7. plt.xlabel("Value", fontsize=14)
    8. plt.ylabel("Square value", fontsize=14)
    9. plt.tick_params(axis="both", labelsize=14)
    10. plt.show()

    运行结果,折线的起点横坐标是从1开始的

    4.填充两条折线图之间的部分

    1. plt.plot(dates, highs, c='red', alpha=0.5)
    2. plt.plot(dates, lows, c='blue', alpha=0.5)
    3. # dates代表X轴数值 highs, lows分别是两条折线的Y轴数值
    4. plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1)

     

  • 相关阅读:
    No141.精选前端面试题,享受每天的挑战和学习
    【2】Spring Boot 3 项目搭建
    【数据库原理及应用】——数据库设计(学习笔记)
    Dubbo的高级特性:服务治理篇
    C++笔记 13 (STL初识)
    第七章 块为结构建模 P2|系统建模语言SysML实用指南学习
    OAuth2:搭建授权服务器
    简单python画图
    CalBioreagents 绵羊抗α-2-HS糖蛋白 亲和纯化说明
    伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
  • 原文地址:https://blog.csdn.net/qq_26686565/article/details/126117193