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

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

Plotnine: How to use geom_col and geom_text to display mean by group

问题

  1. (ggplot(df, aes(x='inning', y='value', fill='team'))
  2. + geom_col(stat='summary', position='dodge')
  3. + geom_text(aes(label='stat(y)', group='team'), stat='summary', position=position_dodge(width=0.9), vjust=-0.5, size=8)
  4. + 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:

  1. import pandas as pd
  2. import numpy as np
  3. from plotnine import *
  4. df = pd.DataFrame({'team': {4: 'Tampa Bay Rays',
  5. 8: 'Tampa Bay Rays',
  6. 85: 'Tampa Bay Rays',
  7. 89: 'Tampa Bay Rays',
  8. 1277: 'League',
  9. 1393: 'League',
  10. 3544: 'League',
  11. 3660: 'League'},
  12. 'inning': {4: 1, 8: 1, 85: 2, 89: 2, 1277: 1, 1393: 1, 3544: 2, 3660: 2},
  13. 'value': {4: 3.0,
  14. 8: 1.0,
  15. 85: 1.0,
  16. 89: 0.0,
  17. 1277: 2.0,
  18. 1393: 0.0,
  19. 3544: 2.0,
  20. 3660: 0.0}})
  21. (ggplot(df, aes(x='inning', y='value', fill='team'))
  22. + geom_col(stat='summary', position='dodge')
  23. + 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 相同的统计和位置设置。

  1. df = pd.DataFrame({'team': {4: 'Tampa Bay Rays',
  2. 8: 'Tampa Bay Rays',
  3. 85: 'Tampa Bay Rays',
  4. 89: 'Tampa Bay Rays',
  5. 1277: 'League',
  6. 1393: 'League',
  7. 3544: 'League',
  8. 3660: 'League'},
  9. 'inning': {4: 1, 8: 1, 85: 2, 89: 2, 1277: 1, 1393: 1, 3544: 2, 3660: 2},
  10. 'value': {4: 3.0,
  11. 8: 1.0,
  12. 85: 1.0,
  13. 89: 0.0,
  14. 1277: 2.0,
  15. 1393: 0.0,
  16. 3544: 2.0,
  17. 3660: 0.0}})
  18. (ggplot(df, aes(x='inning', y='value', fill='team'))
  19. + geom_col(stat='summary', position='dodge')
  20. + geom_text(
  21. aes(label=after_stat("y"), color="team"),
  22. stat="summary",
  23. position=position_dodge(width=0.9),
  24. va="bottom"
  25. )
  26. + scale_x_continuous(breaks=np.arange(1,10,1))
  27. + scale_color_discrete(l=.4)
  28. )

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

英文:

You need to give geom_text the same statistics and position settings as geom_col.

  1. df = pd.DataFrame({'team': {4: 'Tampa Bay Rays',
  2. 8: 'Tampa Bay Rays',
  3. 85: 'Tampa Bay Rays',
  4. 89: 'Tampa Bay Rays',
  5. 1277: 'League',
  6. 1393: 'League',
  7. 3544: 'League',
  8. 3660: 'League'},
  9. 'inning': {4: 1, 8: 1, 85: 2, 89: 2, 1277: 1, 1393: 1, 3544: 2, 3660: 2},
  10. 'value': {4: 3.0,
  11. 8: 1.0,
  12. 85: 1.0,
  13. 89: 0.0,
  14. 1277: 2.0,
  15. 1393: 0.0,
  16. 3544: 2.0,
  17. 3660: 0.0}})
  18. (ggplot(df, aes(x='inning', y='value', fill='team'))
  19. + geom_col(stat='summary', position='dodge')
  20. + geom_text(
  21. aes(label=after_stat("y"), color="team"),
  22. stat="summary",
  23. position=position_dodge(width=0.9),
  24. va="bottom"
  25. )
  26. + scale_x_continuous(breaks=np.arange(1,10,1))
  27. + scale_color_discrete(l=.4)
  28. )

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:

确定