Ask Your Question
2

What is the process for creating a box plot in Matplotlib that includes two summary statistics?

asked 2021-08-23 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-11-03 20:00:00 +0000

ladyg gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-23 11:00:00 +0000

Seen: 9 times

Last updated: Nov 03 '22