Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Title for matplotlib legend

I know it seems fairly redundant to have a title for a legend, but is it possible using matplotlib?

Here's a snippet of the code I have:

import matplotlib.patches as mpatches import matplotlib.pyplot as plt  one = mpatches.Patch(facecolor='#f3f300', label='label1', linewidth = 0.5, edgecolor = 'black') two = mpatches.Patch(facecolor='#ff9700', label = 'label2', linewidth = 0.5, edgecolor = 'black') three = mpatches.Patch(facecolor='#ff0000', label = 'label3', linewidth = 0.5, edgecolor = 'black')  legend = plt.legend(handles=[one, two, three], loc = 4, fontsize = 'small', fancybox = True)  frame = legend.get_frame() #sets up for color, edge, and transparency frame.set_facecolor('#b4aeae') #color of legend frame.set_edgecolor('black') #edge color of legend frame.set_alpha(1) #deals with transparency plt.show() 

I would want the title of the legend above label1. For reference, this is the output: Showing legend

like image 819
Brandon Molyneaux Avatar asked Jun 18 '17 22:06

Brandon Molyneaux


People also ask

How do I add a title to my legend?

In case you have an already created legend, you can modify its title with set_title() . For the first answer: legend = plt. legend(handles=[one, two, three], loc=4, fontsize='small', fancybox=True) legend.

How do I change the legend title size in Matplotlib?

To set a legend with title, we use legend() method with labels and title arguments. Then we get the legend title, by using the get_title() function. To change the font size of legend's title, we use set_fontsize() method and set it to x-large. To add a title to the plot, we use title() method.

How to draw legend using matplotlib in Python?

Normally plot the data. Display plot. In this example, we will draw different lines with the help of matplotlib and Use the title argument to plt.legend () to specify the legend title. In this example, we will draw a Bar Graph with the help of matplotlib and Use the title argument to plt.legend () to specify the legend title.

What is the difference between title and labels in Matplotlib?

Similarly, title in Matplotlib is a text area at the top of the Graph which shows the context of the graph. Labels is the information that is displayed on the legend, without the labels legends would be empty. Let’s create a graph of multiple data sets and see how to differentiate between them:

How to get the legend of a figure in Python?

For the first answer: legend = plt.legend (handles= [one, two, three], loc=4, fontsize='small', fancybox=True) legend.set_title ("title") # plt.gca ().get_legend ().set_title () if you didn't store the # legend in an object or you're loading a saved figure.

How do I make a legend for existing plot elements?

Labeling existing plot elements To make a legend for lines which already exist on the Axes (via plot for instance), simply call this function with an iterable of strings, one for each legend item. For example:


1 Answers

Add the title parameter to the this line:

legend = plt.legend(handles=[one, two, three], title="title",                     loc=4, fontsize='small', fancybox=True) 

See also the official docs for the legend constructor.

like image 100
Alper Fırat Kaya Avatar answered Sep 17 '22 19:09

Alper Fırat Kaya