我有一个包含变量Credit_History(0或1)和Loan_Status(Y或N)的数据框。 Temp1只显示我标记为0或1的行数.Temp2是我想将Y或N编码为0或1的位置,然后将其平均。
temp1 = df['Credit_History'].value_counts(ascending=True)
temp2 = df.pivot_table(values='Loan_Status',index=
['Credit_History'],aggfunc=lambda x: x.map({'Y':1,'N':0}).mean())
print ('Frequency Table for Credit History:')
print (temp1)
print ('\nProbility of getting loan for each Credit History class:')
print (temp2)
当我绘制时,我期望1行乘2列区域。但它看起来像一个2行乘2列的区域,包含3个图。 Temp2被绘制了2次,但除了轴标题之外,其中一个是空白的。我假设我在创建temp2对象时声明错误... enter image description here
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12,4))
ax1 = fig.add_subplot(121)
ax1.set_xlabel('Credit_History')
ax1.set_ylabel('Count of Applicants')
ax1.set_title('Applicants by Credit_History')
temp1.plot(kind='bar')
ax2 = fig.add_subplot(122)
temp2.plot(kind = 'bar')
ax2.set_xlabel('Credit_History')
ax2.set_ylabel('Probability of getting loan')
ax2.set_title('Probability of getting loan by credit history')
答案 0 :(得分:0)
考虑布置绘图的尺寸,然后将轴分配给pandas图:
fig, axs = plt.subplots(nrows = 1, ncols=2, figsize=(12,4))
temp1.plot(kind='bar', title='Applicants by Credit_History', ax=axs[0])
axs[0].set_xlabel('Credit_History')
axs[0].set_ylabel('Count of Applicants')
temp2.plot(kind = 'bar', title='Probability of getting loan by credit history', ax=axs[1])
axs[1].set_xlabel('Credit_History')
axs[1].set_ylabel('Probability of getting loan')
fig.tight_layout()
plt.show()
plt.clf()
plt.close()
输出 (使用随机数据)