简体   繁体   中英

Subsubplot of subplot in a matplotlib figure

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?

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM