Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To create a box plot in Matplotlib that includes two summary statistics, you can follow the steps below:

  1. Import the necessary libraries:
import matplotlib.pyplot as plt
import numpy as np
  1. Create your dataset:
data1 = np.random.normal(50, 10, 100)
data2 = np.random.normal(70, 5, 100)
  1. Combine the two datasets:
data = [data1, data2]
  1. Set the labels for each dataset:
labels = ['Dataset 1', 'Dataset 2']
  1. Create the box plot using the boxplot() function:
fig, ax = plt.subplots()
ax.boxplot(data, vert=False, labels=labels, showmeans=True, meanline=True)

Here, vert=False makes the plot horizontal, labels adds the labels we created in step 4, showmeans=True and meanline=True show the mean values and a line indicating the mean.

  1. Display the plot:
plt.show()

This will create a box plot with two datasets, labeled and with the mean values shown.