字典中的 Matplotlib 散点图标记类型

Matplotlib scatter plot marker type from dictionary

使用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),
)

但是下面会报错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),
)

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

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")