python - 将标题放在轴图的底部?

标签 python python-3.x matplotlib

标题按 y 垂直移动,但如果 figheight 发生变化,标题与 x 轴的距离/填充不是恒定的:

fig, axs = plt.subplots(1,1, figsize=(15,2.5*1.5))
axs.set_title("mytitle", fontsize=14, y=-0.2, ha='center')

enter image description here

因此随着高度的增加它会移开:

enter image description here

我也试过: axs.set_title("mytitle", fontsize=14, va='bottom', ha='center') 没有重新定位(保持在顶部)和 fontdict 形式,没有变化:

axs.set_title('my_title', fontdict={'fontsize': 20, 'verticalalignment': 'bottom', 'horizo​​ntalalignment': 'center'})

编辑

标题只是我现在使用的文本,它是模块化的:

axs.text(0.5,1,'some_title', va='bottom', ha='center', transform=axs.transAxes, color='k', fontsize=15, bbox={'facecolor':'white', 'edgecolor':'white', 'alpha':1, 'pad':10})

更多信息请访问 text cmds.

最佳答案

您希望标题在距 x 轴绝对坐标的指定距离处。因此,您首先将其定位在 y=0。然后你可以使用一些以点为单位的填充来从那里移动它。

ax.set_title("Hello Title", y=0, pad=-25, verticalalignment="top")

您可以在图形创建中使用 constrained_layout=True 以在调整大小时不裁剪标题。

How to get constant distance between legend and axes even when the figure is resized? 中提出了关于将图例定位在轴下方的类似问题。

关于python - 将标题放在轴图的底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55239332/

相关文章:

python - 如何显示来自谷歌帐户的图像: django-allauth

python - 通过 Python 仅返回 Google 搜索结果的数量

python - 循环遍历Python字典总是产生不同的结果

macos - 如何在Mac上安装nose for python 3

python - matplotlib:在文本框中使用 LaTeX 时覆盖字符串中的下划线

python - 在Python中调整base64图像的大小

python - 使用 Bottle 捕获 mako 运行时错误

python - Python 中最近更新的文件列表

python - 如何创建具有精确单位和测量值的图

python - 当 D 列为真时,如何告诉 matplotlib.pyplot 使用 A 列的值制作散点图?