如何编辑海洋传奇标题和图形功能的标签
我已经使用seaborn和pandas dataframe(data
)创建了此图:
我的代码:
import seaborn as sns
g = sns.lmplot('credibility', 'percentWatched', data=data, hue='millennial', markers=["+", "."])
您可能会注意到该情节的传奇标题只是变量名('千年')传说项目是其值(0,1)。如何编辑传奇的标题和标签?理想情况下,传奇标题将是“世代”,标签将是“千禧一代”和“老一辈”。
I've created this plot using Seaborn and a pandas dataframe (data
):
My code:
import seaborn as sns
g = sns.lmplot('credibility', 'percentWatched', data=data, hue='millennial', markers=["+", "."])
You may notice the plot's legend title is simply the variable name ('millennial') and the legend items are its values (0, 1). How can I edit the legend's title and labels? Ideally, the legend title would be 'Generation' and the labels would be "Millennial" and "Older Generations".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
花了我一段时间来通读以上。这是我的答案:
参考更多参数:
Took me a while to read through the above. This was the answer for me:
Reference this for more arguments: matplotlib.pyplot.legend
legend_out
将设置为true
,则可以通过g._legend
属性获得Legend,它是图的一部分。 Seaborn Legend是标准Matplotlib传奇对象。因此,您可以更改传奇文本。python 3.8.11
中测试,matplotlib 3.4.3
,seaborn 0.11.2
如果
legend_out
设置为false
,则另一种情况。您必须定义哪个轴具有传奇(在示例中为轴号0):您可以将两种情况结合起来并使用此代码:
基于。
legend_out
is set toTrue
then legend is available through theg._legend
property and it is a part of a figure. Seaborn legend is standard matplotlib legend object. Therefore you may change legend texts.python 3.8.11
,matplotlib 3.4.3
,seaborn 0.11.2
Another situation if
legend_out
is set toFalse
. You have to define which axes has a legend (in below example this is axis number 0):Moreover you may combine both situations and use this code:
This code works for any seaborn plot which is based on
Grid
class.为了自定义传说,您需要直接操纵传说对象。 Seaborn中的传说存储在情节对象中。通过此传奇对象,您可以选择标题并修复标签的详细信息,以了解您的数据类型。
您可以做到这一点:
For the customization of the legend, you need to manipulate the legend object directly. The legend in Seaborn is stored within the plot object. Through this legend object, you can select the title and fix the label's details for your type of data.
Here's how you can do this:
以下是编辑海洋人物传说的其他一些方法(从Seaborn 0.13.2开始)。
由于此处的传奇来自列传递给
hue
,这是最简单的方法(以及一种需要最少的工作IMO),如上所述在注释中将其用作hue
变量。另一种方法是隐藏默认的传说,并使用
add_legend()
。@serenity的答案效果很好,但是它不检查标签是否由正确的新标签替换。您可以使用IF-ELSE块进行操作,并确保用正确的标签替换标签。另外,您可以使用
Legend
而不是_legend
。上述所有选项都执行了编辑图例的以下转换。
Here are some other ways to edit the legend of a seaborn figure (as of seaborn 0.13.2).
Since the legend here comes from the column passed to
hue
, the easiest method (and one that requires the least work imo), as mentioned in comments, is to add a column to the dataframe and use it as thehue
variable.Yet another method is to hide the default legend and add a legend with the new labels and title using
add_legend()
.@Serenity's answer works well but it doesn't check if a label is replaced by the correct new label. You can do so using an if-else block and make sure to replace a label with the correct label. Also, you can use
legend
instead of_legend
.All of the above options perform the following transformation where the legend is edited.