Closed
Description
Bug report
Bug summary
I'm encountering a weird bug when trying to annotate a timeseries plot (x-axis is datetime object), but only when using xycoords=('data', 'axes fraction')
and textcoords='offset pixels'
at the same time
Code for reproduction
Consider the following code:
np.random.seed(12345)
dates = pd.date_range(start='1/1/2008', periods=10, freq='Y')
df = pd.DataFrame({'x':np.arange(10), 'y':np.random.random(size=(10,)), 'dates':dates})
fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6)) = plt.subplots(3,2, sharex='col', sharey=True, figsize=(10,10))
# no datetime, xycoords='data'
ax1.set_title('no datetime', fontweight='bold')
ax1.set_ylabel('xycoords=data', fontweight='bold')
ax1.plot(df['x'], df['y'])
ax1.annotate("test1", xy=(df.loc[5,'x'], 0.5), xycoords='data',
)
ax1.annotate("test2", xy=(df.loc[6,'x'], 0.5), xycoords='data',
xytext=(50,50), textcoords='offset pixels', arrowprops=dict(arrowstyle='->'))
# no datetime, xycoords='axes fraction'
ax3.set_ylabel('xycoords=fraction', fontweight='bold')
ax3.plot(df['x'], df['y'])
ax3.annotate("test1", xy=(0.5, 0.5), xycoords='axes fraction',
)
ax3.annotate("test2", xy=(0.6, 0.6), xycoords='axes fraction',
xytext=(50,50), textcoords='offset pixels', arrowprops=dict(arrowstyle='->'))
# no datetime, xycoords=('data', 'axes fraction')
ax5.set_ylabel('xycoords=mixed', fontweight='bold')
ax5.plot(df['x'], df['y'])
ax5.annotate("test1", xy=(df.loc[5,'x'], 0.5), xycoords=('data','axes fraction'),
)
ax5.annotate("test2", xy=(df.loc[6,'x'], 0.5), xycoords=('data','axes fraction'),
xytext=(50,50), textcoords='offset pixels', arrowprops=dict(arrowstyle='->'))
# datetime, xycoords='data'
ax2.set_title('datetime', fontweight='bold')
ax2.plot(df['dates'], df['y'])
ax2.annotate("test1", xy=(df.loc[5,'dates'], 0.5), xycoords='data',
)
ax2.annotate("test2", xy=(df.loc[6,'dates'], 0.5), xycoords='data',
xytext=(50,50), textcoords='offset pixels', arrowprops=dict(arrowstyle='->'))
# datetime, xycoords='axes fraction'
ax4.plot(df['dates'], df['y'])
ax4.annotate("test1", xy=(0.5, 0.5), xycoords='axes fraction',
)
ax4.annotate("test2", xy=(0.6, 0.6), xycoords='axes fraction',
xytext=(50,50), textcoords='offset pixels', arrowprops=dict(arrowstyle='->'))
# datetime, xycoords='axes fraction'
ax6.plot(df['dates'], df['y'])
ax6.annotate("test2", xy=(df.loc[6,'dates'], 0.5), xycoords=('data','axes fraction'),
xytext=(50,50), textcoords='offset pixels', arrowprops=dict(arrowstyle='->'))
ax6.annotate("test1", xy=(df.loc[5,'dates'], 0.5), xycoords=('data','axes fraction'),
)
fig.autofmt_xdate()
Actual outcome
Expected outcome
The annotations in the two columns should be identical, but notice how "test2" is not shown in the bottom right plot. More troubling, if you invert the order of the last two annotations
ax6.annotate("test2", xy=(df.loc[6,'dates'], 0.5), xycoords=('data','axes fraction'),
xytext=(50,50), textcoords='offset pixels', arrowprops=dict(arrowstyle='->'))
ax6.annotate("test1", xy=(df.loc[5,'dates'], 0.5), xycoords=('data','axes fraction'),
)
then both annotations are gone.
Matplotlib version
- Operating system: macOS
- Matplotlib version: 3.1.1
- Matplotlib backend (
print(matplotlib.get_backend())
): nbAgg - Python version: 3.7.4
- Jupyter version (if applicable):
- Other libraries: