需求:
1. 直接给出箱线图的上下四分位数、中位值等画图
2. 给出序列,再画boxplot
代码1:
matplotlib.axes.Axes 类给出bxp 方法
"fliers":[] outliers 是可选参数,可以通过这个参数给出离群值(异常值)
import matplotlib.pyplot as plt
import matplotlib as mpl
draw_data = [
{
"label":"A",
'whislo': 24, # Bottom whisker position
'q1' : 25, # First quartile (25th percentile)
'med' : 26, # Median (50th percentile)
'q3' : 27, # Third quartile (75th percentile)
'whishi': 28
#"fliers":[] outliers
},
{
"label":"B",
'whislo': 25, # Bottom whisker position
'q1' : 25, # First quartile (25th percentile)
'med' : 26, # Median (50th percentile)
'q3' : 27, # Third quartile (75th percentile)
'whishi': 28
}
]
fig = plt.figure(figsize=(10,6))
ax = fig.add_subplot()
ax.bxp(draw_data, showfliers=False)
ax.set_ylabel("value")
ax.set_xlabel("label")
代码2
可以是dataframe,指定data=dataframe,也可以是以下2个列表,自动计算上下四分位值、中位数、离群值等
import seaborn as sns
xlabel = ['A','A','A','A','A','A','A','B','B','B','B']
value = [23,12,34,20,21,43,21,28,23,29,29]
ax = sns.boxplot(x=xlabel, y=value)