Table of Contents
The order of items in a Matplotlib legend can be changed by using the “handles” and “labels” parameters. These parameters allow for the manual reordering of the legend items by specifying the desired order of the labels and corresponding handles. Additionally, the “handles” parameter can be used to specify which specific items should be included in the legend and in what order. By manipulating these parameters, the order of items in a Matplotlib legend can be easily adjusted to suit the user’s preferences.
Change Order of Items in Matplotlib Legend
You can use the following chunk of code to change the order of items in a Matplotlib legend:
#get handles and labels handles, labels = plt.gca().get_legend_handles_labels() #specify order of items in legend order = [1,2,0] #add legend to plot plt.legend([handles[idx] for idx in order],[labels[idx] for idx in order])
The following example shows how to use this syntax in practice.
Example: Change Order of Items in Matplotlib Legend
Suppose we create the following line chart in Matplotlib:
import pandas as pd import matplotlib.pyplotas plt #create data df = pd.DataFrame({'points': [11, 17, 16, 18, 22, 25, 26, 24, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4, 8], 'rebounds': [6, 8, 8, 10, 14, 12, 12, 10, 11]}) #add lines to plot plt.plot(df['points'], label='Points', color='green') plt.plot(df['assists'], label='Assists', color='steelblue') plt.plot(df['rebounds'], label='Rebounds', color='purple') #add legend plt.legend()

The items in the legend are placed in the order that we added the lines to the plot.
However, we can use the following syntax to customize the order of the items in the legend:
import pandas as pd import matplotlib.pyplotas plt #create data df = pd.DataFrame({'points': [11, 17, 16, 18, 22, 25, 26, 24, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4, 8], 'rebounds': [6, 8, 8, 10, 14, 12, 12, 10, 11]}) #add lines to plot plt.plot(df['points'], label='Points', color='green') plt.plot(df['assists'], label='Assists', color='steelblue') plt.plot(df['rebounds'], label='Rebounds', color='purple') #get handles and labels handles, labels = plt.gca().get_legend_handles_labels() #specify order of items in legend order = [1,2,0] #add legend to plot plt.legend([handles[idx] for idx in order],[labels[idx] for idx in order])

Note that we specified:
- order = [1, 2, 0]
This means:
- The first item in the legend should be the label that was originally in index position 1 of the old legend (“Assists”)
- The second item in the legend should be the label that was originally in index position 2 of the old legend (“Rebounds”)
- The third item in the legend should be the label that was originally in index position 0 of the old legend (“Points”)
Additional Resources
Cite this article
stats writer (2024). How can the order of items in a Matplotlib legend be changed?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-order-of-items-in-a-matplotlib-legend-be-changed/
stats writer. "How can the order of items in a Matplotlib legend be changed?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-the-order-of-items-in-a-matplotlib-legend-be-changed/.
stats writer. "How can the order of items in a Matplotlib legend be changed?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-order-of-items-in-a-matplotlib-legend-be-changed/.
stats writer (2024) 'How can the order of items in a Matplotlib legend be changed?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-order-of-items-in-a-matplotlib-legend-be-changed/.
[1] stats writer, "How can the order of items in a Matplotlib legend be changed?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can the order of items in a Matplotlib legend be changed?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
