英文:
Manipulating the legend for a Seaborn Jointplot
问题
我正在寻求关于如何编辑在使用seaborn jointplot和其他matplotlib散点图图层时,如何编辑图例中符号的颜色的建议。
我的具体问题是:
在给定的数据和当前图表中,如何调整代码以使图例中最后五个点(例如“3”,“4”,“5”,“6”,“8”)的颜色更改为灰色(即#b9b9bd)?
可重现的代码如下,但也可以在Colab Notebook中进行复制和实验。
# 导入包
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
# 加载mpg数据集
mpg_df = sns.load_dataset("mpg")
mpg_df = (
mpg_df
.astype({"cylinders":"category"})
)
mpg_df["cylinders"] = (
mpg_df["cylinders"]
.cat
.as_ordered()
)
# 确定标记符号
_markers_cylinders = {
3: "P",
4: "d",
5: "v",
6: "X",
8: "s"
}
# 为国家确定颜色
_palette_origin = {
"usa": "#fca847",
"japan": "#8aed7b",
"europe": "#7b81ed"
}
kws = {
"s": 225,
"linewidth": 2
}
# 绘制联合图 - 确定图形 - 只需边际分布的内容
jp = sns.jointplot(
data=mpg_df,
x="weight",
y="mpg",
hue="origin",
palette=_palette_origin,
markers=",",
marginal_kws={"fill": True},
color="w",
height=10,
s=1
)
# 作为原始联合图上的图层绘制按原产地和汽缸分类的散点图
origin = ["usa", "japan", "europe"]
for nation in origin:
df = mpg_df[mpg_df["origin"] == nation]
for k, v in _markers_cylinders.items():
jp.ax_joint.scatter(
data=df[df["cylinders"] == k],
x="weight",
y="mpg",
marker=_markers_cylinders[k],
c=_palette_origin[nation],
edgecolor="k",
alpha=0.6,
**kws
)
jp.ax_joint.grid(
color="k",
linestyle=":",
linewidth=0.75
)
han, lab = jp.ax_joint.get_legend_handles_labels()
lab = [
"USA",
"Japan",
"Europe",
"3",
"4",
"5",
"6",
"8"
]
jp.ax_joint.legend(
han[0:8],
lab[0:8],
title="Origin & Cylinders",
fontsize=15,
bbox_to_anchor=(1.20, 1),
title_fontsize=14,
markerscale=2.5,
shadow=True
)
sns.move_legend(
jp.ax_joint,
loc="upper left",
bbox_to_anchor=(1.20, 1),
markerscale=0.25
)
plt.show()
plt.show()
希望这有助于您更改图例中最后五个点的颜色为灰色(#b9b9bd)。
英文:
I am seeking advice on how to edit the colors of the symbols in my legend when using a seaborn jointplot with several other matplotlib scatterplot layers.
My Specific Question:
> Given the data and the current chart below, how can make an adjustment to the code so that the colors of the latter five points on the legend (e.g., "3", "4", "5", "6", "8") can be changed to gray (i.e., #b9b9bd)?
The reproducible code has been pasted below, but there is a publicly accessible Colab Notebook that can be copied and used for experimentation.
<details>
<summary> Reproducible Example </summary>
# import pacakges
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
# load the mpg dataset
mpg_df = sns.load_dataset("mpg")
mpg_df = (
mpg_df
.astype({"cylinders":"category"})
)
mpg_df["cylinders"] = (
mpg_df["cylinders"]
.cat
.as_ordered()
)
# establish the markers
_markers_cylinders = {
3:"P",
4:"d",
5:"v",
6:"X",
8:"s"
}
# establish colors for countries
_palette_origin = {
"usa":"#fca847",
"japan":"#8aed7b",
"europe":"#7b81ed"
}
kws={
"s": 225,
"linewidth": 2
}
# plot the jointplot -- establish the figure -- the content of the plot is not needed just the marginal ditributions
jp = sns.jointplot(
data=mpg_df,
x="weight",
y="mpg",
hue="origin",
palette=_palette_origin,
markers=",",
marginal_kws={"fill":True},
color="w",
height=10,
s=1
)
# plot scatter by origin and cylinder as layers on the original jointplot
origin = ["usa", "japan", "europe"]
for nation in origin:
df = mpg_df[mpg_df["origin"] == nation]
for k,v in _markers_cylinders.items():
jp.ax_joint.scatter(
data=df[df["cylinders"]==k],
x="weight",
y="mpg",
marker=_markers_cylinders[k],
c=_palette_origin[nation],
edgecolor="k",
alpha=0.6,
**kws
)
jp.ax_joint.grid(
color="k",
linestyle=":",
linewidth=0.75
)
han, lab = jp.ax_joint.get_legend_handles_labels()
lab = [
"USA",
"Japan",
"Europe",
"3",
"4",
"5",
"6",
"8"
]
jp.ax_joint.legend(
han[0:8],
lab[0:8],
title="Origin & Cylinders",
fontsize=15,
bbox_to_anchor=(1.20, 1),
title_fontsize = 14,
markerscale=2.5,
shadow = True
)
sns.move_legend(
jp.ax_joint,
loc="upper left",
bbox_to_anchor=(1.20, 1),
markerscale=0.25
)
plt.show()
plt.show()
</details>
答案1
得分: 1
这可以通过重复使用现有的标记并更改标记的面颜色来完成。
han, lab = jp.ax_joint.get_legend_handles_labels()
new_han = [
handles[0],
handles[1],
handles[2],
Line2D([0], [0], marker='P', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
Line2D([0], [1], marker='d', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
Line2D([0], [2], marker='v', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
Line2D([0], [3], marker='X', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
Line2D([0], [4], marker='s', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls='')
]
jp.ax_joint.legend(
new_han,
lab[0:8],
title="Origin & Cylinders",
fontsize=15,
bbox_to_anchor=(1.20, 1),
title_fontsize=14,
markerscale=2.5,
shadow=True
)
请注意,此代码段中的HTML实体已被还原为普通文本。
英文:
This can be accomplished by reusing the existing handles and then changing the face color of the marker.
han, lab = jp.ax_joint.get_legend_handles_labels()
new_han = [
handles[0],
handles[1],
handles[2],
Line2D([0], [0], marker='P', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
Line2D([0], [1], marker='d', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
Line2D([0], [2], marker='v', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
Line2D([0], [3], marker='X', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
Line2D([0], [4], marker='s', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls='')
]
jp.ax_joint.legend(
new_han,#han[0:8],
lab[0:8],
title="Origin & Cylinders",
fontsize=15,
bbox_to_anchor=(1.20, 1),
title_fontsize = 14,
markerscale=2.5,
shadow = True
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论