-1

This is my code to create two subplots within another subplots and the result. I want to let the left and the right plots share their y-axes.

Result

fig = plt.figure()
fig.suptitle('L1000N1800 AGN HMFs')

outer = fig.add_gridspec(1,2)
axout = outer.subplots()
for i in range (2):
    axout[i].set_xticklabels([])
    axout[i].set_yticklabels([])
    axout[i].axis('off')


inner = outer[0,0].subgridspec(2,1,hspace=0)

ax = inner.subplots(sharex=True)

ax[0].errorbar(hmf_DMO_FIDUCIAL[0],hmf_DMO_FIDUCIAL[1])
ax[0].errorbar(hmf_DMO_FIDUCIAL[0],hmf_DMO_FIDUCIAL[1])
ax[0].errorbar(hmf_DMO_FIDUCIAL[0],STRONGEST_SN_DMO)
ax[0].set_yscale("log")
ax[0].set_xscale("log")

ax[1].errorbar(hmf_DMO_FIDUCIAL[0],hmf_DMO_FIDUCIAL[1])
ax[1].set_yscale("log")

inner = outer[0,1].subgridspec(2,1,hspace=0)
ax = inner.subplots(sharex=True)

ax[0].errorbar(hmf_DMO_FIDUCIAL[0],hmf_DMO_FIDUCIAL[1])
ax[0].errorbar(hmf_DMO_FIDUCIAL[0],hmf_DMO_FIDUCIAL[1])
ax[0].set_yscale("log")
ax[0].set_xscale("log")

ax[1].errorbar(hmf_DMO_FIDUCIAL[0],hmf_DMO_FIDUCIAL[1])
ax[1].set_yscale("log")

fig.tight_layout()
fig.show()

I have tried axout = outer.subplots(sharey=True) but this doesn't work.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • This question is not reproducible without **data**. This question needs a [SSCCE](http://sscce.org/). Please see [How to provide a reproducible dataframe](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. If you don't include an mre, it is likely the question will be downvoted, closed, and deleted. – Trenton McKinney Mar 14 '23 at 15:42
  • `fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=False, sharey=True, tight_layout=True)` seems easier than gridspec – Trenton McKinney Mar 14 '23 at 15:45

2 Answers2

0

You can use GridSpecFromSubplotSpec and some clever "hiding" axes ticks to achieve this:

import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec

fig = plt.figure()
outer = gridspec.GridSpec(1, 2, wspace=0, hspace=0.2)

inner = gridspec.GridSpecFromSubplotSpec(2, 1,
                subplot_spec=outer[0], wspace=0.1, hspace=0)

ax = plt.Subplot(fig, inner[0])
ax.plot(x, y1)
fig.add_subplot(ax)

ax = plt.Subplot(fig, inner[1])
ax.plot(x, y2)
fig.add_subplot(ax)
    
inner = gridspec.GridSpecFromSubplotSpec(2, 1,
                subplot_spec=outer[1], wspace=0.1, hspace=0)

ax = plt.Subplot(fig, inner[0])
ax.plot(x, y3)
ax.set(yticks=[])
fig.add_subplot(ax)

ax = plt.Subplot(fig, inner[1])
ax.plot(x, y4)
ax.set(yticks=[])
fig.add_subplot(ax)

You can replace ax.plot(x, y) with your data and plot style.

P. Shroff
  • 396
  • 3
  • 5
-1

This wont be the same as your code but displays how, I have test implanted having same y-axis: import matplotlib.pyplot as plt

# create two subplots with shared x-axis
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)

# plot data on each subplot
ax1.plot([1, 2, 3], [4, 5, 6])
ax2.plot([1, 2, 3], [2, 4, 6])

# set titles for subplots and overall figure
ax1.set(title='Subplot 1')
ax2.set(title='Subplot 2')
fig.suptitle('Two Subplots with Shared X-Axis')

# display the plot
plt.show()
  • This does not answer the question. – Trenton McKinney Mar 14 '23 at 15:43
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 19 '23 at 22:45