Matplotlib 散点图标记类型来自字典

a11*_*a11 4 scatter matplotlib

使用 Matplotlib 制作散点图(不是 Seaborn、Pandas 或其他高级接口),如何使用字典来指定标记类型?

此示例使用颜色字典:

x = [4, 8, 1, 0, 2]
y = [0.1, 1, 0.4, 0.8, 0.9]
name = ["A", "A", "B", "A", "B"]
df = pd.DataFrame(data=zip(x, y, name), columns=["x", "y", "name"])

colors = {"A": "red", "B": "blue"}

fig, ax = plt.subplots(1, 1)
ax.scatter(
    x=df["x"],
    y=df["y"],
    facecolors="none",
    edgecolors=df["name"].map(colors),
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但以下会引发错误TypeError: 'Series' objects are mutable, thus they cannot be hashed

markers = {"A": "v", "B": "D"}

fig, ax = plt.subplots(1, 1)
ax.scatter(
    x=df["x"],
    y=df["y"],
    facecolors="none",
    edgecolors=df["name"].map(colors),
    marker=df['name'].map(markers),
)
Run Code Online (Sandbox Code Playgroud)

a11*_*a11 5

根据 @BigBen 的评论,Matplotlib 似乎不支持多个标记。@BigBen 链接到几个示例解决方法,但我发现以下最适合我,因为它允许我在代码开头显式地将关键字与标记样式相关联,无论我正在使用哪个子df集。(现实生活中的数据有十几个“名称”值,我正在根据其他列中的属性处理各种混合子集。)

x = [4, 8, 1, 0, 2]
y = [0.1, 1, 0.4, 0.8, 0.9]
name = ["A", "A", "B", "A", "B"]
df = pd.DataFrame(data=zip(x, y, name), columns=["x", "y", "name"])

colors = {"A": "red", "B": "blue"}
markers = {"A": "v", "B": "D"}

fig, ax = plt.subplots(1, 1)

for name, group in df.groupby("name"):
    group = group.copy()
    m = markers.get(name)

    ax.scatter(
        x=group["x"],
        y=group["y"],
        facecolors="none",
        edgecolors=group["name"].map(colors),
        marker=m,
        label=name,
    )
    ax.legend(loc="lower right")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述