这是解决你问题的最简单的方法。这里的主要关键字是
arrowstyle="<->"
. 我个人更喜欢用
plt.text
单独放置文本,使其独立于绘图中的箭头端点。它给你更多的自由。由于缺少你的数据,我不得不生成随机数据,但我保留了
x
和
y
范围与您的相似。您可以相应地调整代码。你的问题是
arrowprops
它是
annotate
vlines
.
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 5))
ax = fig.add_subplot(111)
x = np.linspace(-0.4, 0.6, 1000)
y = np.random.normal(0, 0.033, 1000)
plt.plot(x, y, 'o')
plt.hlines(y = max(y), xmin = min(x), xmax = max(x),
color = 'r', linestyle = 'dotted')
plt.hlines(y = min(y), xmin = min(x), xmax = max(x),
color = 'r', linestyle = 'dotted')
ax.annotate("",
xy=(min(x), min(y)), xycoords='data',
xytext=(min(x), max(y)), textcoords='data',
arrowprops=dict(arrowstyle="<->",
connectionstyle="arc3", color='r', lw=2),
)
plt.text(1.2*min(x), max(y), 'Range of residuals: %g'%(max(y) - min(y)),
rotation = 90, fontsize = 16)
plt.xlim(1.3*min(x), 1.3*max(x))
输出