[Python] Overlaying box plot and line plot seaborn

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to overlay a box plot (series of box plot based on another variable) and a line plot of medians of that variable, on the same box plot. A simple code like below works perfectly fine.

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns

    dfx=pd.DataFrame({'S':np.random.randint(10,100,9)*10,'C':
    ['X','X','X','Y','Y','Y','Z','Z','Z']})

    fig,ax=plt.subplots()
    mx=dfx.groupby('C')['S'].median()
    sns.boxplot(y='S',x='C',data=dfx,ax=ax)
    sns.lineplot(y=mx.values,x=mx.index,ax=ax)
    plt.show()


    which gives

    [​IMG]

    However, when I use the same code for this data I am reading from csv file, I just cannot the line plot to appear with the box plot.

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns

    df=pd.read_csv('test.csv')
    fig,ax=plt.subplots()
    m=df.groupby('Start Date')['Score'].median()
    sns.boxplot(y='Score',x='Start Date',data=df,ax=ax)
    sns.lineplot(y=m.values,x=m.index,ax=ax)
    plt.show()


    gives this [​IMG]

    It doesn't matter whether the lineplot command is before or after boxplot, only box plot is shown. I see the line only if boxplot line is commented out.

    I do not understand what is different about this data I am reading from csv that I cannot overlay line and box

    P.S: I know a simple workaround is replace the seaborn lineplot line with matplotlib line command

    ax.plot(m.values,'r-o',linewidth=4)


    and it gives the desired result:

    [​IMG]

    I am just curious why seaborn lineplot is behaving the way it is.

    Continue reading...

Compartilhe esta Página