关于python:matplotlib中是否有线型列表?

Is there a list of line styles in matplotlib?

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


根据文档,您可以通过执行以下操作找到它们:

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

您可以使用标记做同样的事情

编辑:在最新版本中,仍然有相同的样式,但是您可以改变点/线之间的间隔。


plot文档

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

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
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文档字符串的一部分,因此您还可以从终端使用以下命令查看它:

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


根据我的经验,在列表中包含颜色和标记是很好的,这样我在绘制时就可以遍历它们。

这是我获取此类物品清单的方式。花了一些时间。

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

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

因此,要创建线条,颜色和标记的可迭代数组,可以使用类似方法。

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

您可以从Linestyle示例中复制字典:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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)))])

然后,您可以遍历线型

1
2
3
4
5
6
7
8
9
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()

或者您只从其中选择一种线型,

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

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

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

A ||||||||||||||||||||||||||||||||||||||||||||||| B

A || || || || || || || || || || || || || || || || B

A | | | | | | | | | | | | | | | | | | | | | | | | B

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

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

matplotlib.lines.Line2D的文档说明有关set_dashes(seq)的信息:

Set the dash sequence, sequence of dashes with on off ink in points. If seq is empty or if seq = (None, None), the linestyle will be set to solid.

ACCEPTS: sequence of on/off ink in points

我认为应该说得更好:在绘制直线时,数字序列指定沿直线绘制了多少个点,然后省略了多少个点(如果有两个数字),绘制了多少,有多少未上漆(如果序列中有四个数字)。使用四个数字,您可以生成点划线:[30、5、3、5]给出30点长的虚线,5点间隙,3点破折号(点)和5点差距。然后重复。

文档的本页说明了这种可能性。在这里寻找两种不同的方法。