Ubuntu 10.0.4 で matplotlib 1.2.x と Python 2.6.5 を使用しています。上部のプロットと下部のプロットで構成される単一のプロットを作成しようとしています。
X 軸は時系列の日付です。上部のプロットにはデータのローソク足プロットが含まれ、下部のプロットはバー タイプのプロットで構成され、独自の Y 軸 (左側にも - 上部のプロットと同じ) が含まれます。これら 2 つのプロットは重複してはなりません。
ここに私がこれまでに行ったことのスニペットがあります。
datafile = r'/var/tmp/trz12.csv'
r = mlab.csv2rec(datafile, delimiter=',', names=('dt', 'op', 'hi', 'lo', 'cl', 'vol', 'oi'))
mask = (r["dt"] >= datetime.date(startdate)) & (r["dt"] <= datetime.date(enddate))
selected = r[mask]
plotdata = zip(date2num(selected['dt']), selected['op'], selected['cl'], selected['hi'], selected['lo'], selected['vol'], selected['oi'])
# Setup charting
mondays = WeekdayLocator(MONDAY) # major ticks on the mondays
alldays = DayLocator() # minor ticks on the days
weekFormatter = DateFormatter('%b %d') # Eg, Jan 12
dayFormatter = DateFormatter('%d') # Eg, 12
monthFormatter = DateFormatter('%b %y')
# every Nth month
months = MonthLocator(range(1,13), bymonthday=1, interval=1)
fig = pylab.figure()
fig.subplots_adjust(bottom=0.1)
ax = fig.add_subplot(111)
ax.xaxis.set_major_locator(months)#mondays
ax.xaxis.set_major_formatter(monthFormatter) #weekFormatter
ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
ax.format_ydata = price
ax.grid(True)
candlestick(ax, plotdata, width=0.5, colorup='g', colordown='r', alpha=0.85)
ax.xaxis_date()
ax.autoscale_view()
pylab.setp( pylab.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
# Add volume data
# Note: the code below OVERWRITES the bottom part of the first plot
# it should be plotted UNDERNEATH the first plot - but somehow, that's not happening
fig.subplots_adjust(hspace=0.15)
ay = fig.add_subplot(212)
volumes = [ x[-2] for x in plotdata]
ay.bar(range(len(plotdata)), volumes, 0.05)
pylab.show()
上記のコードを使用して 2 つのプロットを表示できましたが、下のプロットには 2 つの問題があります。
それは最初の(上)プロットの下部を完全に上書きします-まるで2番目のプロットが最初のプロットと同じ「キャンバス」に描かれているかのように-どこで/なぜそれが起こっているのかわかりません。
既存の X 軸を独自のインデックスで上書きします。X 軸の値 (日付) は 2 つのプロット間で共有する必要があります。
コードで何が間違っていますか?. 誰かが 2 番目 (下) のプロットが最初 (上) のプロットを上書きする原因を特定できますか?どうすればこれを修正できますか?
上記のコードで作成されたプロットのスクリーンショットを次に示します。
[[編集]]
hwlau の提案に従ってコードを変更した後、これが新しいプロットです。2 つのプロットが分離されているという点で最初のものよりは優れていますが、次の問題が残ります。
X 軸は 2 つのプロットで共有する必要があります (つまり、X 軸は 2 番目の [下] プロットに対してのみ表示する必要があります)。
2 番目のプロットの Y 値の形式が正しくないようです
これらの問題は非常に簡単に解決できるはずですが、最近matplotlibでプログラミングを始めたばかりなので、私のmatplotlib fuは現時点ではあまり良くありません。どんな助けでも大歓迎です。