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

标签 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))

最佳答案

这是您追求的类型吗?

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

关于python - 如何在散点图中自定义标记颜色和形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47684652/

相关文章:

python - Python如何计算列表长度

python - matplotlib图例中符号左侧的文字

python - 配置 argparse 以接受带引号的参数

python - 在 python 中使用 matplotlib 的脂肪带

javascript - nvd3 带对数刻度的气泡图

Python 分组计算

python - pyplot 轴与图之间的距离

两个不等长分布的 Python Q-Q 和 P-P 图

javascript - 在 Google Charts ScatterChart 上画一条线

返回 ggplot 散点图中所有组的回归线