那时,使用
annotate更容易.
例如:
import numpy as np
import matplotlib.pyplot as plt
data = np.random.normal(0, 1, 1000).cumsum()
fig, ax = plt.subplots()
ax.plot(data)
ax.annotate('Label', xy=(-12, -12), xycoords='axes points',
size=14, ha='right', va='top',
bbox=dict(boxstyle='round', fc='w'))
plt.show()
但是,如果您确实想要使用图例,那么您就是这样做的.除了将其大小设置为0并删除其填充外,您还需要显式隐藏图例句柄.
import numpy as np
import matplotlib.pyplot as plt
data = np.random.normal(0, 1, 1000).cumsum()
fig, ax = plt.subplots()
ax.plot(data, label='Label')
leg = ax.legend(handlelength=0, handletextpad=0, fancybox=True)
for item in leg.legendHandles:
item.set_visible(False)
plt.show()