python - 子图边距以适合 matplotlib 中的标题和标签
问题描述
我已经阅读了大量关于类似问题的答案,但没有任何帮助。
我只想增加子图与顶部、左侧和底部边缘之间的边距,以适应fig.suptitle
常见的 xlabel 和 ylabel。
import matplotlib.pyplot as plt
import random
#from matplotlib import rcParams
#rcParams['axes.titlepad'] = 20 # kind of works but shifts all titles, not only suptitle
def dummy(n):
return random.sample(range(1, 100), n)
data = dict()
for i in range(4):
data["area{}".format(i)] = [dummy(10), dummy(10)]
fig, ax = plt.subplots(2, 2, sharex='all', sharey='all', figsize=(10, 10))
fig.suptitle("Common title", fontsize=22)
ax = ax.reshape(-1)
for i, (area, data) in enumerate(data.items()):
ax[i].set_title(area)
ax[i].scatter(data[0], data[1])
fig.text(0.5, 0.01, 'xlabel', ha='center', fontsize=15)
fig.text(0.01, 0.5, 'ylabel', va='center', fontsize=15, rotation=90)
fig.tight_layout() # Does nothing
fig.subplots_adjust(top=0.85) # Does nothing
fig.show()
Matplotlib 3.0.0 版
蟒蛇 3.6.6
解决方案
有很多方法可以处理这个问题,但我建议使用gridspec_kw
输入来plt.subplots
解决这个问题。的文档gridspec_kw
基本上只是gridspec.GridSpec文档。例如:
fig, ax = plt.subplots(2, 2, sharex='all', sharey='all',
gridspec_kw=dict(left=0.1, right=0.9,
bottom=0.1, top=0.9),
figsize=(10, 10))
, left
, right
... 输入gridspec_kw
指定整个子图组的范围。
这会给你你想要的东西吗?
推荐阅读
- excel - Excel VBA - 减少执行剪切操作的运行时间
- java - 用于嵌套类的 MongoDB POJO 自定义编解码器
- mysql - 从 ERP 系统内部运行脚本时,显示供用户选择的各种可能参数的正确方法
- python - 获取由另一列排序的列的百分位数
- jquery - 在ajax post请求中设置href值
- excel - Excel Shift Chart 数据系列超过 1
- sql - 根据一列中的一系列相同值过滤列中的相同值
- angular - 更新异步管道时如何以角度重新渲染或刷新html部分?
- python - 形状不可用的 OpenCV 框架
- azure - 如何在 Azure Arm 模板 (2019) 中使用粘性暂存槽