plotnine - 尝试为 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 而不是实际计数。
解决方案
我刷新了所有库并重新启动了笔记本服务器,现在它可以工作了。我一定在某个地方安装不好。
推荐阅读
- python - 避免 3d 矩阵运算的嵌套 for 循环的一般选项
- python-3.x - 如何在python中将十进制转换为二进制?
- azure-cosmosdb - CosmosDB - 创建索引
- javascript - 如何规避浏览器缓存?
- avro - ksql 表向行键添加了额外的字符
- node.js - 如何在网页中发送kafka消息
- php - 如何将两条记录插入当前仅插入一条的数据库中 - 使用 laravel
- ios - 如何在 iOS 8 及更高版本中使用 OAEP-256 填充实现 RSA 加密?
- python - 为什么下面的模式程序没有给出想要的输出?
- angular - Ngmodel 不是输入的属性