问题描述
我有一个数据集。使用seaborn或matplotlib以2 x 3的图形形式方便地显示sns.lmplot
之类的图形。我尝试过,但是它们显示在一列中。某某某某我尝试,但它不起作用。所有表都包含“ SalePrice”中变量的相关性
fig,ax = plt.subplots(4,len(qualitative)/4,figsize=(4*len(qualitative),4))
sns.lmplot(trainInt,y_vars = ["SalePrice"],x_vars = ('MSSubClass','LotFrontage','LotArea','OverallQual','OverallCond','YearBuilt'))
有一个带有数据的熊猫表
trainInt
-这是一张熊猫桌
MSSubClass LotFrontage总体质量总体条件年度销售价格
10 23 6 43 8 12
12 20 2 46 8 19
11 31 3 13 8 24
您应该在两行三列中得到六个依赖关系图sns.lmplot
:
SalePrice \ | \ ____ SalePrice I ------ SalePrice 我------我
MSSubClass LotFrontage综合质量
。
。
SalePrice \ | \ I SalePrice I --- \ --- SalePrice 我--I ---- |
总体状态年构建销售价格
解决方法
- 为了绘制数据,必须使用
pandas.DataFrame.stack
将数据框从宽格式转换为长(整齐)格式-
SalePrice
必须保留为一列,以与其相对应并在索引中,这可以通过.reset_index
并指定drop=False
完成
-
- 使用
col
和col_wrap
参数使用seaborn.lmplot
进行绘图,以获得2 x 3的绘图网格。- 显示的图的默认值为
ci=95
(回归估计的置信区间)。
- 显示的图的默认值为
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = {'MSSubClass': [10,12,11],'LotFrontage': [23,20,31],'OverallQual': [6,2,3],'OverallCond': [43,46,13],'YearBuilt': [8,8,8],'SalePrice': [12,19,24]}
df = pd.DataFrame(data)
# display(df)
MSSubClass LotFrontage OverallQual OverallCond YearBuilt SalePrice
0 10 23 6 43 8 12
1 12 20 2 46 8 19
2 11 31 3 13 8 24
# set SalePrice as the index
df.set_index('SalePrice',inplace=True,drop=False)
# convert the dataframe to a long (tidy) form
dfl = df.stack().reset_index().rename(columns={'level_1': 'cats',0: 'vals'})
# display(dfl)
SalePrice cats vals
0 12 MSSubClass 10
1 12 LotFrontage 23
2 12 OverallQual 6
3 12 OverallCond 43
4 12 YearBuilt 8
5 12 SalePrice 12
6 19 MSSubClass 12
7 19 LotFrontage 20
8 19 OverallQual 2
9 19 OverallCond 46
10 19 YearBuilt 8
11 19 SalePrice 19
12 24 MSSubClass 11
13 24 LotFrontage 31
14 24 OverallQual 3
15 24 OverallCond 13
16 24 YearBuilt 8
17 24 SalePrice 24
# plot the data
sns.lmplot(x='vals',y='SalePrice',col='cats',col_wrap=3,data=dfl,height=4)
plt.xlim(0,50)
plt.xticks(range(0,55,5))
plt.show()
,
决定是这样。图表的类型刚刚出现))
fig,ax = plt.subplots(round(len(qualitative)/5),5,figsize=(40,100))
for var,subplot in zip(qualitative,ax.flatten()):
sns.set(font_scale = 1.5)
sns.regplot(x = var,y = 'SalePrice',data = trainAnaliticQ,ax = subplot);