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

时间:2017-12-06 22:37:55

标签: python matplotlib scatter-plot markers

我有一个包含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))

1 个答案:

答案 0 :(得分:6)

这是你之后的事吗?

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