我想使用pylab的散点图功能
x = [1,2,3,4,5]
y = [2,1,3,6,7]在这5个点中有两个簇,索引1-2(簇1)和索引2-4 (簇2)。簇1中的点应该使用标记'^',而簇2中的点应该使用标记's‘。所以
cluster = ['^','^','^','s','s']我试过了
fig, ax = pl.subplots()
ax.scatter(x,y,marker=cluster)
pl.show()这是一个玩具示例,真实数据有超过10000个样本
发布于 2015-02-25 05:08:38
要实现此结果,需要在同一轴上多次调用scatter。好消息是,您可以为给定的数据自动执行此操作:
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来做到这一点。
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()https://stackoverflow.com/questions/28706115
复制相似问题