Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selective vertical whitespace in matplotlib legend

I'm trying to create some extra vertical whitespace in a legend produced using matplotlib.pyplot. However, want this extra whitespace to only be between two entries in the legend, while the remaining entries are left untouched. I have a MWE and the image it produces, edited to show where I want the extra whitespace.

import matplotlib.pyplot as plt

plt.plot([0,1],[.2,.2],label = "A")
plt.plot([0,1],[.4,.4],label = "B")
plt.plot([0,1],[.6,.6],label = "C")
plt.plot([0,1],[.8,.8],label = "D")

plt.legend(loc='upper right',labelspacing=.3)
plt.ylim(0,1)

plt.show()

enter image description here

like image 736
NeutronStar Avatar asked Mar 12 '23 21:03

NeutronStar


1 Answers

You probably have to use a Proxy Artist for this. Here is an example:

a, = plt.plot([0,1],[.2,.2],label = "A")
b, = plt.plot([0,1],[.4,.4],label = "B")
c, = plt.plot([0,1],[.6,.6],label = "C")
d, = plt.plot([0,1],[.8,.8],label = "D")

plt.legend([a,b,c,matplotlib.lines.Line2D([],[],linestyle=''),d],['A','B','C','','D'], loc='upper right',labelspacing=.3)
plt.ylim(0,1)

enter image description here

like image 185
Diziet Asahi Avatar answered Mar 27 '23 22:03

Diziet Asahi