使用 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), …Run Code Online (Sandbox Code Playgroud) 我试图用颜色和正确的标签表示预测,以作为虹膜数据集的标记。这是我到目前为止的内容:
from sklearn.mixture import GMM
import pandas as pd
from sklearn import datasets
import matplotlib.pyplot as plt
import itertools
iris = datasets.load_iris()
x = iris.data
y = iris.target
gmm = GMM(n_components=3).fit(x)
labels = gmm.predict(x)
fig, axes = plt.subplots(4, 4)
Superman = iris.feature_names
markers = ["o" , "s" , "D"]
Mi=[]
for i in range(150):
Mi.append(markers[y[i]])
for i in range(4):
for j in range(4):
if(i != j):
axes[i, j].scatter(x[:, i], x[:, j], c=labels, marker = Mi, s=40, cmap='viridis')
else:
axes[i,j].text(0.15, 0.3, …Run Code Online (Sandbox Code Playgroud)