【问题标题】:Set markers for individual points on a line in Matplotlib在 Matplotlib 中为线上的单个点设置标记
【发布时间】:2012-01-14 14:19:52
【问题描述】:

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

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

【问题讨论】:

标签: python matplotlib


【解决方案1】:

在您对plot 的调用中指定关键字参数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
================    ===============================

edit: 以 cmets 中要求的标记任意点子集的示例:

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 的合并,最后一个使用markevery kwarg 的示例可以在1.4+ 中使用。如果您卡在较旧版本的 matplotlib 上,您仍然可以通过在折线图上叠加散点图来获得结果。有关详细信息,请参阅edit history

【讨论】:

    【解决方案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】:

      供将来参考 - plot() 返回的 Line2D 艺术家也有一个 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()
        

        结果是: Plot with multiple markers

        【讨论】:

          【解决方案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()
          

          【讨论】:

          • 您能否更新此答案以绘制 x 轴为时间戳之类的日期的时间序列数据?
          猜你喜欢
          • 2016-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多