# dataframe with 8 columns using pandas dictionary method:
df = DataFrame({'x1':[10.,8,13,9,11,14,6,4,12,7,5],
'y1':[8.04,6.95,7.58,8.81,8.33,9.96,7.24,4.26,10.84,4.82,5.68],
'x2':[10.,8,13,9,11,14,6,4,12,7,5],
'y2':[9.14,8.14,8.74,8.77,9.26,8.1,6.13,3.1,9.13,7.26,4.74],
'x3':[10.,8,13,9,11,14,6,4,12,7,5],
'y3':[7.46,6.77,12.74,7.11,7.81,8.84,6.08,5.39,8.15,6.42,5.73],
'x4':[8.,8,8,8,8,8,8,19,8,8,8],
'y4':[6.58,5.76,7.71,8.84,8.47,7.04,5.25,12.5,5.56,7.91,6.89]})
# creating subplots:
fig, axes = plt.subplots(2, 2)
df.plot(x='x1', y='y1', ax=axes[0,0], kind='scatter')
df.plot(x='x2', y='y2', ax=axes[0,1], kind='scatter')
df.plot(x='x3', y='y3', ax=axes[1,0], kind='scatter')
df.plot(x='x4', y='y4', ax=axes[1,1], kind='scatter')
此外,我很高兴看到pandas
或matplotlib
中是否存在更好的子绘图方法,以获得与我相同的结果,但效率更高。
答案 0 :(得分:2)
您可以使用pd.wide_to_long重塑数据框,并在pandas图中使用subplot参数。
df1 = df.reset_index()
df_out = pd.wide_to_long(df1,['x','y'],'index','values',sep='',suffix='.')\
.set_index(['x'],append=True)\
.unstack(1)\
.reset_index('index',drop=True)
df_out.plot(subplots=True, marker='o', linestyle='none', layout=(2,2));
输出:
答案 1 :(得分:1)