英文:
Plotnine: How to use geom_col and geom_text to display mean by group
问题
(ggplot(df, aes(x='inning', y='value', fill='team'))
+ geom_col(stat='summary', position='dodge')
+ geom_text(aes(label='stat(y)', group='team'), stat='summary', position=position_dodge(width=0.9), vjust=-0.5, size=8)
+ scale_x_continuous(breaks=np.arange(1,10,1)))
英文:
I have a dataset that I am using geom_col to visualize using stat='summary'. I can't quite figure out how to use geom_text() to display the mean values. Here is the code and plot I am able to produce:
import pandas as pd
import numpy as np
from plotnine import *
df = pd.DataFrame({'team': {4: 'Tampa Bay Rays',
8: 'Tampa Bay Rays',
85: 'Tampa Bay Rays',
89: 'Tampa Bay Rays',
1277: 'League',
1393: 'League',
3544: 'League',
3660: 'League'},
'inning': {4: 1, 8: 1, 85: 2, 89: 2, 1277: 1, 1393: 1, 3544: 2, 3660: 2},
'value': {4: 3.0,
8: 1.0,
85: 1.0,
89: 0.0,
1277: 2.0,
1393: 0.0,
3544: 2.0,
3660: 0.0}})
(ggplot(df, aes(x='inning', y='value', fill='team'))
+ geom_col(stat='summary', position='dodge')
+ scale_x_continuous(breaks=np.arange(1,10,1)))
I am looking to add these labels via geom_text():
Those labels are essentially the result of this code, which I was hoping to avoid, and just have plotnine calculate:
df[['team','inning','value']].groupby(['team','inning']).mean()
答案1
得分: 1
你需要给 geom_text
设置与 geom_col
相同的统计和位置设置。
df = pd.DataFrame({'team': {4: 'Tampa Bay Rays',
8: 'Tampa Bay Rays',
85: 'Tampa Bay Rays',
89: 'Tampa Bay Rays',
1277: 'League',
1393: 'League',
3544: 'League',
3660: 'League'},
'inning': {4: 1, 8: 1, 85: 2, 89: 2, 1277: 1, 1393: 1, 3544: 2, 3660: 2},
'value': {4: 3.0,
8: 1.0,
85: 1.0,
89: 0.0,
1277: 2.0,
1393: 0.0,
3544: 2.0,
3660: 0.0}})
(ggplot(df, aes(x='inning', y='value', fill='team'))
+ geom_col(stat='summary', position='dodge')
+ geom_text(
aes(label=after_stat("y"), color="team"),
stat="summary",
position=position_dodge(width=0.9),
va="bottom"
)
+ scale_x_continuous(breaks=np.arange(1,10,1))
+ scale_color_discrete(l=.4)
)
英文:
You need to give geom_text
the same statistics and position settings as geom_col
.
df = pd.DataFrame({'team': {4: 'Tampa Bay Rays',
8: 'Tampa Bay Rays',
85: 'Tampa Bay Rays',
89: 'Tampa Bay Rays',
1277: 'League',
1393: 'League',
3544: 'League',
3660: 'League'},
'inning': {4: 1, 8: 1, 85: 2, 89: 2, 1277: 1, 1393: 1, 3544: 2, 3660: 2},
'value': {4: 3.0,
8: 1.0,
85: 1.0,
89: 0.0,
1277: 2.0,
1393: 0.0,
3544: 2.0,
3660: 0.0}})
(ggplot(df, aes(x='inning', y='value', fill='team'))
+ geom_col(stat='summary', position='dodge')
+ geom_text(
aes(label=after_stat("y"), color="team"),
stat="summary",
position=position_dodge(width=0.9),
va="bottom"
)
+ scale_x_continuous(breaks=np.arange(1,10,1))
+ scale_color_discrete(l=.4)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论