miguelangelcv
miguelangelcv

Reputation: 127

Rename ticks from y-axis in seaborn.barplot

I have the following dataframe, called 'df_top_movies' :

df_top_movies

And later I display this data in a seaborn.barplot:

sns.barplot(x="revenue", y="title", data=df_top_movies).set(ylabel='movie')

barplot

Is it posible to show in y-axis the title of the movie with release_year?

Example:

Previously, I've created a list with the movies and the year:

target_names = list(df_top_movies.apply(lambda movie : movie['title'] + " ("+ str(movie['release_year']) + ")", axis=1))

Upvotes: 1

Views: 2446

Answers (1)

Daniel
Daniel

Reputation: 12026

Of course.

ax = sns.barplot(x="revenue", y="title", data=df_top_movies)
ax.set_yticklabels(target_names)

The function sns.barplot returns a matplotlib object of the type matplotlib.axes._subplots.AxesSubplot. These objects offer multiple methods to customise ticks and axes. It's a common pattern to generate a seaborn plot and then tweak it by calling the ax set methods.

You can see the documentation of set_yticklabels here. You'll see that the method accepts other arguments to change, for example, the fontsize and orientation of the labels.

Upvotes: 2

Related Questions