Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add a string as a legend item in matplotlib

I am producing some plots in matplotlib and would like to add explanatory text for some of the data. I want to have a string inside my legend as a separate legend item above the '0-10' item. Does anyone know if there is a possible way to do this?

enter image description here

This is the code for my legend:
ax.legend(['0-10','10-100','100-500','500+'],loc='best')

like image 760
Osmond Bishop Avatar asked May 30 '13 01:05

Osmond Bishop


People also ask

Which Matplotlib function adds a legend to a plot?

In the matplotlib library, there's a function called legend() which is used to Place a legend on the axes. The attribute Loc in legend() is used to specify the location of the legend. Default value of loc is loc=”best” (upper left).

Can Matplotlib plot strings?

From matplotlib 2.1 on you can use strings in plotting functions. Note that in order to preserve the order of the input strings on the plot you need to use matplotlib >=2.2.


1 Answers

Alternative solution, kind of dirty but pretty quick.

import pylab as plt  X = range(50) Y = range(50) plt.plot(X, Y, label="Very straight line")  # Create empty plot with blank marker containing the extra label plt.plot([], [], ' ', label="Extra label on the legend")  plt.legend() plt.show() 

enter image description here

like image 132
Clement H. Avatar answered Sep 27 '22 19:09

Clement H.