Home > Mobile >  How do I make a SINGLE legend using subplots? _get_legend_handles_labels is not working
How do I make a SINGLE legend using subplots? _get_legend_handles_labels is not working

Time:12-21

I want to make a single legend with corresponding colors for the models that are in the individual plots on the whole subplot.

My current code is as follows:

from matplotlib.legend import _get_legend_handles_labels

colors = ['red', 'blue','darkblue', 'purple', 'orange', 'brown', 'pink', 'darkgreen', 'gray']
models = ['Logistic Regression', 'SVM', 'Decision Tree', 'Random Forest', 'XGBoost', 'ADABoost', 'Gaussian NB', 'KNN', 'MLP']

# English

fig, axs = plt.subplots(4,2, figsize=(25,15))
fig.suptitle('Performance measures for English and Arabic data', fontsize = 25)

axs[0,0].bar(models, en_f1_scores, color=colors)
axs[0,0].set_title("English F1 score", fontsize = 20)

axs[1,0].bar(models, en_acc_scores, color=colors)
axs[1,0].set_title("English accuracy score", fontsize = 20)

axs[2,0].bar(models, en_recall_scores, color=colors)
axs[2,0].set_title("English recall score", fontsize = 20)

axs[3,0].bar(models, en_precision_scores, color=colors)
axs[3,0].set_title("English precision score", fontsize = 20)

# Arabic

axs[0,1].bar(models, ar_f1_scores, color=colors)
axs[0, 1].set_title("Arabic F1 score", fontsize = 20)

axs[1,1].bar(models, ar_accuracy_scores, color=colors)
axs[1,1].set_title("Arabic accuracy score", fontsize = 20)

axs[2,1].bar(models, ar_recall_scores, color=colors)
axs[2,1].set_title("Arabic recall score", fontsize = 20)

axs[3,1].bar(models, ar_precision_scores, color=colors)
axs[3,1].set_title("Arabic precision score", fontsize = 20)

fig.tight_layout(pad=3.0)

And the current output looks like this:

Current outplot subplots

Adding this code:

lines, labels = fig.axes[-1].get_legend_handles_labels()
fig.legend(lines, labels, loc = 'upper center')

Does not do anything, all it shows me is:

<matplotlib.legend.Legend at 0x266574402b0>

Moreover, both lines and labels are empty arrays; []

What can I do to add a legend at the top of the subplot figure? (if it is a horizontal legend, even better!)

Thank you!

CodePudding user response:

First, I would suggest to save all information into lists, so the plot can be made via a large loop. That way, if some detail changes, it only needs to be changed at one spot.

To create a legend, graphical elements that have a "label" will be added automatically. Normally, a complete bar plot only gets one label. By diving into the generated bars, individual labels can be assigned.

The code first creates a dummy legend, so fig.tight_layout() can adapt all the spacings and leave some place for the legend. After calling fig.tight_layout(), the real legend is created. (With the real legend, fig.tight_layout() would try to assign it completely to one subplot, and create a wide gap between the two columns of subplots).

import matplotlib.pyplot as plt
import numpy as np

colors = ['red', 'blue', 'darkblue', 'purple', 'orange', 'brown', 'pink', 'darkgreen', 'gray']
models = ['Logistic Regression', 'SVM', 'Decision Tree', 'Random Forest', 'XGBoost', 'ADABoost', 'Gaussian NB', 'KNN', 'MLP']

titles = ["F1 score", "accuracy score", "recall score", "precision score"]

N = len(models)
en_f1_scores = np.random.rand(N)
en_acc_scores = np.random.rand(N)
en_recall_scores = np.random.rand(N)
en_precision_scores = np.random.rand(N)
en_scores = [en_f1_scores, en_acc_scores, en_recall_scores, en_precision_scores]
ar_f1_scores = np.random.rand(N)
ar_acc_scores = np.random.rand(N)
ar_recall_scores = np.random.rand(N)
ar_precision_scores = np.random.rand(N)
ar_scores = [ar_f1_scores, ar_acc_scores, ar_recall_scores, ar_precision_scores]

fig, axs = plt.subplots(4, 2, figsize=(25, 15), sharex=True, sharey='row')
fig.suptitle('Performance measures for English and Arabic data', fontsize=25)

for axs_row, en_score, ar_score, title in zip(axs, en_scores, ar_scores, titles):
    for language, score, ax in zip(['English', 'Arabic'], [en_score, ar_score], axs_row):
        ax.bar(models, score, color=colors)
        ax.set_title(language   ' '   title, fontsize=20)
        ax.set_xticks([])  # remove the x tick and their labels
        ax.grid(axis='y', ls=':', color='black')  # add some gridlines
        ax.set_axisbelow(True)  # gridlines behind the bars
        for spine in ['top', 'right', 'left']:  # remove part of the surrounding box, as it gets busy with the grid lines
            ax.spines[spine].set_visible(False)
        ax.margins(x=0.01)  # less white space left and right

# the legend is created for each graphical element that has a "label"
for bar, model in zip(axs[0, 0].containers[0], models):
    bar.set_label(model)
# first create a dummy legend, so fig.tight_layout() makes enough space
axs[0, 0].legend(handles=axs[0, 0].containers[0][:1],
                 bbox_to_anchor=(0, 1.12), loc='lower left')
fig.tight_layout(pad=3.0)
# now create the real legend; if fig.tight_layout() were called on this,
#  it would create a large empty space between the columns of subplots
#  as it wants the legend to belong to only one of the subplots
axs[0, 0].legend(handles=axs[0, 0].containers[0], ncol=len(models),
                 bbox_to_anchor=(1.03, 1.12), loc='lower center', fontsize=18)
plt.show()

common legend for 8 subplots

  • Related