操作Seaborn Jointplot的图例

huangapple go评论47阅读模式
英文:

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(&quot;mpg&quot;)
mpg_df = (
mpg_df
.astype({&quot;cylinders&quot;:&quot;category&quot;})
)
mpg_df[&quot;cylinders&quot;] = (
mpg_df[&quot;cylinders&quot;]
.cat
.as_ordered()
)
# establish the markers
_markers_cylinders = {
3:&quot;P&quot;, 
4:&quot;d&quot;, 
5:&quot;v&quot;, 
6:&quot;X&quot;,
8:&quot;s&quot;
}
# establish colors for countries
_palette_origin = {
&quot;usa&quot;:&quot;#fca847&quot;,
&quot;japan&quot;:&quot;#8aed7b&quot;,
&quot;europe&quot;:&quot;#7b81ed&quot;
}
kws={
&quot;s&quot;: 225,
&quot;linewidth&quot;: 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=&quot;weight&quot;,
y=&quot;mpg&quot;,
hue=&quot;origin&quot;,
palette=_palette_origin,
markers=&quot;,&quot;,
marginal_kws={&quot;fill&quot;:True},
color=&quot;w&quot;,
height=10,
s=1
)
# plot scatter by origin and cylinder as layers on the original jointplot
origin = [&quot;usa&quot;, &quot;japan&quot;, &quot;europe&quot;]
for nation in origin:
df = mpg_df[mpg_df[&quot;origin&quot;] == nation]
for k,v in _markers_cylinders.items():
jp.ax_joint.scatter(
data=df[df[&quot;cylinders&quot;]==k],
x=&quot;weight&quot;, 
y=&quot;mpg&quot;, 
marker=_markers_cylinders[k], 
c=_palette_origin[nation],
edgecolor=&quot;k&quot;,
alpha=0.6,
**kws
)
jp.ax_joint.grid(
color=&quot;k&quot;,
linestyle=&quot;:&quot;, 
linewidth=0.75
)
han, lab = jp.ax_joint.get_legend_handles_labels()
lab = [
&quot;USA&quot;,
&quot;Japan&quot;,
&quot;Europe&quot;,
&quot;3&quot;, 
&quot;4&quot;, 
&quot;5&quot;, 
&quot;6&quot;, 
&quot;8&quot;
]
jp.ax_joint.legend(
han[0:8],
lab[0:8],
title=&quot;Origin &amp; Cylinders&quot;,
fontsize=15,
bbox_to_anchor=(1.20, 1),
title_fontsize = 14,
markerscale=2.5,
shadow = True
)
sns.move_legend(
jp.ax_joint,
loc=&quot;upper left&quot;,
bbox_to_anchor=(1.20, 1),
markerscale=0.25
)
plt.show()
plt.show()

</details>

操作Seaborn Jointplot的图例

答案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=&#39;P&#39;, markerfacecolor=&#39;#b9b9bd&#39;, markeredgecolor=&#39;black&#39;, markersize=14, ls=&#39;&#39;),
Line2D([0], [1], marker=&#39;d&#39;, markerfacecolor=&#39;#b9b9bd&#39;, markeredgecolor=&#39;black&#39;, markersize=14, ls=&#39;&#39;),
Line2D([0], [2], marker=&#39;v&#39;, markerfacecolor=&#39;#b9b9bd&#39;, markeredgecolor=&#39;black&#39;, markersize=14, ls=&#39;&#39;),
Line2D([0], [3], marker=&#39;X&#39;, markerfacecolor=&#39;#b9b9bd&#39;, markeredgecolor=&#39;black&#39;, markersize=14, ls=&#39;&#39;),
Line2D([0], [4], marker=&#39;s&#39;, markerfacecolor=&#39;#b9b9bd&#39;, markeredgecolor=&#39;black&#39;, markersize=14, ls=&#39;&#39;)
]
jp.ax_joint.legend(
new_han,#han[0:8],
lab[0:8],
title=&quot;Origin &amp; Cylinders&quot;,
fontsize=15,
bbox_to_anchor=(1.20, 1),
title_fontsize = 14,
markerscale=2.5,
shadow = True
)

操作Seaborn Jointplot的图例

huangapple
  • 本文由 发表于 2023年2月6日 12:38:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定