我试图用颜色和正确的标签来表示预测,以作为虹膜数据集的标记。这是我到目前为止的内容:
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, Superman[i], fontsize = 8)
我不确定为什么Colors会迭代,而Markers却不这样做,但是有没有办法为每个Marks分配一个特定的值,例如color?当我仅输入y的数值时,它也会失败。
它返回的代码是:
无法识别的标记样式['o','o','o','o','o','o','o','o','o','o','o' ,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o','o','o','o','o','o','o','o','o','o','o','o','o' ,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o”,“ o','o','s','s','s','s','s','s','s','s','s','s','s' ,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s','s','s','s','s','s','s','s','s','s','s','s','s' ,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s”,“ s','s','D','D','D','D','D','D','D','D','D','D','D' ,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D','D','D','D','D','D','D','D','D','D','D','D','D' ,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D”,“ D','D']
答案 0 :(得分:2)
matplotlib目前不支持在单个散点图中使用多个标记。不过,https://github.com/matplotlib/matplotlib/issues/11155
对此有功能要求当然可以绘制多个散点图,每种标记类型一个。 我在上面的线程中建议的选项是一个不同的选项,它是在创建散点后设置标记:
import numpy as np
import matplotlib.pyplot as plt
def mscatter(x,y,ax=None, m=None, **kw):
import matplotlib.markers as mmarkers
if not ax: ax=plt.gca()
sc = ax.scatter(x,y,**kw)
if (m is not None) and (len(m)==len(x)):
paths = []
for marker in m:
if isinstance(marker, mmarkers.MarkerStyle):
marker_obj = marker
else:
marker_obj = mmarkers.MarkerStyle(marker)
path = marker_obj.get_path().transformed(
marker_obj.get_transform())
paths.append(path)
sc.set_paths(paths)
return sc
N = 40
x, y, c = np.random.rand(3, N)
s = np.random.randint(10, 220, size=N)
m = np.repeat(["o", "s", "D", "*"], N/4)
fig, ax = plt.subplots()
scatter = mscatter(x, y, c=c, s=s, m=m, ax=ax)
plt.show()
如果只有数字,而不是标记符号,则首先需要将数字映射到符号并将符号列表提供给函数。
答案 1 :(得分:0)
您可以按照以下方式修改代码,以获得所需的结果:
markers = ["o" , "s" , "D"]
colors = ["red", "green", "blue"]
for i in range(4):
for j in range(4):
for k in range(x.shape[0]):
if(i != j):
axes[i, j].scatter(x[k, i], x[k, j], color=colors[labels[k]], marker = markers[y[k]], s=40, cmap='viridis')
else:
axes[i,j].text(0.15, 0.3, Superman[i], fontsize = 8)