Home > Back-end >  How to plot top 5 values in seaborn
How to plot top 5 values in seaborn

Time:11-21

How can I plot the top 5 values that appear to be the biggest in this plot? (Wii,NES,GB,DS,X360)

sns.violinplot(x=df['Global_Sales'],y=df['Platform'],data=df)

enter image description here

CodePudding user response:

Make sure x and y are column names and then sort/filter the data argument:

  • Either use nlargest (fastest):

    sns.violinplot(
        x='Global_Sales',
        y='Platform',
        data=df.nlargest(5, 'Global_Sales')
    )
    
  • Or sort_values with tail:

        ...
        data=df.sort_values('Global_Sales').tail(5)
    
  • Or sort_values(ascending=False) with head:

        ...
        data=df.sort_values('Global_Sales', ascending=False).head(5)
    

In OP's code, x and y are column data (x=df['Global_Sales']) instead of column names (x='Global_Sales'). If x and y are actual data, the data param is ignored, so df would need to be sorted/filtered before the plotting command.

  • Related