Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

python - How to use different marker for different point in scatter plot pylab

I want to use the scatter plot function of pylab

x = [1,2,3,4,5]
y = [2,1,3,6,7]

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']

I have tried

fig, ax = pl.subplots()
ax.scatter(x,y,marker=cluster)
pl.show()

This is a toy example, real data have more than 10000 samples

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...