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

Matplotlib - Variables plotting in wrong 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)

在 pandas 数据帧中使用 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)

输出: