【问题标题】:How to get two subplots side-by-side in matplotlib?如何在 matplotlib 中并排获得两个子图?
【发布时间】:2020-04-25 15:06:34
【问题描述】:

我刚刚使用以下代码绘制了 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 条。

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

【问题讨论】:

标签: python matplotlib subplot


【解决方案1】:

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

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

这应该正确使用生成的两个坐标区实例。

【讨论】:

  • @Ali 不要忘记点击accept,如果这个答案解决了你的问题,这样问题就会被标记为未来用户
猜你喜欢
  • 2020-10-07
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
  • 1970-01-01
  • 2020-03-03
  • 2022-11-18
  • 1970-01-01
相关资源
最近更新 更多