【问题标题】:How to insert the text below subplot in matplotlib?如何在matplotlib的子图下方插入文本?
【发布时间】:2018-02-02 08:32:18
【问题描述】:

我正在使用matplotlib来

#Plot
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(8,8))
gs1 = gridspec.GridSpec(1, 2)
gs1.update(wspace=0.025, hspace=0.05)  # set the spacing between axes.

ax1 = plt.subplot(gs1[0])
ax2 = plt.subplot(gs1[1])
ax1.axis('off')
ax1.set_xlabel('(a)')
ax2.axis('off')
ax2.set_xlabel('(b)')

因为我必须关闭图中的轴,因此我使用了ax1.axis('off')。现在,我想在每个子图下方插入图形描述,例如 (a)、(b)。我使用了xlabel,但由于功能axis('off'),它无法工作。我可以使用.text 函数有其他选项,但它需要已知位置。就我而言,文本必须位于每个子图中的下方并居中。我该如何实现它。谢谢 我的预期结果是

【问题讨论】:

  • 如果设置了axis("off"),xlabel 将从图中移除(连同属于轴的所有其他艺术家)。您有两个选择:(1)保持轴打开并手动删除轴的所有部分,xlabel除外。 (2) 在子图下方添加文本标签。对于这两个选项中的哪一个,您需要帮助?
  • 我认为是第二种选择。你能用代码写吗?如果你也显示第一个选项,我很感谢

标签: python python-2.7 matplotlib


【解决方案1】:

问题是如果设置了axis("off"),则xlabel 为removed from the figure(连同属于轴的所有其他艺术家)。

但是,您可以在轴下方使用一些普通文本标签来模仿 xlabel。

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(8,8))
gs1 = gridspec.GridSpec(1, 2)
gs1.update(wspace=0.025, hspace=0.05)  # set the spacing between axes.

ax1 = plt.subplot(gs1[0])
ax1.imshow([[0,1],[2,1]])
ax2 = plt.subplot(gs1[1])
ax2.imshow([[2,1],[0,1]])

ax1.axis('off')
ax2.axis('off')

ax1.text(0.5,-0.1, "(a) my label", size=12, ha="center", 
         transform=ax1.transAxes)
ax2.text(0.5,-0.1, "(b) my other label", size=12, ha="center", 
         transform=ax2.transAxes)

plt.show()

更改-0.1 将使您在轴和文本之间留出或多或少的空间。

【讨论】:

  • 完美!谢谢
猜你喜欢
  • 2012-04-20
  • 2018-02-26
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多