Problem using the `geom_col` in plotnine with Linux (Ubuntu 22.04.3 LTS) and Windows

80 Views Asked by At

When I try to run the following reproducible example using plotnine I get an error:

import pandas as pd
from plotnine import (
      ggplot, aes, 
      geom_col
)
from plotnine.data import mtcars


df = (mtcars.
loc[:, ["name", "disp"]].
drop_duplicates(subset="name"))

(ggplot(data=df,
        mapping=aes(x="name", y="disp")) +
 geom_col())

This is the complete error I get:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\Usuario\ANACON~1\Lib\site-packages\plotnine\ggplot.py", line 114, in __repr__
    figure = self.draw(show=True)
             ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Usuario\ANACON~1\Lib\site-packages\plotnine\ggplot.py", line 224, in draw
    self._build()
  File "C:\Users\Usuario\ANACON~1\Lib\site-packages\plotnine\ggplot.py", line 336, in _build
    layers.compute_position(layout)
  File "C:\Users\Usuario\ANACON~1\Lib\site-packages\plotnine\layer.py", line 479, in compute_position
    l.compute_position(layout)
  File "C:\Users\Usuario\ANACON~1\Lib\site-packages\plotnine\layer.py", line 345, in compute_position
    data = self.position.compute_layer(data, params, layout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Usuario\ANACON~1\Lib\site-packages\plotnine\positions\position.py", line 79, in compute_layer
    return groupby_apply(data, "PANEL", fn)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Usuario\ANACON~1\Lib\site-packages\plotnine\utils.py", line 599, in groupby_apply
    lst.append(func(d, *args, **kwargs))
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Usuario\ANACON~1\Lib\site-packages\plotnine\positions\position.py", line 77, in fn
    return cls.compute_panel(pdata, scales, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Usuario\ANACON~1\Lib\site-packages\plotnine\positions\position_stack.py", line 103, in compute_panel
    nl_trans = get_non_linear_trans(scales.y)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Usuario\ANACON~1\Lib\site-packages\plotnine\positions\position_stack.py", line 97, in get_non_linear_trans
    if _is_non_linear_trans(sc.trans):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Usuario\ANACON~1\Lib\site-packages\plotnine\positions\position_stack.py", line 87, in _is_non_linear_trans
    trans.dataspace_is_numerical and tname not in linear_transforms
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'identity_trans' object has no attribute 'dataspace_is_numerical'

My session info is the following:

-----
pandas              2.1.4
plotnine            0.12.2
session_info        1.0.0
-----
Python 3.11.6 | packaged by conda-forge | (main, Oct  3 2023, 10:29:11) [MSC v.1935 64 bit (AMD64)]
Windows-10-10.0.22621-SP0
-----
Session information updated at 2023-12-13 13:38

I think the problem is with Windows taking into account that at least in Linux Ubuntu 22.04.3 LTS you will get some warnings related to the mizani module but it works fine and it will generate the plot.

However in Linux Ubuntu 22.04.3 LTS if you change the variables that corresponds to each axis, x="disp", y="name", you will get a strange plot using the following code:

import pandas as pd
from plotnine import (
      ggplot, aes, 
      geom_col
)
from plotnine.data import mtcars


df = (mtcars.
loc[:, ["name", "disp"]].
drop_duplicates(subset="name"))

(ggplot(data=df,
        mapping=aes(x="disp", y="name")) +
 geom_col())

enter image description here

Where my session info in the case of Linux is the following:

-----
pandas              2.1.1
plotnine            0.12.1
session_info        1.0.0
-----
Python 3.10.9 (main, Mar  1 2023, 18:23:06) [GCC 11.2.0]
Linux-6.2.0-37-generic-x86_64-with-glibc2.35
-----
Session information updated at 2023-12-13 13:53

Anyone knows what will be a possible solution for Windows and how you can fix the plot in Linux?

1

There are 1 best solutions below

0
On BEST ANSWER

Until plotnine learns to infer the orientation of the plot based on the types mapped to the aesthetics, the solution is to use map the discrete column to the x aesthetic then use coord_flip to flip the axes.

import pandas as pd
from plotnine import ggplot, aes, geom_col, coord_flip
from plotnine.data import mtcars

(ggplot(mtcars, aes(x="name", y="disp"))
 + geom_col()
 + coord_flip()
)