【问题标题】:Plot vertical arrows for my points为我的点绘制垂直箭头
【发布时间】:2014-09-21 07:14:47
【问题描述】:

我正在想办法为我的每个数据点添加一个向上的垂直箭头。我在下面有散点图和代码。我需要垂直箭头从向上的点开始,直到图形比例尺的长度约为 0.2。

import matplotlib.pyplot as plt
fig = plt.figure()
a1 = fig.add_subplot(111)

simbh = np.array([5.3,  5.3,  5.5,  5.6,  5.6,  5.8,  5.9,  6.0,   6.2,  6.3,  6.3])
simstel =np.array([10.02, 10.08, 9.64, 9.53, 9.78, 9.65, 10.05, 10.09, 10.08, 10.22, 10.42])
sca2=a1.scatter(simstel, simbh )

【问题讨论】:

  • 看看quiver 除了所有其他答案。 (quiver 如果您希望箭头朝不同方向移动,则特别有用。)我猜您想指出您的值是下限,因此在这种情况下,其他方法之一可能更好,但无论如何,quiver 很有用。
  • @JoeKington errorbar 实际上会这样做

标签: python-2.7 matplotlib scatter-plot


【解决方案1】:

这有点hacky,调整arrow_offsetarrow_size 直到图看起来正确。

import matplotlib.pyplot as plt
fig = plt.figure()
a1 = fig.add_subplot(111)

simbh = np.array([5.3,  5.3,  5.5,  5.6,  5.6,  5.8,  5.9,  6.0,   6.2,  6.3,  6.3])
simstel =np.array([10.02, 10.08, 9.64, 9.53, 9.78, 9.65, 10.05, 10.09, 10.08, 10.22, 10.42])
sca2=a1.scatter(simstel, simbh, c='w' )
arrow_offset = 0.08
arrow_size = 500
sca2=a1.scatter(simstel, simbh + arrow_offset, 
                marker=r'$\uparrow$', s=arrow_size)

【讨论】:

    【解决方案2】:

    这个可以直接做

    from matplotlib import pyplot as plt
    import numpy as np
    
    # set up figure
    fig, ax = plt.subplots()
    
    # make synthetic data
    x = np.linspace(0, 1, 15)
    y = np.random.rand(15)
    yerr = np.ones_like(x) * .2
    
    
    # if you are using 1.3.1 or older you might need to use uplims to work
    # around a bug, see below
    
    ax.errorbar(x, y, yerr=yerr, lolims=True, ls='none', marker='o')
    
    # adjust axis limits
    ax.margins(.1)  # margins makes the markers not overlap with the edges
    

    在语义发生变化的情况下,这些箭头的实现方式有些奇怪,因此“lolims”表示“数据点是下限”,“uplims”表示“数据点是最大值”。

    https://github.com/matplotlib/matplotlib/pull/2452

    【讨论】:

    • 不错!我不知道这是一个选择!
    • 在公关/错误报告通过之前我也没有
    【解决方案3】:

    介绍的其他方法都很棒。我今天要争取最黑客奖:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots()
    
    simbh = np.array([5.3,  5.3,  5.5,  5.6,  5.6,  5.8,  5.9,  6.0,   6.2,  6.3,  6.3])
    simstel = np.array([10.02, 10.08, 9.64, 9.53, 9.78, 9.65, 10.05, 10.09, 10.08, 10.22, 10.42])
    sca2 = ax.scatter(simstel, simbh)
    for x, y in zip(simstel, simbh):
        ax.annotate('', xy=(x, y), xytext=(0, 25), textcoords='offset points', 
                    arrowprops=dict(arrowstyle="<|-"))
    

    【讨论】:

      【解决方案4】:

      这不是很优雅,但可以解决问题

      让箭头从数据点开始向上 0.2 个单位:

      for x,y in zip(simstel,simbh):
          plt.arrow(x,y,0,0.2)
      

      【讨论】:

      • 垂直箭头是否可以从向上的点开始,直到图形比例尺的长度约为 0.2?
      • 显然没有正确阅读问题。相应地更正了答案
      • @DizietAsahi 我可以建议您删除答案的第一部分吗?第二个回答非常简洁,我认为它应该放在首位。
      • 删除了我的回答中不符合 OP 说明的第一部分
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2019-12-13
      相关资源
      最近更新 更多