【问题标题】:matplotlib: draw special symbol when a value appearsmatplotlib:出现值时绘制特殊符号
【发布时间】:2013-01-26 23:42:14
【问题描述】:

使用matplotlib 我想绘制一个延迟列表,这样:

  • X轴我有延迟在列表中的位置
  • Y 轴我自己有延迟

现在,在我的列表中有一个特殊值“L”,每当它出现时,我想在 X 轴上绘制一个红十字。 p>

我该如何做最后一件事?

【问题讨论】:

    标签: python graph matplotlib plot


    【解决方案1】:

    有许多不同的方法可以做到这一点。

    首先,让我们将您的数据设为一个 numpy 数组,以便我们可以使用布尔索引。这样可以更轻松地将“标记的”"L" 值与数据值隔离开来。

    理想情况下,您应该将事物转换为带有“L”值的屏蔽数组(并且在任何地方浮动,而不是混合数据类型)。不过,为了简单起见,我们在这里只使用一个对象数组,以便您可以混合字符串和浮点数。

    import matplotlib.pyplot as plt
    import numpy as np
    
    delays = np.array([0.5, 2.3, 'L', 0.9, 'L', 2], dtype=object)
    x = np.arange(delays.size)
    
    fig, ax = plt.subplots()
    ax.plot(x[delays != 'L'], delays[delays != 'L'], 'bo')
    
    # Expand axis limits by 0.5 in all directions for easier viewing
    limits = np.array(ax.axis())
    ax.axis(limits + [-0.5, 0.5, -0.5, 0.5])
    
    flag_positions = x[delays == 'L']
    ax.plot(flag_positions, np.zeros_like(flag_positions), 'rx', 
            clip_on=False, mew=2)
    
    plt.show()
    

    但是,红色的 x 位于固定的 y 位置,如果我们平移或缩放,它们会移出 x 轴。

    您可以通过使用自定义转换来解决此问题。在这种情况下,我们希望 x 坐标使用“正常”数据坐标 (ax.transData),y 坐标使用轴坐标系(例如 0-1,其中 0 是底部,1 是顶部: ax.transAxes)。为此,我们将使用 BlendedGenericTransform,它使用两种不同的变换:一种用于 x 坐标,另一种用于 y 坐标。

    因此,如果您希望红色的 x 始终位于 x 轴上,而不管绘图是如何平移或缩放的,那么您可以执行以下操作:

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.transforms import BlendedGenericTransform
    
    delays = np.array([0.5, 2.3, 'L', 0.9, 'L', 2], dtype=object)
    x = np.arange(delays.size)
    
    fig, ax = plt.subplots()
    ax.plot(x[delays != 'L'], delays[delays != 'L'], 'bo')
    
    flags = x[delays == 'L']
    ax.plot(flags, np.zeros_like(flags), 'rx', clip_on=False, mew=2,
            transform=BlendedGenericTransform(ax.transData, ax.transAxes))
    
    # Expand axis limits by 0.5 in all directions for easier viewing
    limits = np.array(ax.axis())
    ax.axis(limits + [-0.5, 0.5, -0.5, 0.5])
    
    plt.show()
    

    我们可以通过使用掩码数组让事情变得更简洁(也可以看看pandas)。使用掩码数组(或pandas)是指示缺失数据的更好选择,而不是使用具有混合字符串和浮点值的对象数组。举个例子:

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.transforms import BlendedGenericTransform
    
    delays = [0.5, 2.3, 'L', 0.9, 'L', 2]
    delays = [item if item != 'L' else np.nan for item in delays]
    delays = np.ma.masked_invalid(delays)
    
    fig, ax = plt.subplots()
    
    ax.plot(delays, 'bo')
    
    flags = delays.mask.nonzero()
    ax.plot(flags, np.zeros_like(flags), 'rx', clip_on=False, mew=2,
            transform=BlendedGenericTransform(ax.transData, ax.transAxes))
    
    # Expand axis limits by 0.5 in all directions for easier viewing
    limits = np.array(ax.axis())
    ax.axis(limits + [-0.5, 0.5, -0.5, 0.5])
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2012-11-09
      • 2018-04-25
      • 2018-02-22
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      相关资源
      最近更新 更多