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

Osmond Bishop picture Osmond Bishop · May 30, 2013 · Viewed 46.7k times · Source

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')

Answer

Clement H.  picture Clement H. · Jul 20, 2017

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