如何实现seaborn lmplot以获得包含每个数据帧列的网格化绘图?

2024-09-24 02:20:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集。帮助使用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地块临街总质量总第二年建筑销售价格


    10      23     6.     43    8.    十二,

    12      20     2.     46    8.    十九,

    11      31     3.     13    8.    二十四

您应该在两行三列中获得六个依赖关系图sns.lmplot

销售价格 \|\____        销售价格   我------        销售价格    我------我

      MSSubClass           临街            综合素质

销售价格 \|\I        销售价格   我---\---        销售价格    我——我——|

      总毫秒           年建            销售价格


Tags: 数据图形绘图lenmatplotlibvarsseaborn形式
2条回答
  • 为了绘制数据,必须使用^{}将数据帧从宽格式转换为长格式(整齐)
    • SalePrice必须保持为一列,以针对自身进行绘图,并位于索引中,这可以通过使用^{}并指定drop=False来完成
  • 使用colcol_wrap参数使用^{}绘图,以获得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()

enter image description here

决定这样做。刚刚出现的图表类型)

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);

enter image description here

相关问题 更多 >