• Python matplotlib 画图入门 03 绘图线


    Matplotlib 绘图线

    绘图过程如果我们自定义线的样式,包括线的类型、颜色和大小等。

    线的类型

    线的类型可以使用 linestyle 参数来定义,简写ls

    类型简写说明
    'solid' (默认) '-' 实线
    'dotted' ':' 点虚线
    'dashed' '--' 破折线
    'dashdot' '-.' 点划线
    'None' '' 或 ' ' 不画线

    实例

    import matplotlib.pyplot as plt
    import numpy as np
    
    ypoints = np.array([6, 2, 13, 10])
    
    plt.plot(ypoints, linestyle = 'dotted')
    plt.show()

    显示结果如下:

    使用简写:

    实例

    import matplotlib.pyplot as plt
    import numpy as np
    
    ypoints = np.array([6, 2, 13, 10])
    
    plt.plot(ypoints, ls = '-.')
    plt.show()

    显示结果如下:

    线的颜色

    线的颜色可以使用 color 参数来定义,简写为 c

    颜色类型:

    颜色标记描述
    'r' 红色
    'g' 绿色
    'b' 蓝色
    'c' 青色
    'm' 品红
    'y' 黄色
    'k' 黑色
    'w' 白色

    当然也可以自定义颜色类型,例如:SeaGreen、#8FBC8F 等,完整样式可以参考 HTML 颜色值。

    实例

    import matplotlib.pyplot as plt
    import numpy as np
    
    ypoints = np.array([6, 2, 13, 10])
    
    plt.plot(ypoints, color = 'r')
    plt.show()

    显示结果如下:

    实例

    import matplotlib.pyplot as plt
    import numpy as np
    
    ypoints = np.array([6, 2, 13, 10])
    
    plt.plot(ypoints, c = '#8FBC8F')
    plt.show()

    显示结果如下:

    实例

    import matplotlib.pyplot as plt
    import numpy as np
    
    ypoints = np.array([6, 2, 13, 10])
    
    plt.plot(ypoints, c = 'SeaGreen')
    plt.show()

    显示结果如下:

    线的宽度

    线的宽度可以使用 linewidth 参数来定义,简写为 lw,值可以是浮点数,如:12.05.67 等。

    实例

    import matplotlib.pyplot as plt
    import numpy as np
    
    ypoints = np.array([6, 2, 13, 10])
    
    plt.plot(ypoints, linewidth = '12.5')
    plt.show()

    显示结果如下:

    多条线

    plot() 方法中可以包含多对 x,y 值来绘制多条线

    实例

    import matplotlib.pyplot as plt
    import numpy as np
    
    y1 = np.array([3, 7, 5, 9])
    y2 = np.array([6, 2, 13, 10])
    
    plt.plot(y1)
    plt.plot(y2)
    
    plt.show()

    从上图可以看出 x 的值默认设置为 [0, 1, 2, 3]

    显示结果如下:

    我们也可以自己设置 x 坐标等值:

    实例

    import matplotlib.pyplot as plt
    import numpy as np
    
    x1 = np.array([0, 1, 2, 3])
    y1 = np.array([3, 7, 5, 9])
    x2 = np.array([0, 1, 2, 3])
    y2 = np.array([6, 2, 13, 10])
    
    plt.plot(x1, y1, x2, y2)
    plt.show()

    显示结果如下:

    REF

    https://www.runoob.com/matplotlib/matplotlib-line.html

  • 相关阅读:
    沙盒解决方案与场解决方案之间的差异
    Windows 7:77 个 Windows 7 提示
    SharePoint disable loopback check
    SharePoint 2010 工作流解决方案:序言
    SharePoint 2010 查看“运行时错误”
    sharepoint 链接库链接在新窗口打开
    如何启用SharePoint 2010的代码块
    沙盒解决方案注意事项
    ie8.0 不能用document.all兼容IE7模式
    php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
  • 原文地址:https://www.cnblogs.com/emanlee/p/16021281.html
Copyright © 2020-2023  润新知