Seaborn 的 Legends 和 matplotlib AxesSubplot 之间的交互
Interaction between Legends from Seaborn and matplotlib AxesSubplot
seaborn 0.11.0 和 matplotlib 3.3.3
写下下面的代码,你得到图例 x
为蓝色,y
为橙色。取消注释 plt.show()
之前的最后一行,现在即使我在 sns.kdeplot()
.
中指定了 hue_order
,图例也会反转
# random data for testing
x = np.random.randn(100)
y = np.random.randn(100)
df = pd.DataFrame({"x": x, "y": y})
# long format
df = df.melt()
# plot
ax = sns.kdeplot(x="value", hue="variable",
hue_order=["x", "y"], data=df)
print(ax.get_legend_handles_labels())
print(ax.legend_)
print(type(ax))
# ax.legend(["x", "y"])
plt.show()
还要注意 ax
本身没有句柄也没有图例,打印语句打印:
([], [])
Legend
<class 'matplotlib.axes._subplots.AxesSubplot'>
我不知道如何处理 seaborn 生成的图例,以便我可以根据需要修改它。
我有一个有 10 个轴的图,我想在这些轴上用 sns.kdeplot()
绘图,但图有一个图例。我必须使用辅助函数,我们将 legend=True
设置为 kdeplot()
。这是一个硬约束。
我认为可行的是,我从所有子图中删除了图例,然后手动将它们添加到图中。我认为这应该可行,因为我在 kdeplot()
中指定了 hue_order
,但它不是您从上面的示例中看到的。
我也尝试删除除一个子图之外的所有图例并将其相对于图形放置,但我找不到这样做的方法。
您可以从轴图例创建图形图例,然后删除它们:
import seaborn as sns
import numpy as np
import pandas as pd
# random data for testing
np.random.seed(42)
x = np.random.randn(100)
y = np.random.randn(100)
df = pd.DataFrame({"x": x, "y": y})
# long format
df = df.melt()
# plot
fig,(ax1,ax2) = sns.mpl.pyplot.subplots(2)
sns.kdeplot(x="value", hue="variable",
hue_order=["x", "y"], data=df, ax=ax1)
sns.kdeplot(x="value", hue="variable",
hue_order=["x", "y"], data=df.assign(value=df.value*2), ax=ax2)
fig.legend(ax.get_legend().get_lines(),
[t.get_text() for t in ax.get_legend().get_texts()],
title='variable')
ax1.get_legend().remove()
ax2.get_legend().remove()
seaborn 0.11.0 和 matplotlib 3.3.3
写下下面的代码,你得到图例 x
为蓝色,y
为橙色。取消注释 plt.show()
之前的最后一行,现在即使我在 sns.kdeplot()
.
hue_order
,图例也会反转
# random data for testing
x = np.random.randn(100)
y = np.random.randn(100)
df = pd.DataFrame({"x": x, "y": y})
# long format
df = df.melt()
# plot
ax = sns.kdeplot(x="value", hue="variable",
hue_order=["x", "y"], data=df)
print(ax.get_legend_handles_labels())
print(ax.legend_)
print(type(ax))
# ax.legend(["x", "y"])
plt.show()
还要注意 ax
本身没有句柄也没有图例,打印语句打印:
([], [])
Legend
<class 'matplotlib.axes._subplots.AxesSubplot'>
我不知道如何处理 seaborn 生成的图例,以便我可以根据需要修改它。
我有一个有 10 个轴的图,我想在这些轴上用 sns.kdeplot()
绘图,但图有一个图例。我必须使用辅助函数,我们将 legend=True
设置为 kdeplot()
。这是一个硬约束。
我认为可行的是,我从所有子图中删除了图例,然后手动将它们添加到图中。我认为这应该可行,因为我在 kdeplot()
中指定了 hue_order
,但它不是您从上面的示例中看到的。
我也尝试删除除一个子图之外的所有图例并将其相对于图形放置,但我找不到这样做的方法。
您可以从轴图例创建图形图例,然后删除它们:
import seaborn as sns
import numpy as np
import pandas as pd
# random data for testing
np.random.seed(42)
x = np.random.randn(100)
y = np.random.randn(100)
df = pd.DataFrame({"x": x, "y": y})
# long format
df = df.melt()
# plot
fig,(ax1,ax2) = sns.mpl.pyplot.subplots(2)
sns.kdeplot(x="value", hue="variable",
hue_order=["x", "y"], data=df, ax=ax1)
sns.kdeplot(x="value", hue="variable",
hue_order=["x", "y"], data=df.assign(value=df.value*2), ax=ax2)
fig.legend(ax.get_legend().get_lines(),
[t.get_text() for t in ax.get_legend().get_texts()],
title='variable')
ax1.get_legend().remove()
ax2.get_legend().remove()