• matplotlib入门详细教程


    show()——显示绘制图形

    import matplotlib.pyplot as plt    #引入pyplot模块
    plt.show()    # 显示绘制的图形,当我们绘制后调用,在jupyter notebook里自带此函数
    
    • 1
    • 2

    plot()——画线

    plot(列表x轴值,列表y轴值)
    
    import matplotlib.pyplot as plt    #引入pyplot模块
    import numpy as np   #引入numpy库
    x=np.arange(-10,11)  #创建一个-10-10的数组
    y=np.arange(-100,110,10) #创建一个-100-110,间隔为10的数组
    plt.plot(x,y)
    plt.show()  #在jupyter notebook里自带此函数
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    输出结果:
    在这里插入图片描述

    linewidth——线条宽度

    plt.plot(x,y,linewidth=20)
    
    • 1
    import matplotlib.pyplot as plt    #引入pyplot模块
    import numpy as np   #引入numpy库
    x=np.arange(-10,11)  #创建一个-10-10的数组
    y=np.arange(-100,110,10) #创建一个-100-110,间隔为10的数组
    plt.plot(x,y,linewidth=20)  # linewidth=5是正常宽度
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    import matplotlib.pyplot as plt    #引入pyplot模块
    import numpy as np   #引入numpy库
    x=np.arange(-10,11)  #创建一个-10-10的数组
    y=np.arange(-100,110,10) #创建一个-100-110,间隔为10的数组
    plt.plot(x,y,linewidth=5)  # 正常宽度
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    title()、xlabel()、ylabel()——标题的显示

    plt.title("string")     #图的标题string
    plt.xlabel("string") #x轴的标题string
    plt.ylabel("string") #y轴的标题string
    
    • 1
    • 2
    • 3
    import matplotlib.pyplot as plt    #引入pyplot模块
    import numpy as np   #引入numpy库
    x=np.arange(-10,11)  #创建一个-10-10的数组
    y=np.arange(-100,110,10) #创建一个-100-110,间隔为10的数组
    plt.plot(x,y,linewidth=20)  # linewidth=5是正常宽度
    
    plt.title("y=x * 10")     #图的标题
    plt.xlabel("value_x") #x轴的标题
    plt.ylabel("value_y") #y轴的标题
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    xticks()、yticks()——设置x、y轴刻度

    xticks(ticks, [labels], **kwargs)
    ticks:数组类型,用于设置X轴刻度间隔
    [labels]:数组类型,用于设置每个间隔的显示标签
    **kwargs:用于设置标签字体倾斜度rotation和颜色color等外观属性。
    
    • 1
    • 2
    • 3
    • 4
    import matplotlib.pyplot as plt    #引入pyplot模块
    import numpy as np   #引入numpy库
    x=np.arange(-10,11)  #创建一个-10-10的数组
    y=np.arange(-100,110,10) #创建一个-100-110,间隔为10的数组
    plt.plot(x,y,linewidth=5)  # linewidth=5是正常宽度
    plt.title("y=x * 10")     #图的标题
    plt.xlabel("value_x") #x轴的标题
    plt.ylabel("value_y") #y轴的标题
    
    xlabels=['T%s'%i for i in x]
    plt.xticks(x,xlabels,rotation=45,color="blue")#倾斜45度,x轴刻度为蓝色
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    legend()——图例说明

    a=plt.plot(数组x,数组y,label="数值a")   #定义个label
    b=plt.plot(数组x,数组y,label="数值b")   #定义个label
    plt.legend(a,b)    #调用legend()
    
    • 1
    • 2
    • 3
    import matplotlib.pyplot as plt    #引入pyplot模块
    import numpy as np   #引入numpy库
    x=np.arange(-10,11)  #创建一个-10-10的数组
    y=np.arange(-100,110,10) #创建一个-100-110,间隔为10的数组
    plt.plot(x,y,linewidth=5)  # linewidth=5是正常宽度
    plt.title("y=x * 10")     #图的标题
    plt.xlabel("value_x") #x轴的标题
    plt.ylabel("value_y") #y轴的标题
    xlabels=['T%s'%i for i in x]
    plt.xticks(x,xlabels,rotation=45,color="blue")#倾斜45度,x轴刻度为蓝色
    
    plt.plot(x,y,label="数值a")   #定义个label
    plt.legend()    #调用legend()
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

  • 相关阅读:
    JNI编程之java层和native层的数组数据的交互
    [MyBatisPlus]DQL编程控制①(条件查询)
    【单片机基础】C51语言基础
    企业应用现代化之 如何快、准、狠地进行容器化改造?
    在AgilePLM项目中使用积木报表
    YOLOv8改进 | 注意力机制 | 在主干网络中添加MHSA模块【原理+附完整代码】
    Mac图像编辑器Pixelmator Pro
    二进制搭建 Kubernetes+部署网络组件+部署CornDNS+负载均衡部署+部署Dashboard
    Array题型之双指针(TwoPointers) [leetcode][数据结构]
    简述电子企业MES管理系统解决方案的实施策略
  • 原文地址:https://blog.csdn.net/m0_63568001/article/details/126179693