0

I have data that looks like:

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

Rather than use all 50 k data points for every Scenario (there are many more than One and Two), I've computed the positions I need for the box and whiskers.

I try to plot this via

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)

The documentation at https://plotnine.readthedocs.io/en/stable/generated/plotnine.geoms.geom_boxplot.html#plotnine.geoms.geom_boxplot indicates that the geom_boxplot line of code supplies the required aesthetic parameters to define the box and whiskers.

Running this, however, gives

plotnine.exceptions.PlotnineError: 'stat_boxplot requires the following missing aesthetics: y'

Why is stat_boxplot being called, with its required aesthetics, not geom_boxplot?

And more importantly, does anybody know how to correct this?

GoneAsync
  • 349
  • 5
  • 18

1 Answers1

1

You are using geom_boxplot with stat_boxplot instead of stat_identity.

geom_boxplot(stat='identity', ...)
has2k1
  • 2,095
  • 18
  • 16
  • That works, many thanks. I don't understand why, though. If I wanted to use `stats.stat_boxplot`, wouldn't I just use _it_? – GoneAsync Oct 06 '19 at 00:10
  • 1
    geoms and stats have independent requirements. By default, geoms and stats are paired so that the `stat` computes the requirements of the `geom`, so the user has to only provide the requirements of the `stat`. If you choose to provide the requirements of the `geom`, then you have to ignore the default `stat` for that `geom`. That is what `stat_identity` is for i.e pass through the user mappings directly to the `geom`. – has2k1 Oct 06 '19 at 10:40