边缘箱线图
箱图是用来显示变量分布的统计图,直方图更看重与在取值范围内的分布,而箱线图更看重于观察变量间的重要分割点
箱线图能够显示数据分布的关键数据的节点,常用来作为查找异常值的方式
1当直方图的分布越接近正态分布,箱线图就会越对称
2直方图的柱子越高的地方,就是箱线图的箱子所在的地方
3箱线图所显示的中位数一般都接近直方图的最高的柱子
##重要的参数详解##########
sns.boxplot()
重要的参数
x:需要绘制的箱线图的变量
y:需要绘制箱线图的子图
orient:箱线图的子图,可选填‘v’过着‘h’来决定箱线图的方向
meanline:是否显示中值线
#简单实列
##正态分布下的随机数
X=np.random.randn(10000)
##严重偏态下的正态分布
x=np.random.randint(0,300,50).tolist()+np.random.randint(300,800,50).tolist()+np.random.randint(800,1000,900).tolist()
fig,(ax1,ax2) =plt.subplots(2)
#直方图
ax1.hist(X,bins=100)
#箱线图
sns.boxplot(x=X,
ax=ax2,
orient='vertical',
color='red',
meanline=True
);
##########严重偏态下的分布############
fig,(ax1,ax2) =plt.subplots(2)
#直方图
ax1.hist(x,bins=100)
#箱线图
sns.boxplot(x=x,
ax=ax2,
orient='h',
color='red',
meanline=True
);
############################
Q3,Q1 =np.percentile(x,[75,25])
iqr=Q3-Q1
Q1-1.5*iqr
#min 函数
###应用####
import numpy as np
import importlib as mpl
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
#######绘图#############
#创建画布与格子
fig =plt.figure(figsize=(16,10),dpi=80,facecolor='white')
grid =plt.GridSpec(4,4,hspace=0.5,wspace=0.2)#4*4的格子
sns.set_style("darkgrid") #s设立风格
#在分割完毕的画布上确定子图的位置
ax_main=fig.add_subplot(grid[:-1,:-1])
ax_right=fig.add_subplot(grid[:-1,-1],xticklabels=[],yticklabels=[])
ax_bottom=fig.add_subplot(grid[-1,0:-1],xticklabels=[],yticklabels=[])
#在中心绘制汽包图
ax_main.scatter("displ","hwy"
,s=df.cty*4 #设置尺寸以影响气泡图,这里是城市里程/加仑
,data=df
,c=df.manufacturer.astype('category').cat.codes #这是按类别编码的一种方法
,cmap='autumn'#colormap可以根据自己喜欢的随意修改
,edgecolors ='gray',linewidths=.5, alpha=.9
)
#在右侧和下侧绘制箱型图
sns.boxplot(x=df.hwy,
ax=ax_right,
orient="v",
color='red',
#meanline=True
);
sns.boxplot(x=df.displ,
ax=ax_bottom,
orient='h',
color='orange',
#meanline=True
);
#装饰图像
#移除箱线图自带的坐标轴名称
ax_right.set(xlabel='')
ax_bottom.set(xlabel='')
######################
plt.rcParams['font.sans-serif']=['Simhei'] #显示中文,为微软雅黑
ax_main.set(title='边缘直方图\n发动机排量vs公路里程/加仑'
,xlabel ='发动机排量(L)'
,ylabel ='公路里程/加仑')
####################
for item in [ax_bottom,ax_right]:
item.set_xticks([])
item.set_yticks([])
###################
ax_main.title.set_fontsize(20) #对标题设置字体的大小
##################
for item in ([ax_main.xaxis.label,ax_main.yaxis.label]+ax_main.get_xticklabels()+ax_main.get_yticklabels()):####知识点##
item.set_fontsize(14) #对所有横坐标纵坐标上的标题与标尺都设置字体的大小
有个错误一直没没找出来,右侧的箱型图在参数中设置v也竖立不起来??