彼responseに@ImportanceOfBeingErnestによって提案された方法は本当にきちんとしているが、データはパンダのデータ内にある場合、それは動作しません。 ([0,1,2、..、N])ではなく、インデックスにプロットすることが望ましく、その値はx's-である。
私は前述の解決策を適応させて、パンダplot
の機能で使用しました。私はまた対称のmin
関数を書いた。
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)
使用は、たとえば、単純です:
ax = df[Series[0]].plot(grid=True, use_index=True, \
title=None);
annot_max(df[Series[0]].index,df[Series[0]],ax);
plt.show();
私は、これは誰にどんな助けになるでしょう願っています。
TypeError: 'RangeIndex'オブジェクトが呼び出し可能ではありません - 配列ではなくDataFrame内に軸データがあります。これがエラーメッセージを受け取るのはなぜですか?コードを見るだけで、うまくいくはずです。 – shadewolf
私のコードのどの行にエラーがありますか? –
12行目:(xpos = y.index(ymax)) – shadewolf