Barplot using seaborn in Python

Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated to the data structures from pandas.

seaborn.barplot() method

seaborn.barplot() method is used to draw a barplot. A bar plot represents an estimate of central tendency for a numeric variable with the height of each rectangle and provides some indication of the uncertainty around that estimate using error bars.

Syntax : seaborn.barplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=, ci=95, n_boot=1000, units=None, seed=None, orient=None, color=None, palette=None, saturation=0.75, errcolor=’.26′, errwidth=None, capsize=None, dodge=True, ax=None, **kwargs)

Parameters : This method is accepting the following parameters that are described below :

  • x, y : This parameter take names of variables in data or vector data, Inputs for plotting long-form data.
  • hue : (optional) This parameter take column name for colour encoding.
  • data : (optional) This parameter take DataFrame, array, or list of arrays, Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.
  • color : (optional) This parameter take matplotlib color, Color for all of the elements, or seed for a gradient palette.

Returns : Returns the Axes object with the plot drawn onto it.



Below examples illustrate the barplot() method of the seaborn library.

Example 1 : Draw a set of vertical bar plots grouped by a categorical variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
  
# read a titanic.csv file
# from seaborn libraray
df = sns.load_dataset('titanic')
  
# class v / s fare barplot 
sns.barplot(x = 'class', y = 'fare', data = df)
  
# Show the plot
plt.show()

chevron_right


Output :
barplot - 1

Example 2 : Draw a set of vertical bars with nested grouping by a two variables.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
  
# read a titanic.csv file
# from seaborn libraray
df = sns.load_dataset('titanic')
  
# class v / s fare barplot 
sns.barplot(x = 'class', y = 'fare', hue = 'sex', data = df)
  
# Show the plot
plt.show()

chevron_right


Output :
barplot-2

Example 3 : show a Horizontal barplot.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
  
# read a titanic.csv file
# from seaborn libraray
df = sns.load_dataset('titanic')
  
# fare v / s class horizontal barplot 
sns.barplot(x = 'fare', y = 'class', hue = 'sex', data = df)
  
# Show the plot
plt.show()

chevron_right


Output :
barplot-3

Example 4 : Plot all bars in a given order.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
  
# read a titanic.csv file
# from seaborn libraray
df = sns.load_dataset('titanic')
  
# class v / s fare barplot in given order
sns.barplot(x = 'class', y = 'fare', data = df,
            order = ["Third", "Second", "First"])
  
# Show the plot
plt.show()

chevron_right


Output :
barplot-4

Example 5 : Plot all bars in a single color.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
  
# read a titanic.csv file from seaborn libraray
df = sns.load_dataset('titanic')
  
# class v / s fare barplot with same colour 
sns.barplot(x = 'class', y = 'fare', data = df, color = "salmon")
  
# Show the plot
plt.show()

chevron_right


Output :
barplot-5

Example 6 : barplot without error bars.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
  
# read a titanic.csv file
# from seaborn libraray
df = sns.load_dataset('titanic')
  
# class v / s fare barplot
# without error bars
sns.barplot(x = 'class', y = 'fare', data = df, ci = None)
  
# Show the plot
plt.show()

chevron_right


Output :
barplot-6




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.