【发布时间】:2021-12-01 21:18:07
【问题描述】:
使用 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 还不支持多个标记吗?
标签: matplotlib scatter