【问题标题】:plotnine geom_boxplot ignores required aesthetic and requires unnecessary aestheticplotnine geom_boxplot 忽略了必需的审美,需要不必要的审美
【发布时间】:2019-10-04 04:56:56
【问题描述】:

我的数据看起来像:

Scenario        ymin         lower        middle         upper      ymax
One     16362.586379  20911.338893  27121.693254  35219.449009  46406.087619
Two     19779.003240  25390.096116  33108.174561  43545.202225  58464.277060

我没有为每个场景使用全部 50 k 个数据点(不止一个和两个),而是计算了盒子和胡须所需的位置。

我尝试通过

import pandas
import plotnine as p9

df = pandas.read_excel('boxplot_data.xlsx', sheet='Sheet1')
gg = p9.ggplot()
gg += p9.geoms.geom_boxplot(mapping=p9.aes(x='Scenario', ymin='ymin', lower='lower', middle='middle', upper='upper', ymax='ymax'), data=df, color='k', show_legend=False, inherit_aes=False)
gg += p9.themes.theme_seaborn()
gg += p9.labels.xlab('Scenario')
gg.save(filename='scenario_boxplot.png', dpi=300)

https://plotnine.readthedocs.io/en/stable/generated/plotnine.geoms.geom_boxplot.html#plotnine.geoms.geom_boxplot 的文档表明 geom_boxplot 代码行提供了定义盒子和胡须所需的美学参数。

但是,运行它会给出

plotnine.exceptions.PlotnineError: 'stat_boxplot 需要 遵循缺失的美学:y'

为什么 stat_boxplot 被调用, 需要美学,而不是 geom_boxplot

更重要的是,有人知道如何纠正这个问题吗?

【问题讨论】:

    标签: python python-ggplot plotnine


    【解决方案1】:

    您将geom_boxplotstat_boxplot 一起使用,而不是stat_identity

    geom_boxplot(stat='identity', ...)
    

    【讨论】:

    • 这行得通,非常感谢。不过,我不明白为什么。如果我想用stats.stat_boxplot,我不就用it吗?
    • geoms 和 stats 有独立的要求。默认情况下,geoms 和 stats 是配对的,因此stat 计算geom 的要求,因此用户只需提供stat 的要求。如果您选择提供geom 的要求,那么您必须忽略该geom 的默认stat。这就是stat_identity 的用途,即将用户映射直接传递到geom
    猜你喜欢
    • 2013-07-19
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2017-08-05
    相关资源
    最近更新 更多