簡體   English   中英

如何在matplotlib中並排獲得兩個子圖?

[英]How to get two subplots side-by-side in matplotlib?

我只是使用以下代碼來繪制 2 個子圖:

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')
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")

我得到以下輸出

截圖 1

截圖 2

我得到了 3 個圖,即我想要的 2 個子圖,但是第二個圖為空,標題如上所述。 第三個在這兩個下方,帶有沒有標題的 temp2 條。

我想要的是兩個子圖並排與描述的標題。 想知道我做錯了什么?

除非我將plt.show()放在創建第二個軸實例和temp2.plot(kind='bar')之間,否則我無法完全重現這一點。 如果您沒有調用plt.show() (或以其他方式清除緩沖區),您應該嘗試

temp1.plot(kind='bar', ax=ax1)
temp2.plot(kind='bar', ax=ax2)

這應該正確使用兩個生成的軸實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM