Jayit Ghosh
Jayit Ghosh

Reputation: 141

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?

Upvotes: 0

Views: 418

Answers (2)

Jayit Ghosh
Jayit Ghosh

Reputation: 141

OK, so I didnt know this, although now it seems stupid not to do so in hindsight!

  1. Pass names(or string labels) in the argument for X-axis.

  2. use plt.xticks(rotate=90) so the labels don't overlap

enter image description here

Upvotes: 0

Trenton McKinney
Trenton McKinney

Reputation: 62403

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

Upvotes: 1

Related Questions