pandas boxplot, groupby different ylim in each subplot

前端 未结 1 836
谎友^
谎友^ 2021-01-21 01:07

I have a dataframe and I would like to plot it as:

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


        
相关标签:
1条回答
  • 2021-01-21 01:24

    What you asked for was to set the y axis separately for each axes. I believe that should be ax.set_ylim([a, b]). But every time I ran it for each axes it updated for all.

    Because I couldn't figure out how to answer your question directly, I'm providing a work around.

    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

    0 讨论(0)
提交回复
热议问题