When just running a plt.legend() the legend appears well, but when sorting it it is…
- User
When just running a plt.legend() the legend appears well, but when sorting it it is empty, why?
Code: def sort_legend(): ax = plt.gca() # Create the legend handles, labels = ax.get_legend_handles_labels() # Sort legend entries sorted_labels_handles = sorted( zip(labels, handles), key=lambda x: x[0]) sorted_labels, sorted_handles = zip(*sorted_labels_handles) return sorted_labels, sorted_handles
sorted_labels, sorted_handles = sort_legend() plt.legend(sorted_labels, sorted_handles, loc='center left', bbox_to_anchor=(1, 0.5))
- Assistant
The issue you're encountering is due to the order in which you're passing the
handlesandlabelsto theplt.legend()function. In Matplotlib, thelegend()function expects the first argument to be a list ofhandles(the plot elements), and the second argument to be the correspondinglabels(the text you want in the legend).In your code, you've accidentally swapped the
handlesandlabelswhen callingplt.legend(). Here's the correction:plt.legend(sorted_handles, sorted_labels, loc='center left', bbox_to_anchor=(1, 0.5))Explanation:
-
Original Code:
plt.legend(sorted_labels, sorted_handles, loc='center left', bbox_to_anchor=(1, 0.5))Here,
sorted_labelsis passed as the first argument, which should besorted_handles. This leads to the legend being empty or not displaying the expected information. -
Corrected Code:
plt.legend(sorted_handles, sorted_labels, loc='center left', bbox_to_anchor=(1, 0.5))By swapping the arguments, you ensure that
plt.legend()receives the correct inputs.
Additional Tips:
-
Understanding
get_legend_handles_labels():This function returns two lists:
handles: A list of artists (lines, patches) that should be in the legend.labels: A list of corresponding labels.
-
Sorting the Legend:
You correctly zipped and sorted the
labelsandhandles, but when unpacking, make sure to maintain their order when passing them back tolegend(). -
Function Signature:
According to the Matplotlib documentation, the function signature is:
legend(handles, labels, **kwargs)handles: Sequence of.Artist(lines, patches) objects.labels: Sequence of strings.
Final Corrected Code with Swapped Arguments:
def sort_legend(): ax = plt.gca() # Create the legend handles, labels = ax.get_legend_handles_labels() # Sort legend entries sorted_labels_handles = sorted(zip(labels, handles), key=lambda x: x[0]) sorted_labels, sorted_handles = zip(*sorted_labels_handles) return sorted_labels, sorted_handles sorted_labels, sorted_handles = sort_legend() plt.legend(sorted_handles, sorted_labels, # Swap arguments here loc='center left', bbox_to_anchor=(1, 0.5))Conclusion:
Swapping the
sorted_handlesandsorted_labelswhen callingplt.legend()should fix the issue and display the sorted legend as expected.References:
I hope this helps you resolve the issue!
Because the
handlesandlabelsare swapped inplt.legend; swapping them will fix it:plt.legend(sorted_handles, sorted_labels, loc='center left', bbox_to_anchor=(1, 0.5)) -