0

I want to use an annotation box, because it allows more on the same ax without any tricks, but how can I anchor it to a corner like a real legend?

Consider

import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(0, 1, 100)
fig, ax = plt.subplots()
ax.plot(x, alpha=0.6, label="legend")
ax.annotate(
    s="annotate",
    xy=(0.01, 0.94),
    xycoords="axes fraction",
    bbox=dict(
        boxstyle="round",
        alpha=0.25,
        facecolor = "white",
        edgecolor = "grey"
    ),
)
ax.legend(loc="upper right")
plt.show()

It looks fine at first, but breaks when being resized.

This is different than Is it possible to anchor a matplotlib annotation to a data coordinate in the x-axis, but to a relative location in the y-axis? where the aim is to maintain a consistent ratio. Here, I want to maintain an absolute offset from the corner.

enter image description here

komodovaran_
  • 1,940
  • 16
  • 44

0 Answers0