Search code examples
pythonmatplotliblegendlegend-properties

Python - Matplotlib: Define Individual Legend Entries


When I plot the following:

plt.plot([0,1],[0,1],'g',label='Solid')
plt.plot([0,1],[.5,1],'b',label='Solid')
plt.plot([0,1],[1,0],'g--',label='Dashed')
plt.plot([0,1],[.5,0],'b--',label='Dashed')
plt.legend()

I get this image:

enter image description here

For me, this is too much legend text. Does anyone know, how I can join the solid blue and green line and the dashed blue and green line to reduce the legend to two entries with a green/blue (preferably the one on top of the other) line and the corresponding text? Thanks for your help


Solution

  • As an alternative have a look at the solution in this post: Single legend item with two lines

    A bit more complicated though