【问题标题】:pandas boxplot, groupby different ylim in each subplotpandas boxplot,groupby 每个子图中的不同 ylim
【发布时间】:2016-09-18 01:39:09
【问题描述】:

我有一个数据框,我想将其绘制为:

>>> X = pd.DataFrame(np.random.normal(0, 1, (100, 3)))
>>> X['NCP'] = np.random.randint(0, 5, 100)
>>> X[X['NCP'] == 0] += 100
>>> X.groupby('NCP').boxplot()

结果是我想要的,但所有子图都有相同的 ylim。这使得无法正确地可视化结果。如何为每个子图设置不同的 ylim?

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    您要求的是为每个轴分别设置 y 轴。我相信应该是ax.set_ylim([a, b])。但每次我为每个轴运行它时,它都会为所有人更新。

    因为我不知道如何直接回答你的问题,所以我提供了一个解决方法。

    X = pd.DataFrame(np.random.normal(0, 1, (100, 3)))
    X['NCP'] = np.random.randint(0, 5, 100)
    X[X['NCP'] == 0] += 100
    
    groups = X.groupby('NCP')
    
    print groups.groups.keys()
    
    # This gets a number of subplots equal to the number of groups in a single 
    # column.  you can adjust this yourself if you need.
    fig, axes = plt.subplots(len(groups.groups), 1, figsize=[10, 12])
    
    # Loop through each group and plot boxplot to appropriate axis
    for i, k in enumerate(groups.groups.keys()):
        group = groups.get_group(k)
        group.boxplot(ax=axes[i], return_type='axes')
    

    subplots DOCUMENTATION

    【讨论】:

    • 它有效。我认为这是一个错误,因为我很确定我能够用我不工作的代码来做到这一点。
    猜你喜欢
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 2021-12-25
    • 1970-01-01
    • 2019-06-12
    • 2013-05-17
    • 2018-05-22
    相关资源
    最近更新 更多