A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://stackoverflow.com/questions/11067368/annotate-time-series-plot-in-matplotlib below:

python - Annotate Time Series plot

Newer versions of matplotlib (e.g. 3.7.0) doesn't require explicit date2num calls anymore. Just pass a point on the plot directly as xy= to annotate().

from datetime import datetime
import matplotlib.pyplot as plt

x = [datetime(2009, 5, 1), datetime(2010, 6, 1), 
     datetime(2011, 4, 1), datetime(2012, 6, 1)]
y = [1, 3, 2, 5]

fig, ax = plt.subplots(facecolor='white')
ax.plot(x, y, linestyle='--')

ax.annotate('Test', xy=(x[1], y[1]),   # <---- directly pass the point position
            xytext=(-15, 15), textcoords='offset points', 
            arrowprops={'arrowstyle': '->'})

fig.autofmt_xdate()

A more terse (but less flexible) solution is to use text() to annotate. Again, no need to perform a datetime-to-number conversion; just pass the point as is.

plt.plot(x, y, linestyle='--')
plt.text(x[1], y[1], 'Test')        # use x-y coordinate values as is
plt.xticks(plt.xticks()[0][::2]);


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4