因此,我将按照本教程进行操作:https://www.analyticsvidhya.com/blog/2016/01/complete-tutorial-learn-data-science-python-scratch-2/

我遇到了一个我很难解决的问题。我的目标是并排输出两个子图,左图来自temp1数据帧,右图来自temp2表:

temp1:

Frequency Table for Credit History:
0.0     89
1.0    475
Name: Credit_History, dtype: int64


temp2:

Probility of getting loan for each Credit History class:
                Loan_Status
Credit_History
0.0                0.078652
1.0                0.795789


这是代码块:

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


这是我得到的输出:

python - 为什么要创建第二个图而不是填充第二个子图?-LMLPHP

我原本希望并排有两个子图,但是我右边的第二个子图是空的,而temp2图则在其下的第三个图中输出。

有任何想法吗?我发现matplotlib非常不直观,因此任何建议都值得赞赏!

编辑:我尝试与此代码块进行修补:

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.plot(temp2) # new attempt
ax2.set_xlabel('Credit_History')
ax2.set_ylabel('Probability of getting loan')
ax2.set_title("Probability of getting loan by credit history")


这使我更接近所需的格式,仅我想要条形图而不是折线

python - 为什么要创建第二个图而不是填充第二个子图?-LMLPHP

最佳答案

temp2.plot(kind = 'bar')是熊猫内置图形函数,因此请使用plt.bar(X, y)

像这样 :
(我使用此数据框,例如3行)

print(df)


   sepal length (cm)  petal length (cm)      target
0                5.1                1.4      setosa
1                7.0                4.7  versicolor
2                6.3                6.0   virginica


所以

fig = plt.figure(figsize=(8,4))
ax1 = fig.add_subplot(121)
ax1.bar(df['target'], df['sepal length (cm)'])
ax1.set_xlabel('Credit_History')
ax1.set_ylabel('Count of Applicants')
ax1.set_title("Applicants by Credit_History")

ax2 = fig.add_subplot(122)
ax2.bar(df['target'], df['petal length (cm)'])
ax2.set_xlabel('Credit_History')
ax2.set_ylabel('Probability of getting loan')
ax2.set_title("Probability of getting loan by credit history")

plt.show()




编辑

我找到了你的数据集。问题很简单。
这是由于两种类型之间的差异。

print(type(temp1))
print(type(temp2))


<class 'pandas.core.series.Series'>
<class 'pandas.core.frame.DataFrame'>


因此,更改类型可以解决问题。
如果您将两种类型都更改为数据框,请使用我的第一条评论。
如果将两种类型都更改为Series,请使用此功能。

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') # seiries

ax2 = fig.add_subplot(122)
ax2.set_xlabel('Credit_History')
ax2.set_ylabel('Probability of getting loan')
ax2.set_title("Probability of getting loan by credit history")
temp2['Loan_Status'].plot(kind = 'bar') #  to seiries

plt.show()


python - 为什么要创建第二个图而不是填充第二个子图?-LMLPHP

09-26 21:14