我正在尝试使用 plotnine 创建带有标签的条形图。根据文档,您可以在 geom_text 的美学中使用 label="stat(count)" 来打印每个条的位置计数。这相当于在 R 的 ggplot2 中使用 ..count.. 关键字。
python版本是3.6.7 plotnine版本是0.5.1
根据文档,此代码应该可以工作:
import numpy as np
import pandas as pd
from plotnine import *
from plotnine.stats import *
from plotnine.data import mtcars
(ggplot(mtcars, aes('factor(cyl)', fill='factor(am)'))
+ geom_bar( position='fill')
+ geom_text(aes(label='stat(count)'), stat='count', position='fill')
)
当我尝试这个时,我收到这条消息:
PlotnineError: "Could not evaluate the 'label' mapping: 'stat(count)' (original error: name 'stat' is not defined)
如果我将表达式替换label='stat(count)'
为label='99'
代码运行并显示正确的绘图,当然所有标签都是常数值 99 而不是实际计数。