I am creating a boxplot with matplotlib. How can I get the points inside the box ?
import matplotlib.pyplot as plt
data = [2,4,5,62,57,3,8,3,96,2,34,43]
box=plt.boxplot(data)
The points inside the box are the values that lie inside the interquartile range. The 1st and 3rd quartiles can be calculated with np.quartile
, and once you know those values you can conditionally sample your data array:
import matplotlib.pyplot as plt
import numpy as np
data = np.array([2,4,5,62,57,3,8,3,96,2,34,43])
box=plt.boxplot(data)
# Get all the data between the 1st and 3rd quartile
data_iqr = data[ (data > np.percentile(data, 25)) & (data < np.percentile(data, 75)) ]
# Visual confirmation:
plt.scatter(np.ones_like(data_iqr), data_iqr, marker='o', label='IQR')
plt.scatter(np.ones_like(data), data, marker='x', label='all')
plt.legend(frameon=False, loc='best')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.