为我的点绘制垂直箭头

时间:2014-07-29 19:24:25

标签: python-2.7 matplotlib scatter-plot

我想找到一种方法来为我的每个数据点添加一个垂直箭头。我在下面散布了图表和代码。我需要垂直箭头从向上的点开始到图表比例大约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 )

enter image description here

4 个答案:

答案 0 :(得分:3)

这有点苛刻,调整arrow_offsetarrow_size,直到数字看起来正确。enter image description here

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)

答案 1 :(得分:3)

这可以直接完成

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

demo image

在语义发生变化的情况下,这些箭头的实现方式有些奇怪,以至于“失败”。意味着数据点是下限'和' uplims'表示数据点是最大值'。

请参阅https://github.com/matplotlib/matplotlib/pull/2452

答案 2 :(得分:2)

这不是超级优雅,但它可以解决问题

让箭头从数据点开始,然后上升0.2个单位:

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

enter image description here

答案 3 :(得分:2)

提出的其他方法很棒。我今天要参加最讨厌的奖项:

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="<|-"))

enter image description here