Issue
I have a dataframe like:
index year month age
0 2020 January 20
1 2021 April 35
2 2020 February 30
3 2022 March 35
4 2019 April 47
5 2022 February 18
6 2020 January 43
7 2021 April 28
8 2019 April 37
9 2022 February 24
I'm making a chart like this:
plt.figure(figsize=(10, 8))
sns.countplot(x='year', data=df, hue='age')
plt.show()
But as a hue, I need to use not age, but 3 age groups: 20-30, 30-40 and over 40 years old. How to divide the age into 3 groups and use them as a hue? Thanks.
Solution
You can use pd.cut()
:
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame(
{'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
'year': [2020, 2021, 2020, 2022, 2019, 2022, 2020, 2021, 2019, 2022],
'month': ['January', 'April', 'February', 'March', 'April', 'February', 'January', 'April', 'April', 'February'],
'age': [40, 35, 30, 35, 47, 18, 43, 28, 37, 24]})
df['age_group'] = pd.cut(df.age, bins=[0, 29, 40, 200], right=True, labels=['under 30', '30-40', 'over 40'])
plt.figure(figsize=(10, 8))
sns.countplot(x='year', data=df, hue='age_group')
plt.show()
Answered By - Алексей Р
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.