我试图同时在matplotlib中绘制地面真相和我的分类。
目前,在应用tsne
到特征空间并使用以下代码添加边缘之后,我仅绘制出了地沟真相
from matplotlib.collections import LineCollection
cols=['rgbkm'[lbl] for lbl in list(data.y.cpu().numpy() - 1)]
lc = LineCollection(X_embedded[out_dict['edges']],linewidth=0.05)
fig = plt.figure()
plt.gca().add_collection(lc)
plt.xlim(X_embedded[:,0].min(), X_embedded[:,0].max())
plt.ylim(X_embedded[:,1].min(), X_embedded[:,1].max())
plt.scatter(X_embedded[:,0],X_embedded[:,1], c=cols)
同时,我希望以某种方式为每个顶点着色:
这是两种方法。
常规散点图的点可以具有内部颜色和边缘颜色。scatter
为其中任何一个接受一个数组,但对两个都不接受。因此,您可以遍历所有边缘颜色,并在同一图上循环绘制它们。使用线宽可以帮助将真实的颜色和预测的颜色一起可视化。
Matplotlib的plot
功能接受标记填充样式,该样式可能是上下双色或左右双色的。每个图只能给出一种类型的样式。因此,对于5种颜色,可以循环绘制25种组合。
在循环显示颜色时,绘图可以生成带有相应双色点的图例标签。
这是一些代码来说明概念:
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
import numpy as np
N = 50
labels = ['ant', 'bee', 'cat', 'dog', 'elk'] # suppose these are the labels for the prediction
colors = list('rgbkm') # a list of 5 colors
cols_true = np.repeat(range(5), N) # suppose the first N have true color 0, the next N true color 1, ...
cols_pred = np.random.randint(0, 5, N * 5) # as a demo, take a random number for each predicted color
# for x and y, suppose some 2D gaussian normal distribution around some centers,
# this would make the 'true' colors nicely grouped
x = np.concatenate([np.random.normal(cx, 2, N) for cx in [5, 9, 7, 2, 2]])
y = np.concatenate([np.random.normal(cy, 1.5, N) for cy in [2, 5, 9, 8, 3]])
fig, ax = plt.subplots(figsize=(10,6))
for tc in range(5):
for pc in range(5):
mask = (cols_true == tc) & (cols_pred == pc)
plt.plot(x[mask], y[mask], c=colors[tc], markerfacecoloralt=colors[pc],
marker='.', linestyle='', markeredgecolor='None',
markersize=15, fillstyle='left', markeredgewidth=0,
label=f'Tr: {labels[tc]} - Pr: {labels[pc]}')
plt.legend(loc='upper right', bbox_to_anchor=(1, -0.1), fontsize=10, ncol=5)
plt.tight_layout()
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句