我目前正在我的 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
提交
取消