Search code examples
matplotlibplotxticks

Replace xticks with names


I am working on the Spotify dataset from Kaggle. I plotted a barplot showing the top artists with most songs in the dataframe. But the X-axis is showing numbers and I want to show names of the Artists.

names = list(df1['artist'][0:19])

plt.figure(figsize=(8,4))
plt.xlabel("Artists")

sns.barplot(x=np.arange(1,20),
                y=df1['song_title'][0:19]);

I tried both list and Series object type but both are giving error.

enter image description here

How to replace the numbers in xticks with names?


Solution

  • Imports

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

    Data

    df = pd.read_csv('Spotify-2000.csv')
    titles = pd.DataFrame(df.groupby(['Artist'])['Title'].count()).reset_index().sort_values(['Title'], ascending=False).reset_index(drop=True)
    titles.rename(columns={'Title': 'Title Count'}, inplace=True)
    
    # titles.head()
    
                 Artist  Title Count
                  Queen           37
            The Beatles           36
               Coldplay           27
                     U2           26
     The Rolling Stones           24
    

    Plot

    plt.figure(figsize=(8, 4))
    chart = sns.barplot(x=titles.Artist[0:19], y=titles['Title Count'][0:19])
    chart.set_xticklabels(chart.get_xticklabels(), rotation=90)
    plt.show()
    

    enter image description here