Reputation: 3328
How do you show a common legend for groups of subplots (not all). For instance, one legend for all subplots in each column.
fig, ax = plt.subplots(nrows=4, ncols=2)
for row in range(0, 4):
for col in range(0, 2):
ax[row][col].legend(loc="upper right")
# This will add a legend for each sub plot.
fig.legend(loc="upper right")
# This will add a legend for whole figure.
# Suggested here: https://stackoverflow.com/a/46921590/947889
Instead what I need is a common legend for each column (or row). This can be useful when plots in different columns are different (e.g., in my case, plots in column one are heatmap and plots in column two are line plot, which obviously should have different legend).
Upvotes: 0
Views: 396
Reputation: 4105
You could do something like this:
fig, ax = plt.subplots(nrows=4, ncols=2)
for row in range(0, 4):
for col in range(0, 2):
if col == 1: # only for the last column, add a legend
ax[row][col].legend(loc="upper right")
Upvotes: 1