如何自定义散点图中的标记颜色和形状?

2024-04-29 07:34:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个9列的数据集。7个特征用于特征选择,其中一个用于分类。 我使用tsne库进行特征选择,以查看我的数据可以被分类多少。tsne的结果如图所示。

但是,我想用另一种方式可视化我的数据。我想根据列f1(id)为每个观察设置一种颜色。例如:

f1(id) f2 f3 ... f9(class label)
1      66 77 ... A
1      44 88 ... A
2      33 55 ... B
2      77 88 ..  B

颜色来自f1,形状来自f9 . 我不知道怎么做!我会感谢您的意见或给我一些参考,以了解更多关于可视化部分。 enter image description here 这是我的代码:

plt.scatter(visualize_x, visualize_y, c= y,marker='^', cmap=plt.cm.get_cmap("jet", 10))

Tags: 数据id颜色可视化方式分类plt特征
1条回答
网友
1楼 · 发布于 2024-04-29 07:34:02

这是你要找的类型吗?

from matplotlib import pyplot as plt 

#generate a list of markers and another of colors 
markers = ["." , "," , "o" , "v" , "^" , "<", ">"]
colors = ['r','g','b','c','m', 'y', 'k']

#make a sample dataset
x = np.arange(0,10)  #test x values.. every feature gets the same x values but you can generalize this
y = [s*x for s in np.arange(7)] #generate 7 arrays of y values 


for i in range(7): #for each of the 7 features 
    mi = markers[i] #marker for ith feature 
    xi = x #x array for ith feature .. here is where you would generalize      different x for every feature
    yi = y[i] #y array for ith feature 
    ci = colors[i] #color for ith feature 
    plt.scatter(xi,yi,marker=mi, color=ci) 
plt.show() 

enter image description here

相关问题 更多 >