0

次のコード スニペットは、4 つのバーの 2 つのローソク足シリーズをプロットしています。

from pylab import * 
from matplotlib.finance import candlestick
import matplotlib.gridspec as gridspec

quotes = [(734542.0, 1.326, 1.3287, 1.3322, 1.3215), (734543.0, 1.3286, 1.3198, 1.3292, 1.3155), (734546.0, 1.321, 1.3187, 1.3284, 1.3186), (734547.0, 1.3186, 1.3133, 1.3217, 1.308)]
quotes2 = [(734542.0, 1.0, 0.9979, 1.0046, 0.9953), (734543.0, 0.998, 0.9991, 1.0024, 0.9952), (734546.0, 0.9991, 1.0014, 1.0038, 0.9951), (734547.0, 1.003, 1.0028, 1.0047, 1.0002)]

fig, ax = subplots()
candlestick(ax,quotes,width = 0.5, colorup = "green", colordown = "red")
candlestick(ax,quotes2, width = 0.2, colorup = "grey", colordown = "black")
ax.xaxis_date()
ax.autoscale_view()
ax.legend(loc=3)


plt.show()

2 つのシリーズにラベルを追加できず、ウェブ上ではまだ何も見つかりませんでした。これを行う正しい構文は何ですか?

candlestick(ax,quotes,width = 0.5, label = "Series 1") #but it doesn't work

注:必要なのは、このスレッドや他のスレッドが説明するような特定のポイントに注釈を付けることではなく、チャートの凡例に適切なラベルを追加することです。最終的な目的は、いくつかの正規化された価格シリーズをプロットして視覚的に比較することです

追加: より正確には、「しかし機能しない」という大まかな試みは、次の予想されるエラーに対して実際には機能しません。

TypeError: candlestick() got an unexpected keyword argument 'label'
4

1 に答える 1

1

ラベルを取得するには、いくつかの場所を変更する必要があります。

C1=candlestick(ax,quotes,width = 0.5, colorup = "green", colordown = "red")
C2=candlestick(ax,quotes2, width = 0.2, colorup = "grey", colordown = "black")
ax.xaxis_date()
ax.autoscale_view()
ax.legend((C1[1][0],C2[1][0]), ('label1', 'label2'),loc=3)

ここに画像の説明を入力

問題は、私たちcolorupcolordownここでは、両方を凡例に簡単に入れることができないことです (まあ、できるかもしれませんが、それは非常に複雑です)。

では、なぜC2[1][0]ですか?これC1は次のとおりです。

In [5]:

C1
Out[5]:
([<matplotlib.lines.Line2D at 0x76b3c90>,
  <matplotlib.lines.Line2D at 0x759d3b0>,
  <matplotlib.lines.Line2D at 0x759dab0>,
  <matplotlib.lines.Line2D at 0x75a61d0>],
 [<matplotlib.patches.Rectangle at 0x76b3df0>,
  <matplotlib.patches.Rectangle at 0x759d590>,
  <matplotlib.patches.Rectangle at 0x759dc90>,
  <matplotlib.patches.Rectangle at 0x75a63b0>])

ローソク足プロットに続く他のプロットがある場合:

plt.hlines(1.10, plt.xlim()[0], plt.xlim()[1], label='Other Plot') #such as an horizontal line
#And may be other plots.
handles, labels = ax.get_legend_handles_labels()
import operator
hl = sorted(zip(handles, labels),
            key=operator.itemgetter(1)) #sort is optional
handles2, labels2 = zip(*hl)
handles2=list(handles2)+[C1[1][0],C2[1][0]] #put the candel plot legend to the end
labels2=list(labels2)+['label1', 'label2'] #put the candel plot legend to the end
ax.legend(handles2, labels2, loc=8)

ここに画像の説明を入力

資料より。

于 2014-04-09T00:25:46.487 に答える