python - 调整单个子图间距
问题描述
fig = plt.figure(figsize=(14, 14))
ax0 = fig.add_subplot(12, 2, (1, 9))
ax1 = fig.add_subplot(12, 2, (2, 10))
ax2 = fig.add_subplot(14, 2, (13, 14))
ax3 = fig.add_subplot(14, 2, (15, 16))
for ax in (ax0, ax1, ax2, ax3):
ax.set_xticks([])
ax.set_yticks([])
fig.subplots_adjust(hspace=.25, wspace=.02)
产量
我可以ax2, ax3
只减少底部两个子图 ( ) 之间的垂直间距吗?
解决方案
这有点 hacky,但是您可以使用每个子图的边界框来将 ax3 的边界框移动到更靠近 ax2 的位置(此处触摸):
fig = plt.figure(figsize=(5, 5)) # Made smaller for the example
ax0 = fig.add_subplot(12, 2, (1, 9))
ax1 = fig.add_subplot(12, 2, (2, 10))
ax2 = fig.add_subplot(14, 2, (13, 14))
ax3 = fig.add_subplot(14, 2, (15, 16))
for ax in (ax0, ax1, ax2, ax3):
ax.set_xticks([])
ax.set_yticks([])
fig.subplots_adjust(hspace=.25, wspace=.02)
# Below is where the m"hack"gic happens
pos_ax3 = ax3.get_position()
pos_ax2 = ax2.get_position()
ywidth = pos_ax3.y1 - pos_ax3.y0
yspace = 0
pos_ax3.y0 = pos_ax2.y0 - ywidth * yspace
pos_ax3.y1 = pos_ax2.y0 - ywidth * (1 + yspace)
ax3.set_position(pos_ax3)
输出:
推荐阅读
- apache-spark - 在 Spark 中动态替换机器学习模型,无需重新启动 Spark-streaming 作业
- mysql - 具有多个 OR 的查询结果不是 OR 的顺序
- mysql - 如果我无法触及插入查询,有没有办法在 sql 中回滚失败的批量插入?也许触发?
- javascript - 为什么我的对象没有被推入不同的数组?
- angular - Angular - 这是编写初始化函数的另一种方式吗?
- java - Java JDBC mysql中的更新查询不起作用
- python - 为什么我可以打印到命令行但不能使用 docker 写入文本文件?
- java - 为什么我自己的 AtomicLong 比 JDK 中提供的要慢?
- javascript - 为什么在条件渲染中解析 Math.mod() 的错误?
- python - 有没有办法在包含字符串的对象数组上使用 numpy 字符串数组函数?