【发布时间】:2018-09-06 14:33:42
【问题描述】:
我正在尝试做一个循环,其中每个i 都有一个带有两个图的图形。图之间的差异由二元列给出。例如:
train = pd.DataFrame({'vehicle': ['car', 'truck', 'bus', 'car', 'bus' ],
'sex': ['male','male','male','female','female'],
'income': ['60000', '50000', '65000', '70000', '60000'],
'age': [31,25,67,90,18],
'crash' : [1,0,0,0,1]
})
每个图必须有两个图,它显示了所有变量和crash 列之间的关系,每个列都有两个案例(crash=1 and crash=0)。这是我的代码,但它仍然无法正常工作:
for i in train.columns:
i_1 = train[i][train["crash"] == 1]
i_0 = train[i][train["crash"] == 0]
fig = plt.figure(figsize=(5, 10))
fig.add_subplot(1, 2, 2)
ax1 = fig.add_subplot(1,1,1)
ax2 = fig.add_subplot(1,2,2)
ax1.plot(train[i], i_1)
ax2.plot(train[i], i_0)
plt.show()
所以,图 1 有两个图表。图 1 是具有 crash=1 的车辆的 count,图 2 是具有 crash=0 的车辆的 count。图2,图1是与crash=1发生性关系的count,图2是与crash=0发生性关系的count。依此类推……总共(在本例中)为 4 个数字。
有什么想法吗?
【问题讨论】:
-
有什么问题? “它不起作用”不是问题陈述。
标签: python pandas loops matplotlib plot