Wednesday, 11 October 2023

A Beginner's Guide to box plots in Pyplot

Box plot is used to display the distribution and summary statistics of a dataset. Following are the key components of the Box plot.

 

a.   box: It represents the middle 50% of the data. The top edge of the box is the third quartile (Q3), and the bottom edge of the box is the first quartile (Q1).

b.   whiskers: The whiskers extend from the box to the minimum and maximum values within a certain distance from the quartiles.

c.    median: Represent the middle value of the data. It is visualized by a line inside the box.

d.   mean: The mean is the average of the data. It is often represented by a dot inside the box, but it is not always shown.

 

hello_world.py

import matplotlib.pyplot as plt

data1 = list(range(1, 100))

# Create the box plot
plt.boxplot([data1])

# Show the plot
plt.show()

Output

 

Add multiples boxes to the plot

data1 = list(range(1, 100))
data2 = list(range(1, 75, 3))
data3 = list(range(1, 50, 4))

# Create the box plot
plt.boxplot([data1, data2, data3])

Above snippet draws a box plot with 3 boxes.

 

multiple_boxes.py

import matplotlib.pyplot as plt

data1 = list(range(1, 100))
data2 = list(range(1, 75, 3))
data3 = list(range(1, 50, 4))

# Create the box plot
plt.boxplot([data1, data2, data3])

# Show the plot
plt.show()

Output




 

Add labels to the boxes in a box plot

Using ‘labels’ parameter, we can add labels to the boxes.

labels = ['Box1', 'Box2', 'Box3']

# Create the box plot
plt.boxplot([data1, data2, data3], labels=labels)

multiple_boxes.py

import matplotlib.pyplot as plt

data1 = list(range(1, 100))
data2 = list(range(1, 75, 3))
data3 = list(range(1, 50, 4))

labels = ['Box1', 'Box2', 'Box3']

# Create the box plot
plt.boxplot([data1, data2, data3], labels=labels)

# Show the plot
plt.show()

Output




Add different colors to the boxes

Following snippet adds different colors to the boxes.

boxes = plt.boxplot(data, labels=labels,  patch_artist=True)
index = 0
for box in boxes['boxes']:
    box.set(color='b', linewidth=2, facecolor=colors[index])
    index = index + 1

a.   I set the parameter patch_artist to True parameter in the plt.boxplot() function to enable patch coloring.

b.   Iterating over each box and set the edge color using the parameter ‘color’ and fill the color using the parameter facecolor.

 

Find the below working application.

 

add_colors_to_the_boxes.py

import matplotlib.pyplot as plt

data1 = list(range(1, 100))
data2 = list(range(1, 75, 3))
data3 = list(range(1, 50, 4))
data4 = list(range(1, 30, 5))
data5 = list(range(1, 25, 6))

labels = ['Box1', 'Box2', 'Box3', 'Box4', 'Box5']
colors = ['r', 'green', '#fedcba', (0, 1, 1), (1, 0, 0, 0.5)]
data = [data1, data2, data3, data4, data5]

# Create the box plot
boxes = plt.boxplot(data, labels=labels,  patch_artist=True)
index = 0
for box in boxes['boxes']:
    box.set(color='b', linewidth=2, facecolor=colors[index])
    index = index + 1

# Show the plot
plt.show()

Output



  

Previous                                                    Next                                                    Home

No comments:

Post a Comment