Вы можете использовать plt.annotate
, чтобы сместить текст относительно точки, которую вы хотите аннотировать. Это делается с помощью ключевого слова textcoords
. Здесь мы можем сместить один текст на +3
, а другой на -3
пунктов.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.randint(1,50, size=(8,4)), columns=list("ABCD"))
fig, ax = plt.subplots()
ax.plot(df.A, df.B, "ro")
f = lambda x, kw: ax.annotate(x[2], xy=(x[0],x[1]), textcoords="offset points", **kw)
g = lambda x: ax.annotate(x[2], xy=(x[0],x[1]), xytext=(2,-5), textcoords="offset points", va="top")
df[["A", "B", 'C']].apply(lambda x: f(x,dict(xytext=(2,3))), axis=1)
df[["A", "B", 'D']].apply(lambda x: f(x,dict(xytext=(2,-3), va="top")), axis=1)
ax.margins(0.1)
plt.show()
