Python 如何在matplotlib中并排获得两个子图?

Python 如何在matplotlib中并排获得两个子图?,python,matplotlib,subplot,Python,Matplotlib,Subplot,我刚刚使用以下代码绘制了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')

我刚刚使用以下代码绘制了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")
我得到以下输出

我得到了3个图,即我想要的2个子图,但是第二个图没有标题,如前所述。第三个在两个横杆下面,是没有标题的temp2


我想要的是两个子情节并排的标题。想知道我做错了什么吗?

除非我将
plt.show()
放在创建第二个轴实例和
temp2.plot(kind='bar')
之间,否则我无法准确地再现这一点。如果您没有调用
plt.show()
(或以其他方式清除缓冲区),您应该尝试

temp1.plot(kind='bar',ax=ax1)
temp2.绘图(种类='bar',ax=ax2)

这应该正确使用两个生成的轴实例

除非我将
plt.show()
放在创建第二个轴实例和
temp2.plot(kind='bar')
之间,否则我无法准确地再现此结果。如果您没有调用
plt.show()
(或以其他方式清除缓冲区),您应该尝试

temp1.plot(kind='bar',ax=ax1)
temp2.绘图(种类='bar',ax=ax2)
这应该正确使用两个生成的轴实例

检查。虽然给定的代码用于垂直相邻的子地块,但说明它也可用于水平相邻的子地块。请检查。虽然给定的代码是用于垂直相邻的子地块,但它也可以应用于水平相邻的子地块。@如果此答案解决了您的问题,请不要忘记点击,这样问题将被标记为将来的问题users@Ali如果这个答案解决了你的问题,别忘了打电话,这样,问题就被标记为未来用户的问题