我正在编写一个脚本,它将进行一些绘图。 我希望它能绘制几个数据系列,每个系列都有其独特的线条样式(不是颜色)。 我可以很容易地遍历一个列表,但Python中是否已经提供了这样的列表?
我正在编写一个脚本,它将进行一些绘图。 我希望它能绘制几个数据系列,每个系列都有其独特的线条样式(不是颜色)。 我可以很容易地遍历一个列表,但Python中是否已经提供了这样的列表?
from matplotlib import markers; markers.MarkerStyle.markers.keys()
。 - joelostblomlines.lineMarkers.keys()
产生的结果与markers.MarkerStyle.markers.keys()
相同。 - Matt Hancockplot
文档
https://matplotlib.org/3.5.3/api/markers_api.html (之前是 https://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
在本地查看。标记和线条样式在末尾的“注释”部分中列出。 - joelostblomlinestyle=(0, (5, 10))
用于自定义虚线。 - mins根据我的经验,将颜色和标记放在列表中很好,这样我就可以在绘图时对它们进行迭代。
以下是如何获取此类内容列表的方法。需要一些挖掘。
import matplotlib
colors_array = matplotlib.colors.cnames.keys()
markers_array = matplotlib.markers.MarkerStyle.markers.keys()
您可以从线条样式示例中复制字典:
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))
will work, but not ls=(0, (5, 10))
- mins.keys()
方法返回的是一个dict_keys
对象而不是一个list
。
如果想要像在Python2中一样索引数组,则需要将结果传递给list()
。例如:这个SO问题
因此,要创建可迭代的行、颜色和标记数组,您可以使用以下代码:import matplotlib
colors_array = list(matplotlib.colors.cnames.keys())
lines_array = list(matplotlib.lines.lineStyles.keys())
markers_array = list(matplotlib.markers.MarkerStyle.markers.keys())
我没有直接回答如何访问列表的问题,但在这个页面上有一个额外的有用的选择:还有一种生成虚线样式的方法。
您可以通过横向条纹在A和B之间生成线条,例如
A ||||||||||||||||||||||||||||||||||||||||||||||| B
A || || || || || || || || || || || || || || || || B
A | | | | | | | | | | | | | | | | | | | | | | | | B
通过增加线条宽度并指定自定义虚线模式:
ax.plot(x, y, dashes=[30, 5, 10, 5])
matplotlib.lines.Line2D
的文档(链接)中关于set_dashes(seq)
的说明: