• 技术文章 >头条

    python Matplotlib有哪些图形画法?

    小妮浅浅小妮浅浅2021-04-27 10:11:11原创5053

    本篇要带来的是普通图和条形图的画法,大家在理解了Matplotlib的简单用法后,一起来看看具体的图形吧。

    1.普通图

    常用函数可以直接用 plt.plot()* 对象。如:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    x = np.linspace(-10,10,100)

    y = 1/(1+np.exp(-x)) # sigmoid 函数

    z = np.sin(x)

    plt.figure(figsize=(10,5))

    plt.plot(x,y-0.5,label="$\sigma (x)$",color="r",linewidth=2)

    plt.plot(x,z,label="sin(x)",color="b",linewidth=2.5)

      

    plt.title("Matplotlib Figure: koding") #图表标题

    plt.legend()    #显示图形标签

    plt.grid()      #显示网格

    plt.show()      #显示绘图窗口

    2.条形图

    条形图可以利用 plt.bar() (axis.bar())对象,默认是垂直条形。水平条形图可以利用 plt.barh()

    语法

    1

    plt.bar(x, height, width, bottom=None, *, align=‘center’, data=None, **kwargs)

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    import numpy as np

    import matplotlib.pyplot as plt

      

    X = ['G1','G2','G3','G4','G5'] # X 轴标签

    A = [20, 35, 30, 35, 27] #A 类高度

    B = [25, 32, 34, 20, 25] #B 类高度

    aerr = [2, 3, 4, 2, 2] #误差线长度

    berr = [3, 5, 2, 3, 3] #误差线长度

      

    fig= plt.figure(figsize=(8,6))

    ax = fig.add_subplot()

      

    # A 类的条形图

    ax.bar(X,   #每个条形的 X 坐标标签

           A,   #每个条形的高度

           yerr=aerr,         #垂直误差线长度

           ecolor = "yellow", #垂直误差线长颜色

           width = 0.9,       #每个条形的宽度

           label = "Class A", #每类条形的标签

           color = "green",   #条形的颜色

           edgecolor = "red", #条形的边缘线颜色

           linestyle = ":",   #条形的边缘线样式

           linewidth = 3.5,   #条形的边缘线宽度

           hatch = "/"        #填充图案样式

           ) # plt.bar 也行

    # B 类的条形图,底部高度应该在A类上方。其余具体属性均没设置。

    ax.bar(X,B,yerr=berr,bottom=A,label="Class B") # plt.bar 也行

      

    ax.set_ylabel("Scores") ## plt.ylabel() 也行

    ax.set_title("Scores by Class A & B") ## plt.title() 也行

    ax.legend() ## plt.legend() 也行

      

    plt.show()

    概念

    1)Matplotlib是一个python的包

    2)用于2D绘图(也可以绘制3D,但是要安装一些支持的工具包)

    3)非常强大非常流行

    4)有很多扩展

    以上就是python Matplotlib的一些图形画法,大家在掌握了相关的Matplotlib使用后,可以就上方的图形进行代码的运行试验。

    (推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

    专题推荐:python matplotlib
    上一篇:python中pandas.Dataframe合并的方法有哪些? 下一篇:java中spring ioc有几种注入?

    相关文章推荐

    • python中datetime和字符串之间如何转换• python中csv如何设置表头?• python中逻辑回归算法是什么?• python使用第三方模块进行连接• SVM在python中的原理如何理解?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网