首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Matplotlib中为线上的各个点设置标记

在Matplotlib中为线上的各个点设置标记
EN

Stack Overflow用户
提问于 2011-12-07 08:56:02
回答 5查看 407.6K关注 0票数 230

我使用Matplotlib在图形上绘制线条。现在,我想为线上的各个点设置样式,特别是标记。我该怎么做呢?

为了澄清我的问题,我希望能够为线上的单个标记设置样式,而不是为线上的每个标记设置样式。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-12-07 08:57:45

在调用plot时指定关键字args、linestyle和/或marker

例如,使用虚线和蓝色圆标记:

代码语言:javascript
复制
plt.plot(range(10), linestyle='--', marker='o', color='b', label='line with marker')
plt.legend()

一条捷径可以做同样的事情:

代码语言:javascript
复制
plt.plot(range(10), '--bo', label='line with marker')
plt.legend()

以下是可能的线条和标记样式的列表:

代码语言:javascript
复制
================    ===============================
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
================    ===============================

编辑:根据注释中的要求,使用标记任意点的子集的示例:

代码语言:javascript
复制
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

票数 423
EN

Stack Overflow用户

发布于 2017-10-09 10:01:12

有一张图片显示了所有标记的名称和描述,希望能对您有所帮助。

代码语言:javascript
复制
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() 

票数 75
EN

Stack Overflow用户

发布于 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

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8409095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档