我目前正在我的 iPython 笔记本中关注 analyticsvidhya.com 上的初级贷款预测分类问题。我在 Jupyter 上使用内联 Pylab。到目前为止,我们已经编写了一个数据透视表和条形图。但是当我尝试绘制 2 个条形图时,我得到 3 个条形图,其中一个为空白。# pivot tabletemp1 = 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)# bar graphsimport matplotlib.pyplot as pltfig = plt.figure(figsize=(8,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")为什么这不只返回 2 个条形图?当我尝试他们的替代方案时:“或者,这两个图也可以通过将它们组合在堆叠图中来可视化:”temp3.plot(kind='bar', stacked=True, color=['red','blue'], grid=False)它像在他们的示例中一样正确返回一个组合条形图,但之前的 2 个条形图不起作用。谢谢。
1 回答
杨__羊羊
TA贡献1943条经验 获得超7个赞
尝试以下操作:在绘制数据框时传递轴对象
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,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', ax=ax1) # <---- changed
ax2 = fig.add_subplot(122)
temp2.plot(kind = 'bar', ax=ax2) # <---- changed
ax2.set_xlabel('Credit_History')
ax2.set_ylabel('Probability of getting loan')
ax2.set_title("Probability of getting loan by credit history")
添加回答
举报
0/150
提交
取消