最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Plot with 8 subplots where 4x4 with horizontal space - Stack Overflow

programmeradmin5浏览0评论

I am trying to make a plot where I just want some distance between the first and second set of plots. Basically when you plot below, you can see the y-axis values off ax5 and ax7 there that is where I need a space from the first set. When I chance wspace-hspace below it just puts the distance between all of them. Can't really figure out how to do this, any help is appreciated.

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(14, 12))

gs = fig.add_gridspec(2, 4, hspace=0, wspace=0) 

ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, 0])
ax4 = fig.add_subplot(gs[1, 1])

ax5 = fig.add_subplot(gs[0, 2])
ax6 = fig.add_subplot(gs[0, 3])
ax7 = fig.add_subplot(gs[1, 2])
ax8 = fig.add_subplot(gs[1, 3])


for ax in [ax1, ax2, ax3, ax4]:
    ax.plot([0, 1], [0, 1])  

for ax in [ax5, ax6, ax7, ax8]:
    ax.plot([0, 1], [1, 0])  

axes_to_modify = [ax2, ax4, ax6, ax8]

for ax in axes_to_modify:
    ax.set_xlabel('')
    ax.set_ylabel('')
    ax.set_xticklabels([])
    ax.set_yticklabels([])

plt.show()

I am trying to make a plot where I just want some distance between the first and second set of plots. Basically when you plot below, you can see the y-axis values off ax5 and ax7 there that is where I need a space from the first set. When I chance wspace-hspace below it just puts the distance between all of them. Can't really figure out how to do this, any help is appreciated.

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(14, 12))

gs = fig.add_gridspec(2, 4, hspace=0, wspace=0) 

ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, 0])
ax4 = fig.add_subplot(gs[1, 1])

ax5 = fig.add_subplot(gs[0, 2])
ax6 = fig.add_subplot(gs[0, 3])
ax7 = fig.add_subplot(gs[1, 2])
ax8 = fig.add_subplot(gs[1, 3])


for ax in [ax1, ax2, ax3, ax4]:
    ax.plot([0, 1], [0, 1])  

for ax in [ax5, ax6, ax7, ax8]:
    ax.plot([0, 1], [1, 0])  

axes_to_modify = [ax2, ax4, ax6, ax8]

for ax in axes_to_modify:
    ax.set_xlabel('')
    ax.set_ylabel('')
    ax.set_xticklabels([])
    ax.set_yticklabels([])

plt.show()
Share Improve this question asked Mar 25 at 13:05 James LaneJames Lane 4512 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

If I were doing this, I'd use subfigures, and add an empty subfigure between the two figures on either side. Note however, that I've adjusted the x and y limits to not have ticks on the very edges, otherwise constrained layout will make space between the axes for them.

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(10, 5), layout='constrained')
fig.get_layout_engine().set(hspace=0.0, wspace=0.0, h_pad=0.0, w_pad=0.0)
sfigs = fig.subfigures(1, 3, width_ratios=[1, 0.1, 1])

for sfig in sfigs[[0, 2]]:

    axs = sfig.subplots(2, 2)
    for ax in axs.flat:
        ax.set_xlim(0.001, 1-0.001)
        ax.set_ylim(0.001, 1-0.001)
        ax.tick_params(axis='both', direction='in')
        ax.label_outer()

plt.show()

Note that you could also do the same thing by using a 2x5 grid and just making the third column invisible.

You can follow something like the example here, i.e., create two GridSpecs and set the right extent of the first grid and left extent of the second grid within the figure to leave some space in between the two. E.g.,

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(14, 12))

# gap to leave (value between 0 and 1)
space = 0.05

# first grid spec for left hand plot (set "right" boundary position)
gs1 = fig.add_gridspec(2, 2, hspace=0, wspace=0, right=0.5 - space/2) 

ax1 = fig.add_subplot(gs1[0, 0])
ax2 = fig.add_subplot(gs1[0, 1])
ax3 = fig.add_subplot(gs1[1, 0])
ax4 = fig.add_subplot(gs1[1, 1])

# second grid spec for left hand plot (set "left" boundary position)
gs2 = fig.add_gridspec(2, 2, hspace=0, wspace=0, left=0.5 + space/2) 

ax5 = fig.add_subplot(gs2[0, 0])
ax6 = fig.add_subplot(gs2[0, 1])
ax7 = fig.add_subplot(gs2[1, 0])
ax8 = fig.add_subplot(gs2[1, 1])


for ax in [ax1, ax2, ax3, ax4]:
    ax.plot([0, 1], [0, 1])  

for ax in [ax5, ax6, ax7, ax8]:
    ax.plot([0, 1], [1, 0])  

axes_to_modify = [ax2, ax4, ax6, ax8]

for ax in axes_to_modify:
    ax.set_xlabel('')
    ax.set_ylabel('')
    ax.set_xticklabels([])
    ax.set_yticklabels([])

发布评论

评论列表(0)

  1. 暂无评论