Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib scatter plot marker type from dictionary

Using Matplotlib to make a scatter plot (not Seaborn, Pandas, or other high-level interface), how can I use a dictionary to specify marker types?

This example works with a color dictionary:

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

enter image description here

But the following throws an error 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),
)
like image 851
a11 Avatar asked Sep 21 '25 00:09

a11


1 Answers

Based on the comments by @BigBen, it looks like Matplotlib doesn't support multiple markers. @BigBen linked to a couple of example work-arounds, but I found the following works best for me because it allows me to explicitly relate a keyword to a marker style at the beginning of my code, regardless of what subset of df I am working with. (Real-life data has a dozen "name" values, and I am working with various and mixed subsets based on attributes in other columns.)

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

enter image description here

like image 114
a11 Avatar answered Sep 22 '25 17:09

a11