“How can I create and customize plot legends in Pandas?” 2

“How can I create and customize plot legends in Pandas?”

Creating and customizing plot legends in Pandas allows users to easily display and label the various elements of their plots. This feature allows for better understanding and interpretation of data by providing a clear representation of the different variables. With Pandas, users can create legends by specifying the labels and colors for each element in their plot, as well as customize the location and appearance of the legend. This allows for a more visually appealing and informative representation of data in Pandas plots.

Pandas: Create and Customize Plot Legends


You can use the following basic syntax to add a legend to a plot in pandas:

plt.legend(['A', 'B', 'C', 'D'], loc='center left', title='Legend Title')

The following example shows how to use this syntax in practice.

Example: Create and Customize Plot Legend in Pandas

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'A':7, 'B':12, 'C':15, 'D':17}, index=['Values'])

We can use the following syntax to create a bar chart to visualize the values in the DataFrame and add a legend with custom labels:

import matplotlib.pyplotas plt

#create bar chart
df.plot(kind='bar')

#add legend to bar chart
plt.legend(['A Label', 'B Label', 'C Label', 'D Label'])

We can also use the loc argument and the title argument to modify the location and the title of the legend:

import matplotlib.pyplotas plt

#create bar chart
df.plot(kind='bar')

#add custom legend to bar chart
plt.legend(['A Label', 'B Label', 'C Label', 'D Label'],
            loc='upper left', title='Labels')

Lastly, we can use the size argument to modify the font size in the legend:

import matplotlib.pyplotas plt

#create bar chart
df.plot(kind='bar')

#add custom legend to bar chart
plt.legend(['A Label', 'B Label', 'C Label', 'D Label'], prop={'size': 20})

Notice that the font size in the legend is much larger now.

Additional Resources

Cite this article

stats writer (2024). “How can I create and customize plot legends in Pandas?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-and-customize-plot-legends-in-pandas/

stats writer. "“How can I create and customize plot legends in Pandas?”." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-and-customize-plot-legends-in-pandas/.

stats writer. "“How can I create and customize plot legends in Pandas?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-and-customize-plot-legends-in-pandas/.

stats writer (2024) '“How can I create and customize plot legends in Pandas?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-and-customize-plot-legends-in-pandas/.

[1] stats writer, "“How can I create and customize plot legends in Pandas?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. “How can I create and customize plot legends in Pandas?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top