3

I'm just getting started with Python/Pandas and put together the following code to plot the S&P 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 Yahoo and most other financial sites? I'd also be interested in an example of plotting an OHLC candlestick chart.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Ben McCann
  • 18,548
  • 25
  • 83
  • 101
  • 2
    DataReader is no more in pandas. You have to install pandas_datareader package to continue to use it. – karlacio Nov 08 '16 at 21:05

2 Answers2

3

See here for my answer to a similar question and here for further information regarding mathplotlib's finance candlestick graph.

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.

I believe you can get datelist by referencing sp500.index, see here for more information.

As for your issue with passing it to the plot command, something like

datelist = [date2num(x) for x in sp500.index] where the function date2num is from matplotlib.dates package.

After setting up the relevant subplot, and then call the appropriate fill command to fill_between_alpha the area under the line like the Yahoo graph you linked to.

See here under the Fill Between and Alpha heading for another snippet that shows a filled line graph, with correct date printing.

The initial link has a sample matplotlib snippet which also covers the date format and formatting in more detail.

Community
  • 1
  • 1
Appleman1234
  • 15,946
  • 45
  • 67
  • Cool, thanks for the info :-) Do you know how I get datelist from the sp500 dataframe? I haven't been able to figure that out yet. – Ben McCann Feb 14 '12 at 22:19
  • I saw that you suggested I could reference sp500.index to get the datelist. I tried plt.plot(sp500.index, sp500["adj clos"]) but got the error: ERROR: An unexpected error occurred while tokenizing input The following traceback may be corrupted or invalid The error message is: ('EOF in multi-line statement', (63, 0)) – Ben McCann Feb 14 '12 at 23:58
  • Actually, it looks like the thing it's choking on is sp500["adj clos"] – Ben McCann Feb 15 '12 at 01:01
  • Ok, figured it out. The capialization was off. sp500["Adj Close"] works – Ben McCann Feb 15 '12 at 01:02
0

This gets you a plot of just the Adj Close column vs the Index of you DataFrame (Date).

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:

vol = sp500['Volume']
vol.plot()
plt.show()
brno792
  • 6,479
  • 17
  • 54
  • 71