# Box Plot in Python using Matplotlib

## Box Plot in Python using Matplotlib

Box Plot is also known as Whisker plot is created to display the summary of the set of data values having properties like minimum, first quartile, median, third quartile and maximum. In the box plot, a box is created from the first quartile to the third quartile, a vertical line is also there which goes through the box at the median. Here x-axis denotes the data to be plotted while the y-axis shows the frequency distribution.

### Creating Box Plot

The matplotlib.pyplot module of matplotlib library provides boxplot() function with the help of which we can create box plots.

Syntax:

matplotlib.pyplot.boxplot(data, notch=None, vert=None, patch_artist=None, widths=None)

Parameters:

The data values given to the ax.boxplot() method can be a Numpy array or Python list or Tuple of arrays. Let us create the box plot by using numpy.random.normal() to create some random data, it takes mean, standard deviation, and the desired number of values as arguments.

Example:

 `# Import libraries` `import` `matplotlib.pyplot as plt` `import` `numpy as np` `# Creating dataset` `np.random.seed(``10``)` `data ``=` `np.random.normal(``100``, ``20``, ``200``)` `fig ``=` `plt.figure(figsize ``=``(``10``, ``7``))` `# Creating plot` `plt.boxplot(data)` `# show plot` `plt.show()`

Output:

### Customizing Box Plot

The matplotlib.pyplot.boxplot() provides endless customization possibilities to the box plot. The notch = True attribute creates the notch format to the box plot, patch_artist = True fills the boxplot with colors, we can set different colors to different boxes.The vert = 0 attribute creates horizontal box plot. labels takes same dimensions as the number data sets.

Example 1:

 `# Import libraries` `import` `matplotlib.pyplot as plt` `import` `numpy as np` `# Creating dataset` `np.random.seed(``10``)` `data_1 ``=` `np.random.normal(``100``, ``10``, ``200``)` `data_2 ``=` `np.random.normal(``90``, ``20``, ``200``)` `data_3 ``=` `np.random.normal(``80``, ``30``, ``200``)` `data_4 ``=` `np.random.normal(``70``, ``40``, ``200``)` `data ``=` `[data_1, data_2, data_3, data_4]` `fig ``=` `plt.figure(figsize ``=``(``10``, ``7``))` `# Creating axes instance` `ax ``=` `fig.add_axes([``0``, ``0``, ``1``, ``1``])` `# Creating plot` `bp ``=` `ax.boxplot(data)` `# show plot` `plt.show()`

Output:

Example 2: Let’s try to modify the above plot with some of the customizations:

 `# Import libraries` `import` `matplotlib.pyplot as plt` `import` `numpy as np` `# Creating dataset` `np.random.seed(``10``)` `data_1 ``=` `np.random.normal(``100``, ``10``, ``200``)` `data_2 ``=` `np.random.normal(``90``, ``20``, ``200``)` `data_3 ``=` `np.random.normal(``80``, ``30``, ``200``)` `data_4 ``=` `np.random.normal(``70``, ``40``, ``200``)` `data ``=` `[data_1, data_2, data_3, data_4]` `fig ``=` `plt.figure(figsize ``=``(``10``, ``7``))` `ax ``=` `fig.add_subplot(``111``)` `# Creating axes instance` `bp ``=` `ax.boxplot(data, patch_artist ``=` `True``,` `                ``notch ``=``'True'``, vert ``=` `0``)` `colors ``=` `[``'#0000FF'``, ``'#00FF00'``,` `          ``'#FFFF00'``, ``'#FF00FF'``]` `for` `patch, color ``in` `zip``(bp[``'boxes'``], colors):` `    ``patch.set_facecolor(color)` `# changing color and linewidth of` `# whiskers` `for` `whisker ``in` `bp[``'whiskers'``]:` `    ``whisker.``set``(color ``=``'#8B008B'``,` `                ``linewidth ``=` `1.5``,` `                ``linestyle ``=``":"``)` `# changing color and linewidth of` `# caps` `for` `cap ``in` `bp[``'caps'``]:` `    ``cap.``set``(color ``=``'#8B008B'``,` `            ``linewidth ``=` `2``)` `# changing color and linewidth of` `# medians` `for` `median ``in` `bp[``'medians'``]:` `    ``median.``set``(color ``=``'red'``,` `               ``linewidth ``=` `3``)` `# changing style of fliers` `for` `flier ``in` `bp[``'fliers'``]:` `    ``flier.``set``(marker ``=``'D'``,` `              ``color ``=``'#e7298a'``,` `              ``alpha ``=` `0.5``)` `    ` `# x-axis labels` `ax.set_yticklabels([``'data_1'``, ``'data_2'``,` `                    ``'data_3'``, ``'data_4'``])` `# Adding title` `plt.title(``"Customized box plot"``)` `# Removing top axes and right axes` `# ticks` `ax.get_xaxis().tick_bottom()` `ax.get_yaxis().tick_left()` `    ` `# show plot` `plt.show(bp)`

Output:

Last Updated on October 26, 2021 by admin

## Python set() methodPython set() method

Python | set() method Set, a term in mathematics for a sequence consisting of distinct language is also extended in its language by Python and can easily be made using set(). set() method is used to convert any of the iterable

## How to get column names in Pandas dataframeHow to get column names in Pandas dataframe

While analyzing the real datasets which are often very huge in size, we might need to get the column names in order to perform some certain operations. Let’s discuss how to get column names in Pandas dataframe. First, let’s create

## Pandas DataFrame.isinPandas DataFrame.isin

When working with Pandas DataFrames, there are often cases where we need to check if a value is present in a column or not. One way to achieve this is to use the isin() method of a DataFrame, which returns

## reduce() in Pythonreduce() in Python

Python reduce() The reduce(fun,seq) function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along.This function is defined in “functools” module. Working :  At first step, first

## Get unique values from a column in Pandas DataFrameGet unique values from a column in Pandas DataFrame

In this article, we will explore different methods to extract unique values from a column in a Pandas DataFrame. The unique values in a column can provide valuable insights and help in various data analysis tasks. Method 1: Using the

## Pandas – GroupBy One Column and Get Mean, Min, and Max valuesPandas – GroupBy One Column and Get Mean, Min, and Max values

Pandas – GroupBy One Column and Get Mean, Min, and Max values   We can use Groupby function to split dataframe into groups and apply different operations on it. One of them is Aggregation. Aggregation i.e. computing statistical parameters for

## Pattern matching in Python with RegexPattern matching in Python with Regex

Pattern matching in Python with Regex You may be familiar with searching for text by pressing ctrl-F and typing in the words you’re looking for. Regular expressions go one step further: They allow you to specify a pattern of text

## Python – Intersection of two listsPython – Intersection of two lists

Python – Intersection of two lists   Intersection of two list means we need to take all those elements which are common to both of the initial lists and store them into another list. Now there are various ways in