亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我在 Python 中得到一個額外的條形圖?

為什么我在 Python 中得到一個額外的條形圖?

慕虎7371278 2021-12-21 15:02:56
我目前正在我的 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")


查看完整回答
反對 回復 2021-12-21
  • 1 回答
  • 0 關注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號