【发布时间】: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