matplotlib【1.2.1】-boxplot(箱线图)-1

版权声明:版权所有,翻版必究【Kevin】 https://blog.csdn.net/weixin_30935137/article/details/83998300

示例1:普通箱线图

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
mpl.rcParams['axes.unicode_minus'] = False #用来正常显示负号

testA = np.random.randn(1000)
testB = np.random.randn(1000)

testList=[testA,testB]
labels = ['随机数生成器A','随机数生成器B']


whis = 1.6
width = 0.35

bplot = plt.boxplot(testList,
                    whis = whis,#四分位距的倍数,用来确定箱须包含数据的范围的大小
                    widths = width,#箱体的宽度
                    sym = 'o',#离群值标记样式
                    labels=labels,#每个数据集的刻度标签
                    #notch = True,#v型凹痕箱体
                    #vert=False,#水平方向箱线图
                    #showfliers=False, #是否标记离群值
                    patch_artist=True #是否要颜色填充
                    )
plt.ylabel('随机数值')
plt.title('生成器抗干扰能力的稳定性比较')
plt.show()


在这里插入图片描述

案例2:自定义颜色设置

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
mpl.rcParams['axes.unicode_minus'] = False #用来正常显示负号

testA = np.random.randn(1000)
testB = np.random.randn(1000)

testList=[testA,testB]
labels = ['随机数生成器A','随机数生成器B']


whis = 1.6
width = 0.35

bplot = plt.boxplot(testList,
                    whis = whis,#四分位距的倍数,用来确定箱须包含数据的范围的大小
                    widths = width,#箱体的宽度
                    sym = 'o',#离群值标记样式
                    labels=labels,#每个数据集的刻度标签
                    #notch = True,#v型凹痕箱体
                    #vert=False,#水平方向箱线图
                    #showfliers=False, #是否标记离群值
                    patch_artist=True #是否要颜色填充
                    )

#自定义颜色设置
#colors =['#1b9177','#d95f02'] #颜色设置
colors = ['pink', 'lightblue']
for patch,color in zip(bplot['boxes'],colors):
    patch.set_facecolor(color)

plt.ylabel('随机数值')
plt.title('生成器抗干扰能力的稳定性比较')
plt.show()

在这里插入图片描述

示例3:另一种方式设置x轴刻度标签

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
mpl.rcParams['axes.unicode_minus'] = False #用来正常显示负号

testA = np.random.randn(1000)
testB = np.random.randn(1000)

testList=[testA,testB]
labels = ['随机数生成器A','随机数生成器B']


whis = 1.6
width = 0.35

bplot = plt.boxplot(testList,
                    whis = whis,#四分位距的倍数,用来确定箱须包含数据的范围的大小
                    widths = width,#箱体的宽度
                    sym = 'o',#离群值标记样式
                    #labels=labels,#每个数据集的刻度标签
                    #notch = True,#v型凹痕箱体
                    #vert=False,#水平方向箱线图
                    #showfliers=False, #是否标记离群值
                    patch_artist=True #是否要颜色填充
                    )

#自定义颜色设置
#colors =['#1b9177','#d95f02'] #颜色设置
colors = ['pink', 'lightblue']
for patch,color in zip(bplot['boxes'],colors):
    patch.set_facecolor(color)

plt.xticks([i+1 for i in range(len(testList))],labels)   #设置x轴刻度标签 
    
plt.ylabel('随机数值')
plt.title('生成器抗干扰能力的稳定性比较')
plt.show()


在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_30935137/article/details/83998300
今日推荐