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

I am trying to make a plot where I just want some distance between the first and second set of plots. B

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([])

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744194124a4562571.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信