如何在 pandas 的不同位置绘制图表?

How can I plot graph in different position in pandas?

我有这四个数据集,比如 df1。我想将它们打印成像 2*2 这样的散点图。

df1
    Height  time_of_day resolution  clusters
11  3.146094    0.458333    0.594089    0
90  0.191690    0.541667    0.594089    0
99  1.300386    1.666667    0.594089    1
121 3.054903    2.083333    0.594089    0

df2
    Height  time_of_day resolution  clusters
10  3.146094    0.458333    0.594089    0
60  3.191690    0.541667    0.594089    0
87  1.300386    1.666667    0.594089    1
121 3.054903    1.083333    0.594089    0

df3
    Height  time_of_day resolution  clusters
13  3.146094    0.458333    0.594089    0
61  3.191690    0.541667    0.594089    0
86  1.300386    1.666667    0.594089    1
113 4.054903    1.083333    0.594089    0

df4
    Height  time_of_day resolution  clusters
10  3.146094    0.458333    0.594089    0
20  3.191690    0.541667    0.594089    0
37  1.300386    1.666667    0.594089    1
121 3.054903    1.083333    0.594089    0


我试了好几种方法都不行。

dics = [df1,df2,df3,df4]
rows = range(4)

fig, ax = plt.subplots(2,2,squeeze=False,figsize = (20,10))
for x in rows:
    for i,dic in enumerate(dics):
        sns.lmplot(x="time_of_day", y="Height",fit_reg=False,hue="clusters", data=dic[x], height=6, aspect=1.5)

plt.show()

这是散点图的单一代码

sns.lmplot(x="time_of_day", y="Height",fit_reg=False,hue="clusters", data=summer_spike_df, height=6, aspect=1.5)


要打印成2*2的不同散点图结果应该改什么代码? 谢谢

如果您不绘制回归线,那为什么不直接使用 seaborn.scatterplot

您可以使用 zip 函数和 array.ravel 绘图:

fig, axes = plt.subplots(2,2,squeeze=False,figsize = (20,10))

for df, ax in zip(dics, axes.ravel()):
    sns.scatterplot(x="time_of_day", y="Height",hue="clusters", data=df, ax=ax)

plt.show()