【发布时间】:2020-05-31 10:33:50
【问题描述】:
我有来自 fifa 19 的球员数据。它有沉着、点球能力和国籍。我想绘制一个 X 轴是沉着,Y 轴是点球能力的图,但是如果玩家来自某个国籍,我想给它着色。我有这个代码:
import pandas
import math
import matplotlib.pyplot as plt
df= pandas.read_csv('fifa19.csv')
n= len(df)
x=[]
y=[]
fig, ax=plt.subplots()
j=0
for i in range(n):
if(not math.isnan(df['Composure'][i])
and not math.isnan(df['Penalties'][i])):
x.append(df['Composure'][i])
y.append(df['Penalties'][i])
if(df['Nationality'][i]=='Brazil'): #it supposed to be blue if the nationality is brazil else is default(correct me if i'm wrong)
ax.plot(x, y, 'o', color='blue')
ax.set_xlabel('Composure attribute')
ax.set_ylabel('Penalties attribute')
fig.show()
But it all turned to blue. 我试图改变这样的循环:
if(df['Nationality'][i]=='Brazil'):
ax.plot(x[i], y[i], 'o', color='blue')
但它显示“IndexError: list index out of range”。知道如何使用绘图对其进行着色吗?感谢您的帮助。
【问题讨论】:
标签: python pandas matplotlib