【问题标题】:Matplotlib - Variables plotting in wrong subplot [duplicate]Matplotlib - 在错误的子图中绘制的变量[重复]
【发布时间】:2018-09-04 21:44:05
【问题描述】:

我有 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)

【问题讨论】:

    标签: python-3.x matplotlib subplot


    【解决方案1】:

    在熊猫数据框plot中使用ax参数:

    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)
    

    输出:

    【讨论】:

      猜你喜欢
      • 2021-10-30
      • 1970-01-01
      • 2022-08-16
      • 2018-01-04
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多