has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
3.92k stars 210 forks source link

AttributeError: 'RangeDiscrete' object has no attribute 'range' when calling scale_y_discrete #721

Closed CopyOfA closed 6 months ago

CopyOfA commented 9 months ago

I am using plotnine 0.12.1. Here is my code that results in this error:

import pandas as pd
import plotnine as pn

(
    pn.ggplot(
        data=df, #pandas dataframe
        mapping=pn.aes(
            x="pairs,
            y="agents",
            fill="team",
            color="team",
        ),
    )
    + pn.geom_col(show_legend=False)
    + pn.geom_point(size=0.5, show_legend=False)
    + pn.scale_x_log10()
    + pn.scale_y_discrete()
    + pn.facet_wrap(facets="team", ncol=1)
    + pn.labs(x="pairs", y="gents")
)

The full error is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/miniconda3/envs/venv/lib/python3.11/site-packages/plotnine/ggplot.py", line 114, in __repr__
    figure = self.draw(show=True)
             ^^^^^^^^^^^^^^^^^^^^
  File "~/miniconda3/envs/venv/lib/python3.11/site-packages/plotnine/ggplot.py", line 224, in draw
    self._build()
  File "~/miniconda3/envs/venv/lib/python3.11/site-packages/plotnine/ggplot.py", line 325, in _build
    layout.map_position(layers)
  File "~/miniconda3/envs/venv/lib/python3.11/site-packages/plotnine/facets/layout.py", line 137, in map_position
    self.panel_scales_y.map(data, y_vars, SCALE_Y)
  File "~/miniconda3/envs/venv/lib/python3.11/site-packages/plotnine/scales/scales.py", line 181, in map
    results = sc.map(data.loc[bool_idx, col])
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/miniconda3/envs/venv/lib/python3.11/site-packages/plotnine/scales/scale_xy.py", line 82, in map
    limits = self.limits
             ^^^^^^^^^^^
  File "~/miniconda3/envs/venv/lib/python3.11/site-packages/plotnine/scales/scale_xy.py", line 108, in limits
    return self.range.range
           ^^^^^^^^^^^^^^^^
AttributeError: 'RangeDiscrete' object has no attribute 'range'
has2k1 commented 9 months ago

What version of mizani do you have installed?

CopyOfA commented 9 months ago

What version of mizani do you have installed?

0.9.2

CopyOfA commented 8 months ago

Any update on this?

has2k1 commented 6 months ago

This must be an environment issue. I was unable to reproduce it when installing from pip.

If someone still encounters this, please include minimal installation steps to reproduce it.

knl commented 1 month ago

@CopyOfA maybe your issue is related to https://github.com/has2k1/plotnine/issues/708? I came here for the same error, but as I had integers, switching to scale_y_continuous fixed the issue for me.