更改图例标签时 Matplotlib 图例颜色会发生变化
Matplotlib Legend colors change when changing legend labels
我不明白我在这里做错了什么。我想将图例中的 0 和 1 更改为“零”和“一”,但不知何故这也会改变图例中的颜色。
这是我得到第一张图片的方式:
sns.scatterplot(ax = axes, data = data_pcoa, x = "Coordinate 0",
y = "Coordinate 1", hue = "Number", palette = ["orange", "blue"])
axes.set(xlabel = "1st PCo", ylabel = "2nd PCo")
axes.legend()
这是我得到第二张图片的方式:
sns.scatterplot(ax = axes, data = data_pcoa, x = "Coordinate 0",
y = "Coordinate 1", hue = "Number", palette = ["orange", "blue"])
axes.set(xlabel = "1st PCo", ylabel = "2nd PCo")
axes.legend(labels = ['zero', 'one'])
如您所见,在第二张图片中,图例标题已更改,但颜色不再与情节相符。
据我了解,您需要在 scatterplot
中自定义标签名称。
这是回答您问题的小例子。
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='My Title', loc='upper left', labels=['Zero', 'One'])
plt.show(g)
另一种方法:
sns.scatterplot(data = tips, x = "total_bill",
y = "tip", hue = "smoker", palette = ["orange", "blue"])
ax = plt.gca()
ax.set(xlabel = "1st PCo", ylabel = "2nd PCo")
ax.legend(labels = ['zero', 'one'])
我通过创建自定义图例解决了这个问题。这不是很优雅,但它完成了工作。不过这很奇怪,因为当我在类似的数据集上使用 axes.legend(labels = ["zero", "one"]
时它确实有效。无论如何,这解决了我的问题(但我很想知道一个更优雅的解决方案):
legend_elements = [Line2D([0], [0], color = 'w', markerfacecolor = 'b', marker = 'o', label='one', markersize=8),
Line2D([0], [0], color = 'w', markerfacecolor = 'orange', marker='o', label='zero', markersize=8)]
这对我有用,而且更干净
handles, labels = ax.get_legend_handles_labels()
labels = ["zero", "one"]
ax.legend(handles, labels)
我不明白我在这里做错了什么。我想将图例中的 0 和 1 更改为“零”和“一”,但不知何故这也会改变图例中的颜色。
这是我得到第一张图片的方式:
sns.scatterplot(ax = axes, data = data_pcoa, x = "Coordinate 0",
y = "Coordinate 1", hue = "Number", palette = ["orange", "blue"])
axes.set(xlabel = "1st PCo", ylabel = "2nd PCo")
axes.legend()
这是我得到第二张图片的方式:
sns.scatterplot(ax = axes, data = data_pcoa, x = "Coordinate 0",
y = "Coordinate 1", hue = "Number", palette = ["orange", "blue"])
axes.set(xlabel = "1st PCo", ylabel = "2nd PCo")
axes.legend(labels = ['zero', 'one'])
如您所见,在第二张图片中,图例标题已更改,但颜色不再与情节相符。
据我了解,您需要在 scatterplot
中自定义标签名称。
这是回答您问题的小例子。
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='My Title', loc='upper left', labels=['Zero', 'One'])
plt.show(g)
另一种方法:
sns.scatterplot(data = tips, x = "total_bill",
y = "tip", hue = "smoker", palette = ["orange", "blue"])
ax = plt.gca()
ax.set(xlabel = "1st PCo", ylabel = "2nd PCo")
ax.legend(labels = ['zero', 'one'])
我通过创建自定义图例解决了这个问题。这不是很优雅,但它完成了工作。不过这很奇怪,因为当我在类似的数据集上使用 axes.legend(labels = ["zero", "one"]
时它确实有效。无论如何,这解决了我的问题(但我很想知道一个更优雅的解决方案):
legend_elements = [Line2D([0], [0], color = 'w', markerfacecolor = 'b', marker = 'o', label='one', markersize=8),
Line2D([0], [0], color = 'w', markerfacecolor = 'orange', marker='o', label='zero', markersize=8)]
这对我有用,而且更干净
handles, labels = ax.get_legend_handles_labels()
labels = ["zero", "one"]
ax.legend(handles, labels)