0

I am trying to plot some graphs, I want it to look like that, and do it in python.

I tried many things but it looks way uglier than what I want to achieve, any help would be appreciated

enter image description here

0

3 Answers 3

2

You can use add_grid_spec(), add_subplot(), and subgridspec(). (matplotlib.__version__ >= '3.5.0')

# Please Update matplotlib
# !pip install matplotlib --upgrade

import matplotlib.pyplot as plt

fig = plt.figure()
outer = fig.add_gridspec(nrows=1, ncols=2)
ax1 = fig.add_subplot(outer[0, 0])

inner = outer[0, 1].subgridspec(ncols=2, nrows=2, wspace=0.5)
ax2 = inner.subplots()


ax1.plot(range(10), 'r')
ax2[0,0].plot(range(20), 'g')
ax2[0,1].plot(range(10), 'r')
ax2[1,0].plot(range(5), 'b')
ax2[1,1].plot(range(10), 'y')
    
    
fig.tight_layout()
plt.show()

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

this code throws this error: 'GridSpecFromSubplotSpec' object has no attribute 'subplots'
@DemetreDzmanashvili, what is your matplotlib version? check with these two lines : import matplotlib and matplotlib.__version__
the version is 3.2.2.
@DemetreDzmanashvili, update matplotlib with this : !pip install matplotlib --upgrade before run again restart your kernel
2

you can use a grid try to follow this pattern

from matplotlib.gridspec import GridSpec

fig = plt.figure(constrained_layout=True)
gs = GridSpec(4, 4, figure=fig)
ax1 = fig.add_subplot(gs[:, :2])
# identical to ax1 = plt.subplot(gs.new_subplotspec((0, 0), colspan=3))
ax2 = fig.add_subplot(gs[:2, 2])
ax3 = fig.add_subplot(gs[:2, 3])
ax4 = fig.add_subplot(gs[2:, 2])
ax5 = fig.add_subplot(gs[2:, 3])

plt.show()

enter image description here

Comments

2

Perhaps easier than the above (with relatively modern matplotlib):

fig, axs = plt.subplot_mosaic(
    [['A', 'b', 'c'],
     ['A', 'd', 'e']],
    gridspec_kw={'width_ratios':[2, 1, 1]},
    constrained_layout=True
)

Note that when 3.6 gets released, you can just do:

fig, axs = plt.subplot_mosaic(
    [['A', 'b', 'c'],
     ['A', 'd', 'e']],
    width_ratios=[2, 1, 1],
    constrained_layout=True
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.