2017-02-11 56 views
1

我正在尝试使用matplot lib做一些基本的股票绘图操作,而且我试图填充图的体积部分。Matplotlib - 填充故障

我试图在两个单独的图表(一个在另一个顶部)一个股票的价格和交易量。当我2绘制直线向前行(代码未遂1)它看起来很好 - 如你所愿。

当我尝试填充卷线下方(代码中的ATTEMPT 2)时,价格图看起来完全压缩到约5个像素宽,而第二个卷图中没有任何内容。

我认为这件事情做与x轴(日期)系列作为其只尝试明确设置2.

另外,我还尝试与双轴线一张图中绘制相同的数据。我得到的只是在单个图表相同的结果。我假设同样的解决方案将解决这个问题有两种类型的图表?

而且,能有人给我如何收缩y轴的刻度提示吗?

我错过了什么?

谢谢!

import datetime 
import numpy as np 
import matplotlib.colors as colors 
import matplotlib.finance as finance 
import matplotlib.dates as mdates 
import matplotlib.ticker as mticker 
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt 
import matplotlib.font_manager as font_manager 

# get the stock data 
fh = finance.fetch_historical_yahoo('AAPL', (2007, 2, 12), (2011, 2, 12)) 
r = mlab.csv2rec(fh) 
fh.close() 
r.sort() 


# *** ATTEMPT 1: 2 standard line plots ****************************** 
f, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=False) 
ax1.plot(r.close) 
ax2.plot(r.volume) 
f.subplots_adjust(hspace=0) 


# *** ATTEMPT 2: Fill the volume plot ******************************* 
f, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=False) 
ax1.plot(r.close) 
ax2.fill_between(r.date,0, r.volume, facecolor='#0079a3', alpha=0.4) 
f.subplots_adjust(hspace=0) 
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False) 

回答

1

既然你链接两个xaxes,他们应该共享相同x轴的数据。使用plot(x,y)语法而不是plot(y)应该解决的问题。

f, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=False) 
ax1.plot(r.date,r.close) 
ax2.fill_between(r.date,0, r.volume, facecolor='#0079a3', alpha=0.4) 
f.subplots_adjust(hspace=0) 
+0

工程很好。谢谢! – BigWinston