How to Create Boxplot from Pandas DataFrame?
A box plot (or whisker plot) is a statistical graph that shows the minimum, first quartile (Q1), median, third quartile (Q3) and maximum values of a dataset. It helps analyze data spread, skewness and outliers and is widely used in data visualization. In this article you'll learn how to create box plots using Pandas, detect outliers and explore different methods to generate them in Python.
Using .plot() function
We can create a box plot on each column of a Pandas DataFrame by following the below syntax-
DataFrame_Name['column_name'].plot(kind='box', title='title_of_plot')
Finding Quartiles in Pandas
Before creating a boxplot you can calculate quartiles using the quantile() function.
import pandas as pd
data = pd.Series([1, 2, 3, 4, 5, 6])
print(data.quantile([0.25, 0.5, 0.75]))
Output:

Example 1: Boxplot without an Outlier
Consider the below data to create a DataFrame and to plot a box plot on it. Let’s create it and generate a boxplot for the "Marks" column.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.DataFrame({'Name': ['Akhil', 'Nikhil', 'Satyam', 'Sravan', 'Pavan'],
'Marks': [77, 95, 89, 78, 64],
'Credits': [8, 10, 9, 8, 7]})
data['Marks'].plot(kind='box', title='Marks of Students')
plt.show()
Output:
Example 2: Boxplot with an Outlier
If a dataset contains outliers they will be marked separately in the boxplot. Like in below example the minimum mark of the student is 10 which is very small and far from other marks (data points). So it is indicated as o at the bottom which represents an outlier. If any of the data points in the data is much larger or smaller compared to other values then the following plot will be generated.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.DataFrame({'Name': ['Akhil', 'Nikhil', 'Satyam', 'Sravan', 'Pavan'],
'Marks': [77, 95, 89, 78, 10],
'Credits': [8, 10, 9, 8, 0]})
data['Marks'].plot(kind='box', title='Marks of students')
plt.show()
Output:

Using pandas.DataFrame.boxplot() function
Pandas also provides the boxplot() function to create a boxplot directly. We use pandas.DataFrame.boxplot to draw the box plot for respective columns in a DataFrame. Here we plotted the boxplot using the boxplot method instead of using the plot method and specifying its kind. As we did not specify the grid argument as a parameter in the boxplot method, it will consider the default value i.e. True.
import pandas as pd
data = pd.DataFrame({'Name': ['Akhil', 'Nikhil', 'Satyam', 'Sravan', 'Pavan'],
'Marks': [77, 95, 89, 78, 64],
'Credits': [8, 10, 9, 8, 7]})
data.boxplot(column='Marks')
Output:
