在 IPython 中使用 Pandas 绘制股票图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9283166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotting a stock chart with Pandas in IPython
提问by Ben McCann
I'm just getting started with Python/Pandas and put together the following code to plot the S&P 500.
我刚刚开始使用 Python/Pandas,并将以下代码放在一起绘制标准普尔 500 指数。
from pandas.io.data import DataReader
# returns a DataFrame
sp500 = DataReader("^GSPC", "yahoo", start=datetime.datetime(1990, 1, 1))
sp500.plot(figsize = (12,8))
It looks like this is plotting the high, low, open, close, adj close, and volume all on one graph. Is there an easy way to plot just the adj close in one graph and the volume right below it like they do on Yahooand most other financial sites? I'd also be interested in an example of plotting an OHLC candlestick chart.
看起来这是在一张图上绘制最高价、最低价、开盘价、收盘价、调整收盘价和成交量。有没有一种简单的方法可以像在雅虎和大多数其他金融网站上那样,在一个图表中只绘制 adj 收盘价和它下方的交易量?我也对绘制 OHLC 烛台图的示例感兴趣。
采纳答案by Appleman1234
See herefor my answer to a similar question and herefor further information regarding mathplotlib's finance candlestick graph.
请参阅此处了解我对类似问题的回答,此处了解有关 mathplotlib 的金融烛台图的更多信息。
To get just the adj close from your sp500, you would use something like sp500["Adj Close"]and then pass that to the relevant matplotlib plot command plt.plot(datelist, sp500["Adj Close"] )where datelist is your list of dates on the x axis.
要从 sp500 获得 adj 接近的值,您可以使用类似的方法sp500["Adj Close"],然后将其传递给相关的 matplotlib plot 命令plt.plot(datelist, sp500["Adj Close"] ),其中 datelist 是 x 轴上的日期列表。
I believe you can get datelist by referencing sp500.index, see herefor more information.
我相信您可以通过引用获取日期列表sp500.index,请参阅此处了解更多信息。
As for your issue with passing it to the plot command, something like
至于您将其传递给 plot 命令的问题,例如
datelist = [date2num(x) for x in sp500.index]where the function date2num is from matplotlib.dates package.
datelist = [date2num(x) for x in sp500.index]其中函数 date2num 来自 matplotlib.dates 包。
After setting up the relevant subplot, and then call the appropriate fill command to fill_between_alphathe area under the line like the Yahoo graph you linked to.
设置相关子图后,然后调用适当的填充命令到fill_between_alpha您链接到的雅虎图等线下的区域。
See hereunder the Fill Between and Alphaheading for another snippet that shows a filled line graph, with correct date printing.
请参阅此处的“填充之间”和“Alpha”标题下的另一个片段,该片段显示填充的折线图,并带有正确的日期打印。
The initial link has a sample matplotlib snippet which also covers the date format and formatting in more detail.
初始链接有一个示例 matplotlib 片段,其中还更详细地介绍了日期格式和格式。
回答by brno792
This gets you a plot of just the Adj Close column vs the Index of you DataFrame (Date).
这将为您提供仅 Adj Close 列与 DataFrame 索引(日期)的图。
from pandas.io.data import DataReader
sp500 = DataReader("^GSPC", "yahoo", start=datetime.datetime(1990, 1, 1))
close = sp500['Adj Close']
close.plot()
plt.show()
Likewise, You can plot the Volume the same way:
同样,您可以以相同的方式绘制 Volume:
vol = sp500['Volume']
vol.plot()
plt.show()

