如何根据列变量更改标记的形状? [英] How to change the shape of the marker depending on a column variable?

查看:30
本文介绍了如何根据列变量更改标记的形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

将pandas导入为pd从 matplotlib 导入 pyplot 作为 plt将numpy导入为npdf=pd.read_csv('knnDataSet.csv')df.columns = ['SN','x','y','TL','L']color = ['红色','绿色','蓝色']组= df.groupby('TL')无花果,ax = plt.subplots()对于名称,分组分组:ax.plot(group.x,group.y,marker ='o',linestyle ='',ms = 12,label = name)ax.legend()plt.show()

此外,我需要根据标记在 L 列中是否带有标签来更改形状,但是我不知道如何更新代码以适应此要求.

这是 knnDataSet.csv 文件的链接:

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

解决方案

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:

这篇关于如何根据列变量更改标记的形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆