【问题标题】:Is there a list of line styles in matplotlib?matplotlib 中是否有线条样式列表?
【发布时间】:2012-11-01 20:02:38
【问题描述】:

我正在编写一个脚本来进行一些绘图。我希望它绘制几个数据系列,每个系列都有其独特的线条样式(不是颜色)。我可以轻松地遍历一个列表,但是 python 中是否已经有这样的列表?

【问题讨论】:

  • 我知道这不是您真正要搜索的内容,但我的回答中确实列出了它们here

标签: python matplotlib linestyle


【解决方案1】:

你可以从the Linestyle example复制字典:

from collections import OrderedDict

linestyles = OrderedDict(
    [('solid',               (0, ())),
     ('loosely dotted',      (0, (1, 10))),
     ('dotted',              (0, (1, 5))),
     ('densely dotted',      (0, (1, 1))),

     ('loosely dashed',      (0, (5, 10))),
     ('dashed',              (0, (5, 5))),
     ('densely dashed',      (0, (5, 1))),

     ('loosely dashdotted',  (0, (3, 10, 1, 10))),
     ('dashdotted',          (0, (3, 5, 1, 5))),
     ('densely dashdotted',  (0, (3, 1, 1, 1))),

     ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
     ('dashdotdotted',         (0, (3, 5, 1, 5, 1, 5))),
     ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))])

然后您可以遍历线条样式

fig, ax = plt.subplots()

X, Y = np.linspace(0, 100, 10), np.zeros(10)
for i, (name, linestyle) in enumerate(linestyles.items()):
    ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black')

ax.set_ylim(-0.5, len(linestyles)-0.5)

plt.show()

或者你只是从中取出一个线条样式,

ax.plot([0,100], [0,1], linestyle=linestyles['loosely dashdotdotted'])

【讨论】:

  • linestyle=(0, (5, 10)) 可以,但ls=(0, (5, 10)) 不行
【解决方案2】:

根据the doc,您可以通过以下方式找到它们:

from matplotlib import lines
lines.lineStyles.keys()
>>> ['', ' ', 'None', '--', '-.', '-', ':']

你可以用markers做同样的事情

编辑:在latest versions 中,仍然有相同的样式,但您可以改变点/线之间的间距。

【讨论】:

  • 标记的语法似乎略有不同:from matplotlib import markers; markers.MarkerStyle.markers.keys()
  • 在 2.1.2 版本中,lines.lineMarkers.keys() 产生与markers.MarkerStyle.markers.keys() 相同的结果。
  • 很遗憾,只有 4 个选项,但我有 5 行要绘制。
【解决方案3】:

我没有直接回答访问列表的问题,但在此页面上提供另一种选择会很有用:还有另一种方法可以生成虚线样式。

您可以在 A 和 B 之间生成带有横向条纹的线条,例如

A |||||||||||||||||||||||||||||||||||||||||||| |乙

一个 || || || || || || || || || || || || || || || ||乙

一个 | | | | | | | | | | | | | | | | | | | | | | | |乙

通过增加线条的宽度指定自定义的破折号模式:

ax.plot(x, y, dashes=[30, 5, 10, 5])

matplotlib.lines.Line2Dsays this 关于set_dashes(seq)的文档:

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

接受:以点为单位的开/关墨水顺序

我认为它可以说得更好:当它画线时,数字序列指定沿着线画多少点,然后省略多少点(如果有两个数字),多少已涂漆,有多少未涂漆(如果序列中有四个数字)。使用四个数字,您可以生成一条点划线:[30, 5, 3, 5] 给出一个 30 点长的破折号、一个 5 点的间隔、一个 3 点的破折号(一个点)和一个 5点差距。然后重复。

This page of the documentation 解释了这种可能性。 Look here 有两种不同的做法。

【讨论】:

    【解决方案4】:

    plot 文档

    http://matplotlib.org/1.5.3/api/pyplot_api.html#matplotlib.pyplot.plot 有一个线条+标记样式列表:

    character description
    '-'       solid line style
    '--'      dashed line style
    '-.'      dash-dot line style
    ':'       dotted line style
    '.'       point marker
    ','       pixel marker
    'o'       circle marker
    'v'       triangle_down marker
    '^'       triangle_up marker
    '<'       triangle_left marker
    '>'       triangle_right marker
    '1'       tri_down marker
    '2'       tri_up marker
    '3'       tri_left marker
    '4'       tri_right marker
    's'       square marker
    'p'       pentagon marker
    '*'       star marker
    'h'       hexagon1 marker
    'H'       hexagon2 marker
    '+'       plus marker
    'x'       x marker
    'D'       diamond marker
    'd'       thin_diamond marker
    '|'       vline marker
    '_'       hline marker
    

    由于这是pyplot.plot 文档字符串的一部分,您也可以通过以下方式从终端查看它:

    import matplotlib.pyplot as plt
    help(plt.plot)
    

    【讨论】:

    • 由于pyplot.plot() 的文档中列出了线条样式,因此可以通过阅读该函数的文档字符串在本地查看它们:import matplotlib.pyplot as plt; ?plt.plot。标记和线条样式列在最后的“注释”部分。
    • 还有linestyle=(0, (5, 10)) 用于自定义虚线
    【解决方案5】:

    在 python3 中,.keys() 方法返回一个 dict_keys 对象,而不是一个 list。 您需要将结果传递给 list() 以便能够像在 python2 中那样对数组进行索引。例如this SO question

    因此,要为线条、颜色和标记创建可迭代数组,您可以使用类似的东西。

    import matplotlib
    colors_array = list(matplotlib.colors.cnames.keys())
    lines_array = list(matplotlib.lines.lineStyles.keys())
    markers_array = list(matplotlib.markers.MarkerStyle.markers.keys())
    

    【讨论】:

      【解决方案6】:

      根据我的经验,将颜色和标记放在一个列表中非常好,这样我可以在绘图时遍历它们。

      以下是我获取此类内容列表的方法。它需要一些挖掘。

      import matplotlib
      colors_array = matplotlib.colors.cnames.keys()
      markers_array = matplotlib.markers.MarkerStyle.markers.keys()
      

      【讨论】:

        猜你喜欢
        • 2021-02-03
        • 1970-01-01
        • 2020-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-28
        相关资源
        最近更新 更多