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
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:
import matplotlib and matplotlib.__version__!pip install matplotlib --upgrade before run again restart your kernelyou 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()
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
)