首页 > 解决方案 > 尝试为 geom_bar 创建计数标签时出错

问题描述

我正在尝试使用 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 而不是实际计数。

标签: plotnine

解决方案


我刷新了所有库并重新启动了笔记本服务器,现在它可以工作了。我一定在某个地方安装不好。


推荐阅读