The method proposed by @ImportanceOfBeingErnest in his response is really neat, but it doesn't work if the data is within a panda data-frame whose index isn't a zero based uniform index ([0,1,2,..,N]), and it is desired to plot against the index -whose values are the x's-.
I took the liberty to adapt the aforementioned solution and use it with pandas plot function. I also wrote the symmetric min function.
def annot_max(x,y, ax=None):
maxIxVal = np.argmax(y);
zeroBasedIx = np.argwhere(y.index==maxIxVal).flatten()[0];
xmax = x[zeroBasedIx];
ymax = y.max()
text= "k={:d}, measure={:.3f}".format(xmax, ymax)
if not ax:
ax=plt.gca()
bbox_props = dict(boxstyle="round,pad=0.3", fc="w", ec="k", lw=0.72)
arrowprops=dict(arrowstyle="-",connectionstyle="arc3,rad=0.1")
kw = dict(xycoords='data',textcoords="axes fraction",
arrowprops=arrowprops, bbox=bbox_props, ha="right", va="top")
ax.annotate(text, xy=(xmax, ymax), xytext=(0.94,0.90), **kw)
def annot_min(x,y, ax=None):
minIxVal = np.argmin(y);
zeroBasedIx = np.argwhere(y.index==minIxVal).flatten()[0];
xmin = x[zeroBasedIx];
ymin = y.min()
text= "k={:d}, measure={:.3f}".format(xmin, ymin)
if not ax:
ax=plt.gca()
bbox_props = dict(boxstyle="round,pad=0.3", fc="w", ec="k", lw=0.72)
arrowprops=dict(arrowstyle="-",connectionstyle="arc3,rad=0.1")
kw = dict(xycoords='data',textcoords="axes fraction",
arrowprops=arrowprops, bbox=bbox_props, ha="right", va="top")
ax.annotate(text, xy=(xmin, ymin), xytext=(0.94,0.90), **kw)
Usage is straightforward, for example:
ax = df[Series[0]].plot(grid=True, use_index=True, \
title=None);
annot_max(df[Series[0]].index,df[Series[0]],ax);
plt.show();
I hope this would be of any help to anyone.