如何在散点图Pylab中的不同点使用不同的标记 [英] How to use different marker for different point in scatter plot pylab
问题描述
我要使用pylab的散点图功能
I want to use the scatter plot function of pylab
x = [1,2,3,4,5]
y = [2,1,3,6,7]
在这5个点中有两个聚类,索引1-2(集群1)和索引2-4(集群2).簇 1 中的点应使用标记 '^',而簇 2 中的点应使用标记 's'.所以
there are two clusters in this 5 points, index 1-2(cluster 1) and index 2-4 (cluster 2). The point in cluster 1 should use marker '^', whereas the point in cluster 2 should use marker 's'. so
cluster = ['^','^','^','s','s']
我试过了
fig, ax = pl.subplots()
ax.scatter(x,y,marker=cluster)
pl.show()
这是一个玩具示例,真实数据有10000多个样本
This is a toy example, real data have more than 10000 samples
推荐答案
要获得此结果,您需要在同一轴上多次调用 scatter
.好消息是您可以针对给定数据自动执行此操作:
To achieve this result you need to call scatter
multiple times on the same axis. The good news is you can automate this for your given data:
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [2,1,3,6,7]
cluster = ['^','^','^','s','s']
fig, ax = plt.subplots()
for xp, yp, m in zip(x, y, cluster):
ax.scatter([xp],[yp], marker=m)
plt.show()
一个更整洁的解决方案是使用您的集群信息过滤您的输入数据.我们可以使用 numpy
做到这一点.
A neater solution would be to filter your input data using your cluster information. We can do that using numpy
.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1,2,3,4,5])
y = np.array([2,1,3,6,7])
cluster = np.array([1,1,1,2,2])
fig, ax = plt.subplots()
ax.scatter(x[cluster==1],y[cluster==1], marker='^')
ax.scatter(x[cluster==2],y[cluster==2], marker='s')
plt.show()
这篇关于如何在散点图Pylab中的不同点使用不同的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!