python - matplotlib 中的标题和副标题对齐不一致
问题描述
我正在尝试同时使用两者ax.set_title()
并将plt.suptitle()
标题和副标题合并到图表中,但两者似乎并不共享相同的对齐方式。例如,以下内容:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
cats = ('One', 'Two')
vals = (12, 4)
ax.barh(cats, vals, align='center')
plt.suptitle('Title')
ax.set_title('Title')
plt.show()
给我们以下错位的标题:
我怎样才能让这两个标题正确对齐?我认为这可能与ax.title
对齐轴和plt.suptitle
对齐图形有关,但测试更长的 y 标签似乎不会影响偏移:
fig, ax = plt.subplots()
cats = ('One million tiny engines running at one hundred miles per hour', 'Two')
vals = (12, 4)
ax.barh(cats, vals, align='center')
plt.suptitle('Title')
ax.set_title('Title')
plt.show()
解决方案
matplotlib 将 suptitle 与figure对齐,将title与subplot对齐。您可以使用以下方法手动摇动字幕fig.subplotpars
:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
cats = ('One', 'Two')
vals = (12, 4)
# Mid point of left and right x-positions
mid = (fig.subplotpars.right + fig.subplotpars.left)/2
ax.barh(cats, vals, align='center')
plt.suptitle('Title',x=mid)
ax.set_title('Title')
plt.show()
享受!
推荐阅读
- swift - tvOS 如果用户点击播放/暂停
- html - 如何在不干扰主页等链接的情况下从 URL 路径中删除文件夹名称?
- android - 收到错误未知主机“dl.google.com”。您可能需要在 Gradle.in android 4.02 中调整代理设置
- ffmpeg - ffmpeg concat图像与视频,但输出超长
- react-native - 无法通过运行 react-native run-android 命令启动模拟器
- c# - 启动基地失败!/app/.local-chromium/Linux-706915/chrome-linux/chrome: ... libX11.so.6
- python - 如何查找列表中前两个元素的索引,它们是另一个列表中的任何元素?
- reactjs - Reactjs 输出未显示
- gdb - 是否可以指示 GDB 从目标异步获取变量值?
- sql-server - 将日期时间转换为生肖名称