在python seaborn plot中创建多列图例

在python seaborn plot中创建多列图例,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我正在使用seaborn.distplot(python3),希望每个系列有2个标签 我尝试了一种黑客字符串格式的方法,如下所示: # bigkey and bigcount are longest string lengths of my keys and counts label = '{{:{}s}} - {{:{}d}}'.format(bigkey, bigcount).format(key, counts['sat'][key]) 在文本为固定宽度的控制台中,我得到: (-inf,

我正在使用
seaborn.distplot
(python3),希望每个系列有2个标签

我尝试了一种黑客字符串格式的方法,如下所示:

# bigkey and bigcount are longest string lengths of my keys and counts
label = '{{:{}s}} - {{:{}d}}'.format(bigkey, bigcount).format(key, counts['sat'][key])
在文本为固定宽度的控制台中,我得到:

(-inf, 1)  -   2538
[1, 3)     -   7215
[3, 8)     -  40334
[8, 12)    -  20833
[12, 17)   -   6098
[17, 20)   -    499
[20, inf)  -     87
我假设绘图中使用的字体不是固定宽度的,因此我想知道是否有一种方法可以指定我的图例具有两个对齐列的标签,并且可能使用
元组调用
seaborn.distplot
,以获得
标签
kwarg(或任何可行的方法)

我的图供参考:


看起来不错,但我真的希望每个系列的2个标签以某种方式对齐。

这不是一个好的解决方案,但希望是一个合理的解决方案。关键思想是将图例拆分为3列以进行对齐,使第2列和第3列上的图例控制柄不可见,并将第3列与其右侧对齐

import io

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

x = np.random.randn(100)
s = [["(-inf, 1)", "-", 2538],
     ["[1, 3)", "-", 7215],
     ["[3, 8)", "-", 40334],
     ["[8, 12)", "-", 20833],
     ["[12, 17)", "-", 6098],
     ["[17, 20)", "-", 499],
     ["[20, inf)", "-", 87]]

fig, ax = plt.subplots()
for i in range(len(s)):
    sns.distplot(x - 0.5 * i, ax=ax)

empty = matplotlib.lines.Line2D([0],[0],visible=False)
leg_handles = ax.lines + [empty] * len(s) * 2
leg_labels = np.asarray(s).T.reshape(-1).tolist()
leg = plt.legend(handles=leg_handles, labels=leg_labels, ncol=3, columnspacing=-1)
plt.setp(leg.get_texts()[2 * len(s):], ha='right', position=(40, 0))

plt.show()

图例可以使用固定宽度字体吗?