[英]Change legend location and labels in Seaborn scatter plot
I am tying to change the location as well as labels of my legend in Seaborn scatterplot.我想在 Seaborn 散点图中更改我的图例的位置和标签。 Here is my code:
这是我的代码:
ax_total_message_ratio=sns.scatterplot(x='total_messages', y='email_messages_ratio',hue='poi',data=df_new)
ax_total_message_ratio.set_title("Email Messages Ratio vs. Total Messages Across Poi",y=1.12,fontsize=20,fontweight='bold')
ax_total_message_ratio.set_ylabel('Email Messages Ratio')
ax_total_message_ratio.set_xlabel('Total Messages')
ax_total_message_ratio.legend.loc("lower right")
put.show()
But I am getting following error message;
但我收到以下错误消息;
'function' object has no attribute 'loc'
. 'function' object has no attribute 'loc'
。 Can I get some help on how to control legends with Seaborn?我可以获得有关如何使用 Seaborn 控制传奇的帮助吗? Additionally, I also need to replace
0
by No and 1
by Yes in the legend labels.此外,我还需要将图例标签中的
0
替换为 No,将1
替换为 Yes。 Thanks谢谢
You can adjust the position using the loc
keyword in calling legend()
, like您可以在调用
legend()
使用loc
关键字调整位置,例如
ax_total_message_ratio.legend(loc="lower right")
To include custom labels for your markers you can create a custom legend, ie要为标记包含自定义标签,您可以创建自定义图例,即
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
custom = [Line2D([], [], marker='.', color='b', linestyle='None'),
Line2D([], [], marker='.', color='r', linestyle='None')]
fig = plt.figure(figsize=(8,5))
plt.legend(custom, ['Yes', 'No'], loc='lower right')
plt.show()
This will give you这会给你
and should remove the automatically generated legend, leaving only the custom legend.并且应该删除自动生成的图例,只留下自定义图例。
You can use the convenience method get_legend_handles_labels() to get the handles and the text of the labels:您可以使用便捷方法 get_legend_handles_labels() 来获取标签的句柄和文本:
ax=sns.scatterplot(x='total_messages', y='email_messages_ratio',hue='poi',data=df_new)
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, ['Yes', 'No'], loc='lower right')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.