Skip to content

Unholy interaction between coord_trans and geom_smooth #526

@ivanistheone

Description

@ivanistheone

I am working through some examples in the Hadley WICKHAM's paper A Layered Grammar of Graphics and I found an edge case involving coord_trans and geom_smooth that throws an exception in plotnine.

WORKING EXAMPLE 1

# setup some sample data to plot
n = 10
df = pd.DataFrame(dict(x=range(1,1+n), y=range(11,11+n)+np.random.rand(n)))

# scatter plot with LM fit using log-log coordinates
ggplot(df, aes(x="x", y="y")) + \
    geom_point() + \
    coord_trans(x="log10", y="log10") + \
    geom_smooth(method="lm")

Don't look for logic in the code, just an example used to understand how coord_trans works.

ERROR CASE

Now I want to turn off the standard error estimate (gray region) that comes by default with geom_smooth :

# same as above but with LM's se turned off
ggplot(df, aes(x="x", y="y")) + \
    geom_point() + \
    coord_trans(x="log10", y="log10") + \
    geom_smooth(method="lm", se=False)

(litterally the only difference is the se=False addition)

and I get this error

TypeError: loop of ufunc does not support argument 0 of type NoneType which has no callable log10 method
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
AttributeError: 'NoneType' object has no attribute 'log10'

and a big stack trace:

Screen Shot 2021-08-16 at 6 46 41 PM

WORKING EXAMPLE 2

The reason I think it is some interaction between coord_trans and geom_smooth is because the code without the coord_trans line runs fine:

ggplot(df, aes(x="x", y="y")) + \
    geom_point() + \
    geom_smooth(method="lm", se=False)

Using

numpy==1.19.5
pandas==1.1.5
plotnine==0.8.0

under Python 3.6.9 inside VSCode on a mac

I found this pandas issue that might be relevant pandas-dev/pandas#33492 but not sure why it would pop-up only when se=False.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions