Skip to content
Related Articles

Related Articles

Seaborn.barplot() method in Python
  • Last Updated : 10 Aug, 2020

Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. There is just something extraordinary about a well-designed visualization. The colors stand out, the layers blend nicely together, the contours flow throughout, and the overall package not only has a nice aesthetic quality, but it provides meaningful insights to us as well.

seaborn.barplot() method

A barplot is basically used to aggregate the categorical data according to some methods and by default it’s the mean. It can also be understood as a visualization of the group by action. To use this plot we choose a categorical column for the x-axis and a numerical column for the y-axis, and we see that it creates a plot taking a mean per categorical column.

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

Parameters :

Arguments                         Value                                                                              Description
x, y, huenames of variables in “data“ or vector data, optionalInputs for plotting long-form data. See examples for interpretation.
dataDataFrame, array, or list of arrays, optionalDataset for plotting. If “x“ and “y“ are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.
order, hue_orderlists of strings, optionalOrder to plot the categorical levels in, otherwise the levels are inferred from the data objects.
estimatorcallable that maps vector -> scalar, optionalStatistical function to estimate within each categorical bin.
cifloat or “sd” or None, optionalSize of confidence intervals to draw around estimated values.  If “sd”, skip bootstrapping and draw the standard deviation of the observations. If “None“, no bootstrapping will be performed, and error bars will not be drawn.
n_bootint, optionalNumber of bootstrap iterations to use when computing confidence intervals.
unitsname of variable in “data“ or vector data, optionalIdentifier of sampling units, which will be used to perform a multilevel bootstrap and account for repeated measures design. 
orient“v” | “h”, optionalOrientation of the plot (vertical or horizontal). This is usually inferred from the dtype of the input variables, but can be used to specify when the “categorical” variable is a numeric or when plotting wide-form data.
colormatplotlib color, optionalColor for all of the elements, or seed for a gradient palette.
palettepalette name, list, or dict, optionalColors to use for the different levels of the “hue“ variable. Should be something that can be interpreted by :func:`color_palette`, or a dictionary mapping hue levels to matplotlib colors.
saturationfloat, optionalProportion of the original saturation to draw colors at. Large patches often look better with slightly desaturated colors, but set this to “1“ if you want the plot colors to perfectly match the input color spec.
errcolormatplotlib colorColor for the lines that represent the confidence interval.
errwidthfloat, optionalThickness of error bar lines (and caps). 
capsizefloat, optionalWidth of the “caps” on error bars.
dodgebool, optionalWhen hue nesting is used, whether elements should be shifted along the categorical axis. 
axmatplotlib Axes, optionalAxes object to draw the plot onto, otherwise uses the current Axes.
kwargsey, value mappingsOther keyword arguments are passed through to “plt.bar“ at draw time.

Following steps are used :



  • Import Seaborn
  • Load Dataset from Seaborn as it contain good collection of datasets.
  • Plot Bar graph using seaborn.barplot() method.

Below is the implementation :

Example 1:

Python3




# 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')
 
# who v/s fare barplot
sns.barplot(x = 'who',
            y = 'fare',
            data = df)
 
# Show the plot
plt.show()

Output:

simple barpot

Example 2:

Python3




# 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')
 
 
# who v/s fare barplot
sns.barplot(x = 'who',
            y = 'fare',
            hue = 'class',
            data = df)
 
# Show the plot
plt.show()

Output:



barplot - 2

Example 3:

Python3




# 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')
 
 
# who v/s fare barplot
sns.barplot(x = 'who',
            y = 'fare',
            hue = 'class',
            data = df,
            palette = "Blues")
 
# Show the plot
plt.show()

Output:

barplot - 3

Example 4:

Python3




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

Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :