Вот моя попытка:

Код: Выделить всё
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
# Plot rectangle to visualize the text bounding box
def plot_bbox(handle):
# Get the bounding box in display units
bb_du = handle.get_window_extent()
# Convert to data coordinates to display coordinates
t = ax.transData.inverted()
bb_dc = bb_du.transformed(t)
x0, y0 = bb_dc.min
x1, y1 = bb_dc.max
w = x1 - x0
h = y1 - y0
print(f'{x0:.2f}, {y0:.2f}, {w:.2f}, {h:.2f}')
# Draw a rectangle for the bounding box
rect = Rectangle((x0, y0), w, h, fc='none', color ='m', linewidth = 2)
ax.add_patch(rect)
# Plot text
fig_kw = dict(layout='constrained')
fig, ax = plt.subplots(**fig_kw, subplot_kw=dict(aspect=1))
xys = ((6, 1), (8, 1), (6, 3), (8, 3))
texts = (('aBjg', 90), ('aBjg', 10), ('aBjg', 60), ('aBjg', 30))
xo = yo = 0.3
for (x, y), (text, r) in zip(xys, texts):
ax.scatter((x,), (y,), c='C0')
text_h = ax.text(x+xo, y+yo, text, rotation=r, size=18)
plot_bbox(text_h)
Подробнее здесь: https://stackoverflow.com/questions/790 ... plot-units