python - 如何为图形级函数编辑 seaborn 图例标题和标签

标签 python matplotlib legend seaborn

我使用 Seaborn 和 pandas 数据框 (data) 创建了这个图:

enter image description here

我的代码:

g = sns.lmplot('credibility', 'percentWatched', data=data, hue = 'millennial', markers = ["+", "."], x_jitter = True, y_jitter = True, size=5)
g.set(xlabel = 'Credibility Ranking\n ← Low       High  →', ylabel = 'Percent of Video Watched [%]')

您可能会注意到该图的图例标题只是变量名称('millennial'),而图例项是变量的值(0、1)。如何编辑图例的标题和标签?理想情况下,传说的标题是“一代”,标签是“千禧一代”和“老一代”

最佳答案

我花了一些时间阅读以上内容。这就是我的答案:

import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")

g = sns.lmplot(
    x="total_bill", 
    y="tip", 
    hue="smoker", 
    data=tips,  
    legend=False
)

plt.legend(title='Smoker', loc='upper left', labels=['Hell Yeh', 'Nah Bruh'])
plt.show(g)

更多参数请引用:matplotlib.pyplot.legend

enter image description here

关于python - 如何为图形级函数编辑 seaborn 图例标题和标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45201514/

相关文章:

python - Jupyter 中存在 MemoryError,但 Python 中没有

python - Matplotlib:更改数学字体大小

r - 在主标题上方放置图例

python - matplotlib imshow 为每种颜色添加标签并将它们放入图例中

python - Dataframe 计算给出 AttributeError : float object has no attribute mean

python - Pandas 在一台计算机上抛出 ParserError 但在另一台计算机上则不会

python - pygame Sprite 似乎拉伸(stretch)/增长而不是沿 x 轴移动

python - 获取 Matplotlib 表格的单元格宽度以覆盖文本和周围的图形

python - Matplotlib/ basemap : Plot a globe in the center of a plot

java - 如何限制图例大小并使其可以用饼图滚动?和 javafx 布局