Python 将注释和图例保留在同一seaborn绘图中

Python 将注释和图例保留在同一seaborn绘图中,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我正在使用seaborn生成一些数字 import seaborn as sns g=sns.JointGrid(...) 最后,我需要添加一个图例并对情节进行注释。我有: ... g.ax_joint.legend(...) ... g.annotate(scipy.stats.spearmanr,fontsize=14) 但是在annotate()之后,图例就不再存在了。如何将两者保持在同一图形中的同一轴接头上?g.annotate将注释信息添加为图例。在已经有图例的绘图中添加新图例将替

我正在使用seaborn生成一些数字

import seaborn as sns
g=sns.JointGrid(...)
最后,我需要添加一个图例并对情节进行注释。我有:

...
g.ax_joint.legend(...)
...
g.annotate(scipy.stats.spearmanr,fontsize=14)

但是在
annotate()
之后,图例就不再存在了。如何将两者保持在同一图形中的同一轴接头上?

g.annotate
将注释信息添加为图例。在已经有图例的绘图中添加新图例将替换旧图例。解决方法是将旧的图例读入绘图

oldlegend = plt.legend(<something>)
newlegend = plt.legend(<something else>)
plt.gca().add_artist(legend)

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats

mydataset=pd.DataFrame(data=np.random.rand(50,2),columns=['a','b'])
g = sns.JointGrid(x=mydataset['a'], y=mydataset['b'])
g=g.plot_marginals(sns.distplot,color='black',
                   kde=True,hist=False,rug=True,bins=20)
g=g.plot_joint(plt.scatter,label='X')        


legend_properties = {'weight':'bold','size':8}
legendMain=g.ax_joint.legend(prop=legend_properties,loc='upper right')


legendSide=g.ax_marg_x.legend(labels=["x"], 
                              prop=legend_properties,loc='upper right')
g.annotate(scipy.stats.spearmanr,fontsize=14, loc=4)
g.ax_joint.add_artist(legendMain)
plt.show()