为线上的各个点设置标记
- 2025-04-15 09:19:00
- admin 原创
- 26
问题描述:
我使用 Matplotlib 在图形上绘制了线条。现在我想设置线条上各个点的样式,特别是标记。该怎么做?
为了澄清我的问题,我希望能够为一行上的单个标记设置样式,而不是为该行上的每个标记设置样式。
解决方案 1:
在调用中指定关键字 argslinestyle
和/或。marker
`plot`
例如,使用虚线和蓝色圆圈标记:
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()
markevery
由于合并了此功能分支,最后一个使用 kwarg 的示例在 1.4+ 版本中可用。如果您仍在使用旧版本的 matplotlib,仍然可以通过在折线图上叠加散点图来实现此结果。更多详情,请参阅编辑历史记录。
解决方案 2:
有一张图片显示了所有标记的名称和描述,我希望它能对您有所帮助。
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()
解决方案 3:
供将来参考 -Line2D
返回的艺术家plot()
还有一种set_markevery()
方法,允许您仅在某些点上设置标记 - 请参阅https://matplotlib.org/api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D.set_markevery
解决方案 4:
更改特定点标记形状、大小等的一个简单技巧是,先用所有其他数据绘制该点,然后再用该点(或者,如果要更改多个点的样式,则用一组点)绘制一个图。假设我们要更改第二个点的标记形状:
x = [1,2,3,4,5]
y = [2,1,3,6,7]
plt.plot(x, y, "-o")
x0 = [2]
y0 = [1]
plt.plot(x0, y0, "s")
plt.show()
结果是:
带有多个标记的绘图
解决方案 5:
你好有一个例子:
import numpy as np
import matplotlib.pyplot as plt
def grafica_seno_coseno():
x = np.arange(-4,2*np.pi, 0.3)
y = 2*np.sin(x)
y2 = 3*np.cos(x)
plt.plot(x, y, '-gD')
plt.plot(x, y2, '-rD')
for xitem,yitem in np.nditer([x,y]):
etiqueta = "{:.1f}".format(xitem)
plt.annotate(etiqueta, (xitem,yitem), textcoords="offset points",xytext=(0,10),ha="center")
for xitem,y2item in np.nditer([x,y2]):
etiqueta2 = "{:.1f}".format(xitem)
plt.annotate(etiqueta2, (xitem,y2item), textcoords="offset points",xytext=(0,10),ha="center")
plt.grid(True)
plt.show()
grafica_seno_coseno()
相关推荐
热门文章
项目管理软件有哪些?
热门标签
曾咪二维码
扫码咨询,免费领取项目管理大礼包!
云禅道AD