subplots in subplots

Solutions on MaxInterview for subplots in subplots by the best coders in the world

showing results for - "subplots in subplots"
Ben
15 Oct 2017
1import matplotlib.pyplot as plt
2import matplotlib.gridspec as gridspec
3
4fig = plt.figure(figsize=(10, 8))
5outer = gridspec.GridSpec(2, 2, wspace=0.2, hspace=0.2)
6
7for i in range(4):
8    inner = gridspec.GridSpecFromSubplotSpec(2, 1,
9                    subplot_spec=outer[i], wspace=0.1, hspace=0.1)
10
11    for j in range(2):
12        ax = plt.Subplot(fig, inner[j])
13        t = ax.text(0.5,0.5, 'outer=%d, inner=%d' % (i, j))
14        t.set_ha('center')
15        ax.set_xticks([])
16        ax.set_yticks([])
17        fig.add_subplot(ax)
18
19fig.show()
20