一组子情节的常见图例



如何显示子地块组的通用图例(并非所有(。例如,每列中所有子图都有一个图例。

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

相反,我需要的是每个列(或行(的一个通用图例。当不同列中的绘图不同时,这可能很有用(例如,在我的情况下,第一列中的图形是热图,第二列中的图是折线图,显然应该有不同的图例(。

您可以这样做:

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") 

最新更新