简体   繁体   中英

Two subplots in Python (matplotlib)

I want to create two subplots in Python (using Anaconda 2.7) but code which I wrote generates two graphs, both not showing too much. Here's the code:

import pandas as pd
import pandas.io.data as web

import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')

import datetime

start = datetime.datetime(2000, 10, 23)
end = datetime.datetime(2016, 10, 23)

df = web.DataReader("BAC", "yahoo", start, end)

df['100MA'] = pd.rolling_mean(df['Adj Close'], 100, min_periods=1)
df['STD'] = pd.rolling_std(df['Close'], 25, min_periods=1)

print(df.tail())

ax1 = plt.subplot(2, 1, 1)
df[['Adj Close', '100MA']].plot()
ax2 = plt.subplot(2, 1, 2, sharex=ax1)
df['STD'].plot()
plt.show()

Thanks in advance!

import pandas as pd
import pandas.io.data as web

import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')

import datetime

start = datetime.datetime(2000, 10, 23)
end = datetime.datetime(2016, 10, 23)

df = web.DataReader("BAC", "yahoo", start, end)

df['100MA'] = pd.rolling_mean(df['Adj Close'], 100, min_periods=1)
df['STD'] = pd.rolling_std(df['Close'], 25, min_periods=1)

print(df.tail())
f,axarr = plt.subplots(2,1)
df[['Adj Close', '100MA']].plot(ax=axarr[0])
df['STD'].plot(ax=axarr[1])
plt.show()

result:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM