Python 3.x 获取Seaborn legend的位置

Python 3.x 获取Seaborn legend的位置,python-3.x,matplotlib,seaborn,legend-properties,Python 3.x,Matplotlib,Seaborn,Legend Properties,我想在我的图例下添加注释。下面是我想要的示例代码: import pandas as pd import numpy as np import matplotlib.pyplot as plt np.random.seed(0) df1 = pd.DataFrame(np.random.normal(size=100)) df2 = pd.DataFrame(np.random.uniform(size=100)) fig,ax=plt.subplots() sns.distplot(df1

我想在我的图例下添加注释。下面是我想要的示例代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
df1 = pd.DataFrame(np.random.normal(size=100))
df2 = pd.DataFrame(np.random.uniform(size=100))

fig,ax=plt.subplots()
sns.distplot(df1,ax=ax,label='foo')
sns.distplot(df2,ax=ax,label='bar')
hardlocy = 0.92
xmargin=0.02
xmin,xmax = ax.get_xlim()
xtxt=xmax-(xmax-xmin)*xmargin

leg = ax.legend()
plt.text(xtxt,hardlocy,"Comment",
         horizontalalignment='right'
        );
结果是:

如您所见,我依赖于手动位置设置,至少对于y轴。我想自动完成

根据和,我试图通过
p=leg.get\u window\u extent()
访问图例特征,但我得到以下错误消息:

AttributeError: 'NoneType' object has no attribute 'points_to_pixels'
(非常类似于)

我运行MacOS Catalina 10.15.4版,几分钟前我成功地执行了
conda更新--all
,但没有任何结果

如何自动放置我的评论?

感谢@JohanC,来自:

人们需要画一个数字才能算出它的图例。因此,这里的工作代码可以是:

np.random.seed(0)
df1 = pd.DataFrame(np.random.normal(size=100))
df2 = pd.DataFrame(np.random.uniform(size=100))

fig,ax=plt.subplots()
sns.distplot(df1,ax=ax,label='foo')
sns.distplot(df2,ax=ax,label='bar')
ymargin=0.05

leg = ax.legend()
fig.canvas.draw()
bbox = leg.get_window_extent()
inv = ax.transData.inverted()

(xloc,yloc)=inv.transform((bbox.x1,bbox.y0))
ymin,ymax = ax.get_ylim()
yloc_margin=yloc-(ymax-ymin)*ymargin

ax.text(xloc,yloc_margin,"Comment",horizontalalignment='right')

@约翰太棒了,干杯,伙计!您知道如何将获得的坐标值转换为可用于
text()
的值吗?有一个带有
annotate()
的示例
annotate()
是带有额外选项的
text()
版本。没关系,我找到了它。