Open In App

Boxplot using Seaborn in Python

Last Updated : 10 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 into the data structures from pandas.

Box Plot

A box plot helps to maintain the distribution of quantitative data in such a way that it facilitates the comparisons between variables or across levels of a categorical variable. The main body of the box plot showing the quartiles and the median’s confidence intervals if enabled. The medians have horizontal lines at the median of each box and while whiskers have the vertical lines extending to the most extreme, non-outlier data points and caps are the horizontal lines at the ends of the whiskers.
 

Syntax: seaborn.boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, dodge=True, fliersize=5, linewidth=None, whis=1.5, ax=None, **kwargs)

Parameters: 
x, y, hue: Inputs for plotting long-form data. 
data: Dataset for plotting. If x and y are absent, this is interpreted as wide-form. 
color: Color for all of the elements.

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

Example 1: Basic visualization of “fmri” dataset using violinplot() 

Python3




import seaborn
 
 
seaborn.set(style='whitegrid')
fmri = seaborn.load_dataset("fmri")
 
seaborn.boxplot(x="timepoint",
                y="signal",
                data=fmri)


Output:

Example 2: Basic visualization of “tips” dataset using boxplot() 

Python3




import seaborn
 
 
seaborn.set(style='whitegrid')
tip = seaborn.load_dataset('tips')
 
seaborn.boxplot(x='day', y='tip', data=tip)


Output:
 

Grouping variables in Seaborn boxplot with different attributes

1. Draw a single horizontal box plot using only one axis:

If we use only one data variable instead of two data variables then it means that the axis denotes each of these data variables as an axis.

X denotes an x-axis and y denote a y-axis.

Syntax: 

seaborn.boxplot(x)

Python3




# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
 
seaborn.boxplot(x =tip['total_bill'])


Output:

2. Draw horizontal boxplot:

In the above example we see how to plot a single horizontal boxplot and here can perform multiple horizontal box plots with exchange of the data variable with another axis.

Python3




# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x ='tip', y ='day', data = tip)


Output:

3. Using hue parameter:

While the points are plotted in two dimensions, another dimension can be added to the plot by coloring the points according to a third variable.

Syntax:

seaborn.boxplot(x, y, hue, data);

Python3




# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("fmri")
 
seaborn.boxplot(x ="timepoint",
             y ="signal",
             hue ="region",
             data = fmri)


Output:

4. Draw outlines around the data points using linewidth:

Width of the gray lines that frame the plot elements. Whenever we increase linewidth than the point also will increase automatically.

Syntax:

seaborn.boxplot(x, y, data, linewidth)

Python3




# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x = 'day', y = 'tip',
                data = tip,
                linewidth=2.5)


Output:

5. Draw each level of the hue variable at different locations on the major categorical axis:

When using hue nesting, setting dodge should be True will separate the point for different hue levels along the categorical axis. And Palette is used for the different levels of the hue variable.

Syntax:

seaborn.boxplot(x, y, data, hue, palette, dodge)

Python3




# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x="day", y="total_bill",
                hue="smoker",
                data=tips, palette="Set2",
                dodge=True)


Output:

Possible values of palette are:

Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r,

GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r,

Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r,

Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1,

Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr,

YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r,

cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth,

gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, 

6. Control orientation of the plot (vertical or horizontal):

When we use orient as “h” then it plots the vertical and if we use “V” then it refers to the vertical.

Syntax:

seaborn.boxplot( data, orient )

Python3




# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(data = tip,orient="h")


Output:

Let’s check for vertical orient:

Python3




seaborn.boxplot(data = tip,orient="v")


Output:

7. Using color attributes for Color for all the elements.:

Python3




# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x = 'day', y = 'tip', data = tip,color = "green")


Output:

 



Previous Article
Next Article

Similar Reads

How to Show Mean on Boxplot using Seaborn in Python?
A boxplot is a powerful data visualization tool used to understand the distribution of data. It splits the data into quartiles, and summarises it based on five numbers derived from these quartiles: median: the middle value of data. marked as Q2, portrays the 50th percentile.first quartile: the middle value between "minimum non-outlier" and median.
2 min read
Sort Boxplot by Mean with 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.Box Plot is the visual representation of the depicting
3 min read
How To Use Seaborn Color Palette to Color Boxplot?
Box Plot is the visual representation of the depicting groups of numerical data through their quartiles. Boxplot is also used to detect the outlier in the data set. It captures the summary of the data efficiently with a simple box and whiskers and allows us to compare easily across groups. Adding the right set of colors to Boxplot can reveal a lot
3 min read
How To Make Grouped Boxplot with Seaborn Catplot?
Prerequisite: seaborn A grouped boxplot is a boxplot where categories are organized in groups and subgroups. Whenever we want to visualize data in the group and subgroup format the Seaborn Catplot() plays a major role. The following example visualizes the distribution of 7 groups (called A to G) and 2 subgroups (called low and high) in grouped boxp
2 min read
How To Manually Order Boxplot in Seaborn?
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 into the data structures from pandas.Seaborn aims to make visualization of the central pa
3 min read
Matplotlib.axes.Axes.boxplot() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute. matplotlib.axes.Axes.boxplot() Function The Axes.boxpl
3 min read
How to display text on boxplot in Python?
Boxplot is an important graphical plot that can be used to get a summary of data present in numerical form. The plot can give us information about statistical measures such as percentile, median, minimum and maximum values of the numerical data. In the box plot, the line which passes through the center of the box represents the median value. In thi
3 min read
How to Create Boxplot from Pandas DataFrame?
Box plot is also called a Whisker plot which provides a summary of a set of data that includes minimum, first-quartile, median, third quartile, and maximum value. This Box plot is present in the matplotlib library. In the Box plot graph, the x-axis represents the data we are going to plot and the y-axis represents frequency. Method 1: Using DataFra
3 min read
Grid Plot in Python using Seaborn
Grids are general types of plots that allow you to map plot types to grid rows and columns, which helps you to create similar character-separated plots. In this article, we will be using two different data sets (Iris and Tips) for demonstrating grid plots Using Iris Dataset We are going to use the Iris dataset which is a very famous dataset availab
4 min read
Countplot 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.countplot() seaborn.countplot() method is use
7 min read
Article Tags :
Practice Tags :