How to plot Boxplot in Python

BySlide Scope

How to plot Boxplot in Python

A box plot is used to visualize 5 values in a dataset for the selected column(s):

  • Minimum Value
  • First Quartile or 25%
  • Median (Second Quartile) or 50%
  • Third Quartile or 75%
  • Maximum value

Box Plot is also known as Box and Whisker Plot.

Steps –

  1. Load the dataset using Pandas dataframe
  2. Select any column to visualize
  3. Plot boxplot using Pandas
    OR
  4. Plot boxplot using Seaborn

Python Code :

import pandas as pd

#load data

data = pd.read_csv(‘insurance.csv’)

data.head(10)

index age gender bmi children smoker region charges id
0 19 female 27.900 0 yes southwest 16884.92400 1
1 18 male 33.770 1 no southeast 1725.55230 2
2 28 male 33.000 3 no southeast 4449.46200 3
3 33 male 22.705 0 no northwest 21984.47061 4
4 32 male 28.880 0 no northwest 3866.85520 5
5 31 female 25.740 0 no southeast 3756.62160 6
6 46 female 33.440 1 no southeast 8240.58960 7
7 37 female 27.740 3 no northwest 7281.50560 8
8 37 male 29.830 2 no northeast 6406.41070 9
9 60 female 25.840 0 no northwest 28923.13692 10

data.describe()

age bmi children charges id
count 1338.000000 1338.000000 1338.000000 1338.000000 1338.000000
mean 39.207025 30.663397 1.094918 13270.422265 669.500000
std 14.049960 6.098187 1.205493 12110.011237 386.391641
min 18.000000 15.960000 0.000000 1121.873900 1.000000
25% 27.000000 26.296250 0.000000 4740.287150 335.250000
50% 39.000000 30.400000 1.000000 9382.033000 669.500000
75% 51.000000 34.693750 2.000000 16639.912515 1003.750000
max 64.000000 53.130000 5.000000 63770.428010 1338.000000

# In pandas boxplot one attribute, column is required to plot boxplot
# Column can take name of one column of the dataset or the list of columns
data.boxplot(column=[‘age’],figsize=[10,7])

# We can group data as well.

data.boxplot(column=[‘age’], by=[‘gender’], figsize=[10,7])

Boxplot Using Seaborn Library

About the author

Slide Scope administrator

Slide Scope Posts are written by experts from Core Information Technology and Digital Marketing Industry. We have assistants from Management Science as well.

Leave a Reply