首页 > 解决方案 > 如何从matplotlib中指定绘图颜色

问题描述

我有来自 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()

但这一切都变成了蓝色。 我试图改变这样的循环:

if(df['Nationality'][i]=='Brazil'): 
        ax.plot(x[i], y[i], 'o', color='blue')

但它说'IndexError:列表索引超出范围'。知道如何使用绘图为它着色吗?谢谢您的帮助。

标签: pythonpandasmatplotlib

解决方案


如果他们来自巴西,你只会画画吗?否则,您可以编写 else 语句并选择所需的颜色


推荐阅读