我使用Matplotlib在图形上绘制线条。现在,我想为线上的各个点设置样式,特别是标记。我该怎么做呢?
为了澄清我的问题,我希望能够为线上的单个标记设置样式,而不是为线上的每个标记设置样式。
发布于 2011-12-07 08:57:45
在调用plot时指定关键字args、linestyle和/或marker。
例如,使用虚线和蓝色圆标记:
plt.plot(range(10), linestyle='--', marker='o', color='b', label='line with marker')
plt.legend()一条捷径可以做同样的事情:
plt.plot(range(10), '--bo', label='line with marker')
plt.legend()

以下是可能的线条和标记样式的列表:
================ ===============================
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
================ ===============================编辑:根据注释中的要求,使用标记任意点的子集的示例:
import numpy as np
import matplotlib.pyplot as plt
xs = np.linspace(-np.pi, np.pi, 30)
ys = np.sin(xs)
markers_on = [12, 17, 18, 19]
plt.plot(xs, ys, '-gD', markevery=markers_on, label='line with select markers')
plt.legend()
plt.show()

由于this feature branch的合并,自1.4+以来,使用markevery kwarg的最后一个示例是可能的。如果您使用的是旧版本的matplotlib,您仍然可以通过在线状图上叠加散点图来获得结果。有关更多详细信息,请参阅edit history。
发布于 2017-10-09 10:01:12
有一张图片显示了所有标记的名称和描述,希望能对您有所帮助。
import matplotlib.pylab as plt
markers = ['.',',','o','v','^','<','>','1','2','3','4','8','s','p','P','*','h','H','+','x','X','D','d','|','_']
descriptions = ['point', 'pixel', 'circle', 'triangle_down', 'triangle_up','triangle_left',
'triangle_right', 'tri_down', 'tri_up', 'tri_left', 'tri_right', 'octagon',
'square', 'pentagon', 'plus (filled)','star', 'hexagon1', 'hexagon2', 'plus',
'x', 'x (filled)','diamond', 'thin_diamond', 'vline', 'hline']
x=[]
y=[]
for i in range(5):
for j in range(5):
x.append(i)
y.append(j)
plt.figure(figsize=(8, 8))
for i,j,m,l in zip(x,y,markers,descriptions):
plt.scatter(i,j,marker=m)
plt.text(i-0.15,j+0.15,s=m+' : '+l)
plt.axis([-0.1,4.8,-0.1,4.5])
plt.axis('off')
plt.tight_layout()
plt.show()

发布于 2012-08-13 03:36:17
作为将来的参考-由plot()返回的Line2D美工人员也有一个set_markevery()方法,它允许您仅在某些点上设置标记-请参见https://matplotlib.org/api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D.set_markevery
https://stackoverflow.com/questions/8409095
复制相似问题