编程语言
首页 > 编程语言> > python – 在图中迭代标记

python – 在图中迭代标记

作者:互联网

我试图用颜色和正确的标签作为虹膜数据集的标记来表示预测.这是我到目前为止:

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)

我不确定为什么颜色迭代和标记没有,但是有没有办法为每个标记指定一个像颜色一样的值?当我从y输入数值时,它也会失败.

它返回的代码是:

Unrecognized marker style [‘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’]

解决方法:

在单个散点中使用多个标记当前不是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()

enter image description here

如果您只有数字而不是标记符号,则首先需要将数字映射到符号并将符号列表提供给函数.

标签:python,matplotlib,markers
来源: https://codeday.me/bug/20190926/1820382.html