英文:
I need a bar graph with three variables at the same time
问题
我需要一个柱状图,y轴上有三个列:rating_standard、rating_rapid、rating_blitz。
我附上了一个类似于我的请求的图表。
df_7 = df3[df3['fide_id'] == 14109336]
df_7
fide_id | year | month | rating_standard | rating_rapid | rating_blitz |
---|---|---|---|---|---|
146116 | 2015 | 1 | 2530.0 | 2599.0 | 2540.0 |
146116 | 2015 | 2 | 2530.0 | 2530.0 | 2530.0 |
..... | .... | .... | .... | .... | .... |
146116 | 2021 | 3 | 2546.0 | 2546.0 | 2546.0 |
146116 | 2021 | 4 | 2546.0 | 2521.0 | 2608.0 |
英文:
I need a bar graph with three columns at the y axis: rating_standard, rating_rapid, rating_blitz.
I attach a graph similar to my request.
df_7 = df3[(df3['fide_id'] == 14109336)]
df_7
fide_id | year | month | rating_standard | rating_rapid | rating_blitz |
---|---|---|---|---|---|
146116 | 2015 | 1 | 2530.0 | 2599.0 | 2540.0 |
146116 | 2015 | 2 | 2530.0 | 2530.0 | 2530.0 |
..... | |||||
146116 | 2021 | 3 | 2546.0 | 2546.0 | 2546.0 |
146116 | 2021 | 4 | 2546.0 | 2521.0 | 2608.0 |
答案1
得分: 1
假设你的df_7
是一个Pandas DataFrame,有两种方法可以实现这个:
最简单的方法:
df_7.plot(kind='bar', x='month', y=['rating_standard', 'rating_rapid', 'rating_blitz'])
另一种方法使用seaborn
,你可以首先像这样更改数据的存储方式:
df = df.melt(id_vars=['month'],
value_vars=['rating_standard', 'rating_rapid', 'rating_blitz'],
var_name='rating',
value_name='value',
)
这将使表格变成如下形式:
month rating value
0 1 rating_standard 2530.0
1 2 rating_standard 2530.0
2 3 rating_standard 2546.0
3 4 rating_standard 2546.0
4 1 rating_rapid 2599.0
其中这些评分列现在是一个变量,也就是原表中的每一行现在对应新表中的3行。然后你可以使用seaborn如下:
import seaborn as sns
sns.barplot(data=df, x='month', y='value', hue='rating')
如果你想要实现与你分享的图像完全相同,包括月份名称、颜色等,让我知道,然后我们可以进行一些更改。
英文:
assuming your df_7 is a pandas DataFrame, there are two ways to do this:
easiest way:
df_7.plot(kind='bar',x='month',y=['rating_standard','rating_rapid','rating_blitz'])
another way using seaborn
, you can first change the way the data is stored in it like this:
df = df.melt(id_vars=['month'],
value_vars=['rating_standard','rating_rapid','rating_blitz'],
var_name='rating',
value_name='value',
)
it will make the table as:
month rating value
0 1 rating_standard 2530.0
1 2 rating_standard 2530.0
2 3 rating_standard 2546.0
3 4 rating_standard 2546.0
4 1 rating_rapid 2599.0
where those rating columns are now a variable, i.e. each row in the original table now corresponds to 3 rows in the new table. Then you can use seaborn as the following:
import seaborn as sns
sns.barplot(data=df, x='month', y='value', hue='rating')
let me know if you want to be exactly like the image you shared, like the month names, colors, etc. then we can make some changes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论