python - 如何从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:列表索引超出范围'。知道如何使用绘图为它着色吗?谢谢您的帮助。
解决方案
如果他们来自巴西,你只会画画吗?否则,您可以编写 else 语句并选择所需的颜色
推荐阅读
- discord.js - Discord.js 排行榜命令不起作用
- css - 阻止 div 在导航栏上重叠
- json - JSONata - 如何按键值排序?
- php - 在同一函数中获取 num_rows 和 result() 数据,并在 codeigniter 中传递给控制器数据
- jenkins - Jenkins SSH 主机/用户证书工作流程
- c# - 删除 DevEpress 电子表格中列中的重复值
- vb.net - vb.net 如何在数据表的列上归档用户电报 ID?
- architecture - 我们如何将程序与 MIPS 中的指令流匹配
- python - 根据python中的分类值数据框提取新列并填充值
- raspberry-pi - 如何为运行 SSM 代理的实例设置实例名称?