Given the following code:
from pandas import DataFrame, date_range, Timestamp
from numpy import arange
data = DataFrame({'A': arange(100, 200), 'B': arange(500, 400, -1)}, index=date_range('2024-01-01', periods=100))
ax_a = data['A'].plot(color='C0')
ax_b = data['B'].plot(color='C1', secondary_y=True)
ax_a.annotate(
'', (Timestamp('2024-01-15'), 120), xytext=(Timestamp('2024-03-20'), 120),
arrowprops=dict(arrowstyle="->", color='C0', ),
)
ax_a.annotate(f'Label A', (Timestamp('2024-03-02'), 120), bbox=dict(fc="0.8"))
ax_b.annotate(
'', (Timestamp('2024-03-20'), 420), xytext=(Timestamp('2024-01-15'), 420),
arrowprops=dict(arrowstyle="->", color='C1', ),
)
ax_b.annotate(f'Label B', (Timestamp('2024-02-02'), 420), bbox=dict(fc="0.8"))
I get the following plot:
What should I change such that none of the lines, including the annotation arrows, overlap either of the annotation bboxes and their content?
As you can see below, currently any lines plotted on ax_b
overlap the bbox
for “Label A”.