我试图找出一种方法来为我的每个数据点添加一个垂直箭头指向上。我有下面的散点图和代码。我需要垂直箭头从向上的点开始,在th图标度中长度约为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

有帮助吗?

解决方案

所提出的其他方法很棒。我今天要参加哈基斯最奖项:

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

其他提示

这是有点hacky,调整 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)

这可以直接完成

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'表示“数据点是下限”,并且“上限”表示“数据点是最大值”。

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

这不是超级优雅,但它做了诀窍

将箭头从数据点开始并上升0.2单位:

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top