0

For sake of comparison I would like to create a figure containing 2x2 subplots in matplotlib using something like:

fig = plt.figure(figsize=(15,15))
ax1 = fig.add_subplot(2, 2, 1)
ax1.plot(x1,y1)
ax2 = fig.add_subplot(2, 2, 2)
ax2.plot(x2,y2)
ax3 = fig.add_subplot(2, 2, 3)
ax3.plot(x3,y3)
ax4 = fig.add_subplot(2, 2, 4)
ax4.plot(x4,y4)`enter code here`
fig.tight_layout(pad=3.0)

However, lets assume the data which is supposed to be plotted in subplot(2,2,2) needs a discontinued x-axis. All methods I found so far to plot data with a discontinued x-axis rely on splitting the data up and plot it into different subplots, as explained in this accepted stackoverflow answer:

https://stackoverflow.com/questions/32185411/break-in-x-axis-of-matplotlib

Thus, Im looking for a way to display the plot with a discontinued x-axis in a 2x2 subplot as subplot(2,2,2) along with 3 axes containing no discontinued axis.

The idea is illustrated in the following sketch: sketch subsubplot in a subplot

Any suggestions?

redundant
  • 1
  • 2

1 Answers1

0

This is possible using the fig.add_gridspec for the outer axes, and then use subgridspec to create the two sub-Axes.

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8, 8))

outer_grid = fig.add_gridspec(2, 2)

# Add the three "normal" subplots
ax1 = fig.add_subplot(outer_grid[0, 0])
ax2 = fig.add_subplot(outer_grid[1, 0])
ax4 = fig.add_subplot(outer_grid[1, 1])

# Add the two axes to create the "broken" axes
inner_grid = outer_grid[0, 1].subgridspec(ncols=2, nrows=1, wspace=0)
(ax3a, ax3b) = inner_grid.subplots()

plt.show()

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165