df = pd.DataFrame(
np.random.randint(10, size=(5, 7)),
columns='x1 x2 x3 x4 x5 x6 my_y'.split()
)
df
x1 x2 x3 x4 x5 x6 my_y
0 0 8 3 2 7 5 8
1 0 6 2 5 8 4 9
2 4 7 1 2 6 4 5
3 8 5 4 0 5 7 4
4 5 6 0 1 8 7 2
選項1
使用從axes
元件scatter
方法。
fig, axes = plt.subplots(2, 3, figsize=(6, 4), sharex=True, sharey=True)
y = df.my_y.values
for i in range(6):
axes[i//3, i%3].scatter(df.iloc[:, i].values, y)
fig.tight_layout()
選項2
使用pandas.DataFrame.plot
fig, axes = plt.subplots(2, 3, figsize=(6, 4), sharex=True, sharey=True)
y = df.my_y.values
for i in range(6):
df.plot(x='x' + str(i+1),
y='my_y',
kind='scatter',
marker='x',
color='black',
ylim=[0, 10],
ax=axes[i//3, i%3])
fig.tight_layout()
迴應評論
沒有sharex=True
fig, axes = plt.subplots(2, 3, figsize=(6, 4), sharey=True)
y = df.my_y.values
for i in range(6):
df.plot(x='x' + str(i+1),
y='my_y',
kind='scatter',
marker='x',
color='black',
ylim=[0, 10],
ax=axes[i//3, i%3])
fig.tight_layout()
會發生什麼,如果你做的'X = [ 'X1', 'X2', 'X3', 'X4',「X5 ','x6']'? –