简体   繁体   English

如何根据年龄范围更改散点图 plot 上数据点的颜色?

[英]How to change color of data points on a scatter plot according to an age range?

!( https://i.stack.imgur.com/FX1vB.png ) !( https://i.stack.imgur.com/mGajr.png ) !( https://i.stack.imgur.com/FX1vB.png ) !( https://i.stack.imgur.com/mGajr.png )

Hello everyone,大家好,

I am very new to Python so bear with me.我是 Python 的新手,请多多包涵。 I am sure this is an easy answer.我相信这是一个简单的答案。

Above is my scatter plot, with GOLF Data from Kaggle.以上是我的散点图 plot,带有来自 Kaggle 的 GOLF 数据。 The X variable is Fairway Hit% and the Y variable is Average Driving Distance. X 变量是球道命中率,Y 变量是平均开球距离。 I can see there is a slight negative correlation in the data.我可以看到数据中存在轻微的负相关。

Each red dot is a player.每个红点都是一个玩家。 I want to make each dot a different color based on the age of the player.我想根据玩家的年龄使每个点具有不同的颜色。 There is a whole series in my data set titled 'AGE' and it varies from 21 to 49. For example, I want to have players that are aged 20-29 be a blue dot, aged 30-39 be a red dot, and aged 40-49 be a yellow dot.我的数据集中有一个名为“AGE”的系列,从 21 到 49 不等。例如,我想让 20-29 岁的球员成为蓝点,30-39 岁的球员成为红点,并且40-49 岁为黄点。

I have tried to research this to not much avail, as I tried to write code like the third picture above.我试图对此进行研究,但收效甚微,因为我试图编写如上图第三张图所示的代码。 I tried to define a subseries of 'AGE' as something like 'AGE' >= 20 <= 29.我试图将“AGE”的子系列定义为“AGE”>= 20 <= 29。

I haven't had any luck and I'm sure this isn't too difficult, so any help would be appreciated.我没有运气,我相信这不是太难,所以任何帮助将不胜感激。 Thank you.谢谢你。 INCORRECT DATA数据不正确

I tried to make each dot a different color that was representative of the age of the golfer.我试图让每个点都具有不同的颜色,以代表高尔夫球手的年龄。

import pandas as pd
df = pd.DataFrame({'Age': [18, 22, 26,36, 47,78]})
YOUNG = df[(df['Age']>=20) & (df['Age']<=29)]
YOUNG

Or if the type of Age is string,或者如果Age的类型是字符串,

import pandas as pd
df = pd.DataFrame({'Age': ['18', '22', '26', '36', '47', '78']})
df['Age'] = df['Age'].astype('int64')
YOUNG = df[(df['Age']>=20) & (df['Age']<=29)]
YOUNG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM