python - seaborn: share X label not working as expected

前端 未结 1 1999
别跟我提以往
别跟我提以往 2021-01-07 14:56

i am dealing with a dataset that shows relationships between two points, such as bus stops. For example, we have bus stops A, B, C, and D.

I want to make histogram p

相关标签:
1条回答
  • 2021-01-07 15:06

    As seen from the documentation, sns.boxplot has an argumen order

    order, hue_order : lists of strings, optional
    Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.

    Using this like

    sns.boxplot(..., order=['A','B','C','D'])
    

    would give you the desired plot.

    Complete code:

    import pandas as pd
    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    time=np.random.randn(1000)
    point1 = ['A','B','C','D'] * 250
    point2 = ['A'] * 250 + ['B'] * 250 + ['C'] * 250 + ['D'] * 250 
    
    df_time = pd.DataFrame(
        {'point1': point1,
         'point2': point2,
         'time': time
        })
    df_time=df_time[df_time['point1']!=df_time['point2']] ##cannot sell to another
    
    fig, ax = plt.subplots(nrows=4, sharey=True)
    
    for point1i, axi in zip(point1, ax.ravel()):
        sns.boxplot(data=df_time[df_time['point1']==point1i], x='point2', y='time', 
                    ax=axi, order=['A','B','C','D'])
    
    plt.tight_layout()    
    plt.show()
    

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