【发布时间】:2020-03-26 09:48:53
【问题描述】:
我试图让 matplotlib 中此折线图中的文本注释显示在线上方。我正在使用adjustText 库来排斥行中的文本,但如图所示,一些文本位于行下方。我的代码基于接受的答案here。
如何让每个注释出现在线上方,并通过灰线连接到它的点?他们中的一些人已经是这样了,但其他人不是。
x = list(df_usa['date'][61:-1].str.slice(5,)) # strings
x_range = np.arange(0, len(x))
y = list(df_usa['growth_factor'][61:-1])
plt.figure(figsize=(20,6))
plt.plot(x, y, color="red", alpha=0.5)
plt.xticks(np.arange(0, len(x), 5))
texts = [plt.text(x_range[i], y[i], round(y[i], 2)) for i in range(len(x))]
f = interpolate.interp1d(x_range, y)
f_x = np.arange(min(x_range), max(x_range), 0.005)
f_y = f(f_x)
# Generate non-overlapping data point labels
adjust_text(texts, x=f_x, y=f_y,
only_move={'points': 'y', 'text': 'y'},
force_points=0.15,
autoalign='y',
arrowprops=dict(arrowstyle="-", color="gray", lw=0.5))
plt.show()
【问题讨论】:
标签: python matplotlib annotations