I would like to create a plot that consists of three subplots, where the upper left plot has the same width as the lower left plot but 1/3 of the height. Besides, I'd also like to plot the legend in the upper right area from the lower left plot. Is this even possible?
fig, ax = plt.subplots(2, figsize = (16,9))
ax1 = plt.subplot2grid((2,3), (1,0), colspan=2)
ax2 = plt.subplot2grid((2,3), (1,2), colspan=1)
ax3 = plt.subplot2grid((2,3), (0,0), colspan=2)
fig.suptitle('Title')
fig.tight_layout()
CodePudding user response:
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import numpy as np
x = np.linspace(0, 2*np.pi)
y1 = np.cos(x)
y2 = np.sin(x)
fig = plt.figure()
gs = GridSpec(2, 2, width_ratios=[2, 1], height_ratios=[1, 3])
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1])
ax3 = fig.add_subplot(gs[2])
ax4 = fig.add_subplot(gs[3])
ax3.plot(x, y1, label="cos")
ax3.plot(x, y2, label="sin")
handles, labels = ax3.get_legend_handles_labels()
# hide axis on the top left subplot
ax2.axis("off")
# adding two legends
legend1 = ax2.legend([handles[0]], [labels[0]], loc="upper left")
legend2 = ax2.legend([handles[1]], [labels[1]], loc="lower right")
ax2.add_artist(legend1)
plt.tight_layout()