Plotnine:如何使用geom_col和geom_text显示分组的均值

huangapple go评论104阅读模式
英文:

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)))

Plotnine:如何使用geom_col和geom_text显示分组的均值

I am looking to add these labels via geom_text():
Plotnine:如何使用geom_col和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) 
)

Plotnine:如何使用geom_col和geom_text显示分组的均值

英文:

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) 
)

Plotnine:如何使用geom_col和geom_text显示分组的均值

huangapple
  • 本文由 发表于 2023年6月29日 21:08:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定