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
159 views
in Technique[技术] by (71.8m points)

python - How to change the shape of the marker depending on a column variable?

I am trying to create a scatter plot for a csv file in python which contains 4 columns x, y, TL, L. I am supposed to plot x versus y and change the color of the marker based on the class ID in the TL column which I have achieved in the below code.

import pandas as pd    
from matplotlib import pyplot as plt    
import numpy as np

df=pd.read_csv('knnDataSet.csv')  
df.columns=['SN','x','y','TL','L']

color=['red','green','blue']

groups = df.groupby('TL')

fig, ax = plt.subplots()

for name, group in groups:    
    ax.plot(group.x, group.y, marker='o', linestyle='', ms=12, label=name)

ax.legend()    
plt.show()

Also, I need to change the shape depending on whether the marker has a label in the L column or not, but I dont know how to update my code to fit this requirement.

Here is the link for the knnDataSet.csv file: knnDataSet.csv

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You probably want something like this:

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np

df=pd.read_csv('knnDataSet.csv')
df.columns=['SN','x','y','TL','L']
color=['red','green','blue']

groups = df.groupby('TL')

fig, ax = plt.subplots(figsize=(11,8))

for name, group in groups:   
    for x in group.values:
        if np.isnan(x[4]):
            ax.plot(x[1], x[2], marker='x', linestyle='', ms=12)
        else:
            ax.plot(x[1], x[2], marker='o', linestyle='', ms=12)                       

#ax.legend()
plt.show()

If a label in the L column is NOT defined then the marker will be X
If a label in the L column IS defined then the marker will be O

Output: enter image description here


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

...