plt.boxplot
时间: 2023-10-12 11:11:21 浏览: 90
Python使用plt.boxplot() 参数绘制箱线图
The `plt.boxplot()` function is a method in the Matplotlib library used to create box plots, also known as box-and-whisker plots. Box plots are used to show the distribution of a dataset by displaying the median, quartiles, and outliers of the data.
The function takes in a list of data values as its primary input and returns a box plot. Additional parameters can be used to customize the appearance of the plot, such as changing the color, width, and style of the boxes and whiskers.
Here is an example of using `plt.boxplot()` function to create a box plot for a dataset:
```python
import matplotlib.pyplot as plt
import numpy as np
# create a dataset
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
# create a box plot
plt.boxplot(data)
# add labels and title
plt.xticks([1, 2, 3], ['Data 1', 'Data 2', 'Data 3'])
plt.ylabel('Value')
plt.title('Box plot example')
# display the plot
plt.show()
```
This code will generate a box plot that shows the distribution of three datasets with different standard deviations. The x-axis labels indicate which dataset each box represents, and the y-axis shows the values of the data. The box represents the interquartile range (IQR), which is the middle 50% of the data, and the whiskers extend to the minimum and maximum values within 1.5 times the IQR. Any values outside of this range are considered outliers and are plotted as individual points.
阅读全文