箱型图或盒图:单组数据和多组数据箱线图
运行结果为:
<备注:需安装第三方软件pandas、numpy、seaborn >
代码如下:
# 箱型图或盒图:单组数据和多组数据箱线图
# matplotlib.pyplot.boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None, widths=None, patch_artist=None, bootstrap=None, usermedians=None, conf_intervals=None, meanline=None, showmeans=None, showcaps=None, showbox=None, showfliers=None, boxprops=None, labels=None, flierprops=None, medianprops=None, meanprops=None, capprops=None, whiskerprops=None, manage_xticks=True, autorange=False, zorder=None, hold=None, data=None)
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
'''
# =======单组数据箱线图=========
data = [5, 6, 2, 4, 8, 9, 10, 2, 4, 5, 3, 5, 15]
plt.boxplot(data, labels='A')
'''
# =======多组数据箱线图=========
data1 = [5, 6, 2, 4, 8, 9, 10, 2, 4, 5, 3, 5, 15]
data2 = [1, 2, 7, 9, 5, 7, 6, 8, 2, 3, 4, 10, 2, 4, 0]
plt.boxplot((data1, data2), labels=['A', 'B']) # 竖向箱线图
# plt.boxplot((data1, data2), labels=['A', 'B'], vert=False) # 横向箱线图
plt.show()