This is a reproducible example of 6 scatterplots in Pandas, obtained from pd.groupby()
for 6 consecutive years. On x axis -- there is oil price (brent) for the year, on y -- the value for sp500 for the same year.
import matplotlib.pyplot as plt
import pandas as pd
import Quandl as ql
%matplotlib inline
brent = ql.get('FRED/DCOILBRENTEU')
sp500 = ql.get('YAHOO/INDEX_GSPC')
values = pd.DataFrame({'brent':brent.VALUE, 'sp500':sp500.Close}).dropna()["2009":"2015"]
fig, axes = plt.subplots(2,3, figsize=(15,5))
for (year, group), ax in zip(values.groupby(values.index.year), axes.flatten()):
group.plot(x='brent', y='sp500', kind='scatter', ax=ax, title=year)
This produces the below plot:
(Just in case, from these plots you may infer there was a strong correlation between oil and sp500 in 2010 but not in other years).
You may change kind
in group.plot()
so that it suits your specific kind or data. My anticipation, pandas will preserve the date formatting for x-axis if you have it in your data.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…