【问题标题】:How to plot a ylabel per subplot using pandas DataFrame plot function如何使用 pandas DataFrame 绘图函数绘制每个子图的 ylabel
【发布时间】:2015-10-23 23:30:04
【问题描述】:

默认情况下,使用 subplots 选项的 pandas.DataFrame.plot() 似乎并不容易为每个子图绘制一个 ylabel。我正在尝试绘制一个熊猫数据框,该数据框在数据框中的每列都有一个子图。到目前为止不起作用的代码:

fig = plt.figure(figsize=(10,10))
ax = plt.gca()
df.plot(y=vars, ax=ax, subplots=True, layout=(3,1), sharex=True, legend=False,)
ax.set_ylabel = ['y','x', 'z']

但这根本没有绘制任何标签。

【问题讨论】:

    标签: python pandas matplotlib plot subplot


    【解决方案1】:

    您可以在每个斧头上分别设置 y 标签。

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    # data
    df = pd.DataFrame(np.random.randn(100,3), columns=list('ABC'))
    
    # plot
    axes = df.plot(figsize=(10, 10), subplots=True, sharex=True, legend=False)
    axes[0].set_ylabel('yA')
    axes[1].set_ylabel('yB')
    axes[2].set_ylabel('yC')
    

    【讨论】:

    • 我之前尝试过类似的方法,但没有成功。我需要删除 layout=(3,1), 才能正常工作。不知何故指定布局不允许标签出现,并出现错误:AttributeError: 'numpy.ndarray' object has no attribute 'set_ylabel'
    • @JacquesMALAPRADE 如果布局是一个元组,你会得到一个 2D 轴数组,即你需要使用axes[0][0].set_label(...) 等。
    猜你喜欢
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2015-06-12
    • 2021-07-07
    • 2019-01-15
    相关资源
    最近更新 更多