【发布时间】:2017-05-03 02:07:35
【问题描述】:
我有一个 DataFrame df 代表在 Advertising.csv 中找到的 CSV 数据。
>>> df = pd.read_csv('Advertising.csv', index_col=0)
>>> df.head(5)
TV Radio Newspaper Sales
1 230.1 37.8 69.2 22.1
2 44.5 39.3 45.1 10.4
3 17.2 45.9 69.3 9.3
4 151.5 41.3 58.5 18.5
5 180.8 10.8 58.4 12.9
我想将 DataFrame 中的每一列与各自散点图中的 Sales 列进行对比,即
我设法做到了
import matplotlib.pyplot as plt
f, ax_l = plt.subplots(1, 3, figsize=(14, 4))
for e, col_name in enumerate(df.loc[:, :'Newspaper'].columns):
ax_l[e].scatter(df[col_name], df.Sales, alpha=0.5, color='r')
ax_l[e].set_xlabel(col_name)
ax_l[e].set_ylabel('Sales')
我的问题是,df.plot 中是否有一个结构可以使这项任务更容易进入 Matplotlib 并像我一样循环?
动机
我知道在 R 中,为了达到类似的结果,我会做类似的事情
savePar <- par(mfrow=c(1,3))
col <- adjustcolor( 'red', 0.5 )
with( Advertising,
{ plot( TV , Sales, pch=19, col=col )
plot( Radio , Sales, pch=19, col=col )
plot( Newspaper, Sales, pch=19, col=col )
}
)
诚然,这似乎比我的 Pandas 方法 IMO 干净得多,这让我质疑是否有更直接的方法来以这种方式绘制 DataFrame 的列。
【问题讨论】:
标签: python pandas matplotlib plot