【问题标题】:Error trying to create count labels for geom_bar尝试为 geom_bar 创建计数标签时出错
【发布时间】:2019-01-28 00:59:03
【问题描述】:

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

【问题讨论】:

  • 您的代码适用于我在 Python 3.7.0 上使用 plotnine 0.5.1。
  • 谢谢,我一定是在安装过程中搞砸了。现在可以了。

标签: plotnine


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多