Matplotlib vline label parameter not showing

Lawrence picture Lawrence · Nov 26, 2014 · Viewed 15.4k times · Source

I want to label my vertical lines with matplotlib's .vline command, but for some reason the label parameter doesn't do anything/show anything on the final plot. Does anyone know how to get the label to show?

plt.vlines(x=pah, ymin=0, ymax=0.6, colors='0.75', linestyles='dashed', label='PAHs')

Everything works apart from the label.

Many thanks,

L

Answer

user2304916 picture user2304916 · Nov 26, 2014

The label keyword is displayed in the legend. You need create the legend explicitly to see the label in the plot:

plt.vlines([1,2,3], 0, 1, label='test')
plt.legend()