微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 seaborn 中创造一个人工传奇

如何解决在 seaborn 中创造一个人工传奇

我想在我的情节中添加一个人工图例。这是人为的,因为我没有对我的观察进行分组(见下面的代码)。这意味着我无法用 plt.legend() 函数解决这个问题:它需要分组变量。有什么办法可以处理吗? 我的代码

sns.set(rc={'figure.figsize':(11.7,8.27)})
sns.set_theme(style="white")
ax = sns.Boxplot(data = data.values.tolist(),palette=['white','black'])
ax.set_xticklabels(labels,fontsize=14)
ax.tick_params(labelsize=14) 

和情节看起来像:

enter image description here

我的愿望是添加一个图例(也许它根本不是图例,只是一张图),其中会写一些类似(抱歉尺寸):

enter image description here

解决方法

由于我没有您的数据,因此无法复制您的图表。但是,您可以尝试在末尾添加以下行(在将 matplotlib.pyplot 作为 plt 导入之后)。

plt.legend(['First','Second'])
,

您可以通过 Seaborn 创建的艺术家创建传奇,如下所示:

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

sns.set_theme(style="white")
ax = sns.boxplot(data = np.random.randn(20,20),palette=['white','black'])

handles = ax.artists[:2]
handles[0].set_label("First")
handles[1].set_label("Second")
ax.legend(handles=handles)
plt.show()

seaborn boxplot with custom legend

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。