如何根据列变量更改标记的形状?
How to change the shape of the marker depending on a column variable?
我正在尝试为 python 中的 csv 文件创建散点图,其中包含 4 列 x
、y
、TL
、L
.我应该绘制 x
与 y
并根据我在下面的代码中实现的 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
文件的 link:
knnDataSet.csv
您可能想要这样的东西:
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
输出:
我正在尝试为 python 中的 csv 文件创建散点图,其中包含 4 列 x
、y
、TL
、L
.我应该绘制 x
与 y
并根据我在下面的代码中实现的 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
文件的 link:
knnDataSet.csv
您可能想要这样的东西:
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
输出: