Matplotlib - 在错误的子图中绘制的变量

时间:2018-03-26 16:51:44

标签: python-3.x matplotlib subplot

我有2个变量,我想在带有错误栏的单独子图中绘制。但是,它们都在底部子图中绘制。如何让他们在单独的子图中绘图?

from pandas import DataFrame, date_range, Timedelta
import numpy as np
from matplotlib import pyplot as plt

rng = date_range(start='2015-01-01', periods=5, freq='24H')
df = DataFrame({'y':np.random.normal(size=len(rng))}, index=rng)
y1 = df['y']
y2 = (y1*3)
sd1 = (y1*2)
sd2 = (y1*2)

fig,(ax1,ax2) = plt.subplots(2,1,sharex=True)

ax1 = y1.plot(yerr=sd1)
ax2 = y2.plot(yerr=sd2)

Example Photo

1 个答案:

答案 0 :(得分:1)

使用pandas dataframe ax中的plot参数:

from pandas import DataFrame, date_range, Timedelta
import numpy as np
from matplotlib import pyplot as plt

rng = date_range(start='2015-01-01', periods=5, freq='24H')
df = DataFrame({'y':np.random.normal(size=len(rng))}, index=rng)
y1 = df['y']
y2 = (y1*3)
sd1 = (y1*2)
sd2 = (y1*2)

fig,(ax1,ax2) = plt.subplots(2,1,sharex=True)

_ = y1.plot(yerr=sd1, ax=ax1)
_ = y2.plot(yerr=sd2, ax=ax2)

输出:

enter image description here