如何为 figure-level 函数编辑 seaborn 图例标题和标签
How to edit a seaborn legend title and labels for figure-level functions
我使用 Seaborn 和 pandas 数据框 (data
) 创建了这个图:
我的代码:
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)。如何编辑图例的标题和标签?理想情况下,图例的标题为 'Generation',标签为“千禧一代”和“上一代”
- 如果
legend_out
设置为 True
则图例可通过 g._legend
属性 获得,并且它是图形的一部分。 Seaborn 图例是标准的 matplotlib 图例对象。因此您可以更改图例文本。
- 测试于
python 3.8.11
、matplotlib 3.4.3
、seaborn 0.11.2
import seaborn as sns
# load the tips dataset
tips = sns.load_dataset("tips")
# plot
g = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, markers=["o", "x"], facet_kws={'legend_out': True})
# title
new_title = 'My title'
g._legend.set_title(new_title)
# replace labels
new_labels = ['label 1', 'label 2']
for t, l in zip(g._legend.texts, new_labels):
t.set_text(l)
legend_out
设置为False
的另一种情况。您必须定义哪些轴具有图例(在下面的示例中,这是轴号 0):
g = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, markers=["o", "x"], facet_kws={'legend_out': False})
# check axes and find which is have legend
leg = g.axes.flat[0].get_legend()
new_title = 'My title'
leg.set_title(new_title)
new_labels = ['label 1', 'label 2']
for t, l in zip(leg.texts, new_labels):
t.set_text(l)
此外,您可以结合这两种情况并使用此代码:
g = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, markers=["o", "x"], facet_kws={'legend_out': True})
# check axes and find which is have legend
for ax in g.axes.flat:
leg = g.axes.flat[0].get_legend()
if not leg is None: break
# or legend may be on a figure
if leg is None: leg = g._legend
# change legend texts
new_title = 'My title'
leg.set_title(new_title)
new_labels = ['label 1', 'label 2']
for t, l in zip(leg.texts, new_labels):
t.set_text(l)
此代码适用于任何基于 Grid
class.
的 seaborn 图
我花了一些时间阅读上面的内容。这就是我的答案:
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
我使用 Seaborn 和 pandas 数据框 (data
) 创建了这个图:
我的代码:
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)。如何编辑图例的标题和标签?理想情况下,图例的标题为 'Generation',标签为“千禧一代”和“上一代”
- 如果
legend_out
设置为True
则图例可通过g._legend
属性 获得,并且它是图形的一部分。 Seaborn 图例是标准的 matplotlib 图例对象。因此您可以更改图例文本。 - 测试于
python 3.8.11
、matplotlib 3.4.3
、seaborn 0.11.2
import seaborn as sns
# load the tips dataset
tips = sns.load_dataset("tips")
# plot
g = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, markers=["o", "x"], facet_kws={'legend_out': True})
# title
new_title = 'My title'
g._legend.set_title(new_title)
# replace labels
new_labels = ['label 1', 'label 2']
for t, l in zip(g._legend.texts, new_labels):
t.set_text(l)
legend_out
设置为False
的另一种情况。您必须定义哪些轴具有图例(在下面的示例中,这是轴号 0):
g = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, markers=["o", "x"], facet_kws={'legend_out': False})
# check axes and find which is have legend
leg = g.axes.flat[0].get_legend()
new_title = 'My title'
leg.set_title(new_title)
new_labels = ['label 1', 'label 2']
for t, l in zip(leg.texts, new_labels):
t.set_text(l)
此外,您可以结合这两种情况并使用此代码:
g = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, markers=["o", "x"], facet_kws={'legend_out': True})
# check axes and find which is have legend
for ax in g.axes.flat:
leg = g.axes.flat[0].get_legend()
if not leg is None: break
# or legend may be on a figure
if leg is None: leg = g._legend
# change legend texts
new_title = 'My title'
leg.set_title(new_title)
new_labels = ['label 1', 'label 2']
for t, l in zip(leg.texts, new_labels):
t.set_text(l)
此代码适用于任何基于 Grid
class.
我花了一些时间阅读上面的内容。这就是我的答案:
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