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

问题描述 投票:0回答:1

我刚刚使用以下代码绘制了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的小节,没有标题。

我想要的是两个子图,两个标题并排,如所述。想知道我做错了什么吗?

python matplotlib subplot
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.