【问题标题】:How to change the shape of the marker depending on a column variable?如何根据列变量更改标记的形状?
【发布时间】:2017-02-03 04:33:53
【问题描述】:

我正在尝试在 python 中为包含 4 列 xyTLL 的 csv 文件创建散点图。我应该绘制xy,并根据我在下面的代码中实现的TL 列中的class ID 更改标记的颜色。

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

另外,我需要根据标记在L 列中是否有标签来更改形状,但我不知道如何更新我的代码以满足此要求。

这是knnDataSet.csv 文件的链接: knnDataSet.csv

【问题讨论】:

标签: python matplotlib scatter-plot markers


【解决方案1】:

你可能想要这样的东西:

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

如果 L 列中的标签定义,则标记将为 X
如果定义了 L 列 IS 中的标签,则标记将为 O

输出:

【讨论】:

    猜你喜欢
    • 2022-08-16
    • 2014-07-09
    • 1970-01-01
    • 2016-10-17
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多