251

I'm trying to use my own labels for a Seaborn barplot with the following code:

import pandas as pd
import seaborn as sns
    
fake = pd.DataFrame({'cat': ['red', 'green', 'blue'], 'val': [1, 2, 3]})
fig = sns.barplot(x = 'val', y = 'cat', 
                  data = fake, 
                  color = 'black')
fig.set_axis_labels('Colors', 'Values')

desired result

However, I get an error that:

AttributeError: 'AxesSubplot' object has no attribute 'set_axis_labels'

What gives?

cottontail
  • 10,268
  • 18
  • 50
  • 51
Erin Shellman
  • 3,553
  • 4
  • 21
  • 26

5 Answers5

400

Seaborn's barplot returns an axis-object (not a figure). This means you can do the following:

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

fake = pd.DataFrame({'cat': ['red', 'green', 'blue'], 'val': [1, 2, 3]})
ax = sns.barplot(x = 'val', y = 'cat', 
              data = fake, 
              color = 'black')
ax.set(xlabel='common xlabel', ylabel='common ylabel')
plt.show()
mwaskom
  • 46,693
  • 16
  • 125
  • 127
sascha
  • 32,238
  • 6
  • 68
  • 110
73

One can avoid the AttributeError brought about by set_axis_labels() method by using the matplotlib.pyplot.xlabel and matplotlib.pyplot.ylabel.

matplotlib.pyplot.xlabel sets the x-axis label while the matplotlib.pyplot.ylabel sets the y-axis label of the current axis.

Solution code:

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

fake = pd.DataFrame({'cat': ['red', 'green', 'blue'], 'val': [1, 2, 3]})
fig = sns.barplot(x = 'val', y = 'cat', data = fake, color = 'black')
plt.xlabel("Colors")
plt.ylabel("Values")
plt.title("Colors vs Values") # You can comment this line out if you don't need title
plt.show(fig)

Output figure:

enter image description here

Steffi Keran Rani J
  • 3,667
  • 4
  • 34
  • 56
  • I think it should be just `plt.show(fig)`. It gives me TypeError: show() takes 1 positional argument but 2 were given. Just `plt.show()` gives me the desired result – Joe Ferndz Feb 09 '21 at 01:20
  • Doing it separately like this also has the advantage of specifying other miscellaneous [matplotlib.text.Text](https://matplotlib.org/stable/api/text_api.html#matplotlib.text.Text) properties. I used it most commonly to specify a custom font that our organization uses by setting fontproperties equal to the location of the TTF file. – Neelotpal Shukla Oct 28 '21 at 21:01
45

You can also set the title of your chart by adding the title parameter as follows

ax.set(xlabel='common xlabel', ylabel='common ylabel', title='some title')
DaFois
  • 2,197
  • 8
  • 26
  • 43
John R
  • 451
  • 4
  • 5
8

Another way of doing it, would be to access the method directly within the seaborn plot object.

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

fake = pd.DataFrame({'cat': ['red', 'green', 'blue'], 'val': [1, 2, 3]})
ax = sns.barplot(x = 'val', y = 'cat', data = fake, color = 'black')

ax.set_xlabel("Colors")
ax.set_ylabel("Values")

ax.set_yticklabels(['Red', 'Green', 'Blue'])
ax.set_title("Colors vs Values") 

Produces:

enter image description here

Mitchell van Zuylen
  • 3,905
  • 4
  • 27
  • 64
simons____
  • 101
  • 1
  • 2
0

The axis labels are the names of columns, so you can just rename (or set_axis) columns inside the barplot call:

fake = pd.DataFrame({'cat': ['red', 'green', 'blue'], 'val': [1, 2, 3]})
sns.barplot(x='Values', y='Colors', 
            data=fake.rename(columns={'cat': 'Colors', 'val': 'Values'}), 
            #data=fake.set_axis(['Colors', 'Values'], axis=1), 
            color='black');

Another way is, since barplot returns the Axes object, you can just chain a set call to it.

sns.barplot(x='val', y='cat', data=fake, color='black').set(xlabel='Values', ylabel='Colors', title='My Bar Chart');

res


If the data is a pandas object, pandas has a plot method that has a lot of options to pass ranging from titles, axis labels to figsize.

fake.plot(y='val', x='cat', kind='barh', color='black', width=0.8, legend=None,
          xlabel='Colors', ylabel='Values', title='My Bar Chart', figsize=(12,5));
cottontail
  • 10,268
  • 18
  • 50
  • 51