[英]Selective vertical whitespace in matplotlib legend
I'm trying to create some extra vertical whitespace in a legend produced using matplotlib.pyplot
.我正在尝试在使用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.我有一个 MWE 和它生成的图像,经过编辑以显示我想要额外空白的位置。
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()
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)
You can use \\n
in labels.您可以在标签中使用\\n
。
Note, that the label is centered to the marker, so use 2 \\n
.请注意,标签以标记为中心,因此请使用 2 \\n
。
plt.plot([0,1],[.6,.6],label = "\nC\n")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.