【问题标题】:Are there really only 4 Matplotlib Line Styles?真的只有 4 个 Matplotlib 线型吗?
【发布时间】:2016-02-29 09:38:11
【问题描述】:

我一直在 matplotlib 中寻找新的线型,唯一可用的线型是 ["-", "--", "-.", ":",]。 (样式选项 ['', ' ', 'None',] 不计算在内,因为它们只是隐藏了线条。)

Matplotlib pyplot 中真的只有 4 种线条样式吗?是否有任何扩展可以添加更多线条样式?有没有办法自定义线条样式?一些三种字符线样式怎么样:

  • '--.':破折号点
  • '-..':点划线点
  • '...':点点点(空格)
  • 'xxx': x 在一行中
  • '\/':之字形,即'\/\/\/\/'
  • '::':平行点,即:::::

这些只是扩展线条样式范围的一些想法。

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    您可以使用dashes kwarg 设置自定义破折号样式。

    来自docs

    设置破折号序列,破折号序列,以点数为单位。如果 seq 为空或 seq = (None, None),则线型将设置为solid。

    以下是基于您的一些建议的一些示例。显然,您可以通过多种方式对其进行自定义。

    import matplotlib.pyplot as plt
    
    fig,ax = plt.subplots(1)
    
    # 3 dots then space
    ax.plot(range(10), range(10),     dashes=[3,6,3,6,3,18],  lw=3,c='b')
    
    # dash dash dot
    ax.plot(range(10), range(0,20,2), dashes=[12,6,12,6,3,6], lw=3,c='r')
    
    # dash dot dot
    ax.plot(range(10), range(0,30,3), dashes=[12,6,3,6,3,6],  lw=3,c='g')
    

    【讨论】:

      【解决方案2】:

      我想补充一些 2021 年的额外信息。 在 matplotlib 版本中。 3.3.4 dashing 选项有点不同。您首先循环浏览破折号的类型,然后添加比率。

      import matplotlib.pyplot as plt
      
      plt.figure()
      
      # dashed with dash length 10, space length 5
      plt.hlines(3, 0, 5, linestyle="--", dashes=(0,(10,5)))
      
      # dashed with dash length 5, space length 10
      plt.hlines(6, 0, 5, linestyle="--", dashes=(0,(5,10)))
      
      plt.ylim(0,10)
      plt.show()
      
      

      结果:

      更多信息请查看matplotlib documentation

      【讨论】:

        猜你喜欢
        • 2012-08-31
        • 1970-01-01
        • 2016-05-24
        • 2020-12-01
        • 2010-09-08
        • 2011-02-12
        • 1970-01-01
        • 2016-09-19
        相关资源
        最近更新 更多