has2k1 / plotnine

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

brewer_pal() not working #768

Closed benoit-decorte closed 2 months ago

benoit-decorte commented 3 months ago

Hello,

I'm not sure this is an known issue (couldn't find an equivalent). Since the 2 last versions, the brewer_pal() function does not seem to correctly work, making related functions such as scale_fill_gradient() or scale_fill_brewer() not working.

image

Would it be a dependency issue?

has2k1 commented 3 months ago

You may have a corrupted dependencies. How about using brewer_pal directly e.g.

from mizani.palettes import brewer_pal
brewer_pal("seq", palette=1)(7)
chrismbaapp commented 3 months ago

image

Same issue here. I also have an issue with hue_pal(). Error: TypeError: hue_pal() takes no arguments

has2k1 commented 3 months ago

Same issue here. I also have an issue with hue_pal(). Error: TypeError: hue_pal() takes no arguments

@chrismbaapp What version of mizani do you have installed?

What is the output of

from mizani.palettes import brewer_pal
brewer_pal("seq", palette=1)(7)

It also seems like this is only happening for conda installs!

kevbrick commented 1 month ago

I get a similar error when trying to use color or fill aesthetics:

This is from a fresh python venv install of plotnine0.13.5 and mizani 0.11.4.

(
    ggplot(df_onemers, aes(x='dist')) + geom_bar(aes(color='lbl')) 
    + facet_wrap('lot + ":" + str_rid + ":" +lbl')
)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/IPython/core/formatters.py:922, in IPythonDisplayFormatter.__call__(self, obj)
    920 method = get_real_method(obj, self.print_method)
    921 if method is not None:
--> 922     method()
    923     return True

File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/plotnine/ggplot.py:141, in ggplot._ipython_display_(self)
    134 def _ipython_display_(self):
    135     """
    136     Display plot in the output of the cell
    137 
    138     This method will always be called when a ggplot object is the
    139     last in the cell.
    140     """
--> 141     self._display()

File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/plotnine/ggplot.py:175, in ggplot._display(self)
    172     save_format = "png"
    174 buf = BytesIO()
--> 175 self.save(buf, format=save_format, verbose=False)
    176 display_func = get_display_function(format)
    177 display_func(buf.getvalue())

File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/plotnine/ggplot.py:663, in ggplot.save(self, filename, format, path, width, height, units, dpi, limitsize, verbose, **kwargs)
    615 def save(
    616     self,
    617     filename: Optional[str | Path | BytesIO] = None,
   (...)
    626     **kwargs: Any,
    627 ):
    628     """
    629     Save a ggplot object as an image file
    630 
   (...)
    661         Additional arguments to pass to matplotlib `savefig()`.
    662     """
--> 663     sv = self.save_helper(
    664         filename=filename,
    665         format=format,
    666         path=path,
    667         width=width,
    668         height=height,
    669         units=units,
    670         dpi=dpi,
    671         limitsize=limitsize,
    672         verbose=verbose,
    673         **kwargs,
    674     )
    676     with plot_context(self).rc_context:
    677         sv.figure.savefig(**sv.kwargs)

File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/plotnine/ggplot.py:612, in ggplot.save_helper(self, filename, format, path, width, height, units, dpi, limitsize, verbose, **kwargs)
    609 if dpi is not None:
    610     self.theme = self.theme + theme(dpi=dpi)
--> 612 figure = self.draw(show=False)
    613 return mpl_save_view(figure, fig_kwargs)

File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/plotnine/ggplot.py:272, in ggplot.draw(self, show)
    270 self = deepcopy(self)
    271 with plot_context(self, show=show):
--> 272     self._build()
    274     # setup
    275     self.figure, self.axs = self.facet.setup(self)

File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/plotnine/ggplot.py:362, in ggplot._build(self)
    358 layout.setup(layers, self)
    360 # Compute aesthetics to produce data with generalised
    361 # variable names
--> 362 layers.compute_aesthetics(self)
    364 # Transform data using all scales
    365 layers.transform(scales)

File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/plotnine/layer.py:463, in Layers.compute_aesthetics(self, plot)
    461 def compute_aesthetics(self, plot: ggplot):
    462     for l in self:
--> 463         l.compute_aesthetics(plot)

File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/plotnine/layer.py:262, in layer.compute_aesthetics(self, plot)
    260 evaled = evaluate(self.mapping._starting, self.data, plot.environment)
    261 evaled_aes = aes(**{str(col): col for col in evaled})
--> 262 plot.scales.add_defaults(evaled, evaled_aes)
    264 if len(self.data) == 0 and len(evaled) > 0:
    265     # No data, and vectors suppled to aesthetics
    266     evaled["PANEL"] = 1

File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/plotnine/scales/scales.py:284, in Scales.add_defaults(self, data, aesthetics)
    282 seen.add(scale_var)
    283 try:
--> 284     sc = make_scale(scale_var, data[col])
    285 except PlotnineError:
    286     # Skip aesthetics with no scales (e.g. group, order, etc)
    287     continue

File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/plotnine/scales/scales.py:351, in make_scale(ae, series, *args, **kwargs)
    349 scale_name = f"scale_{ae}_{stype}"
    350 scale_klass = Registry[scale_name]
--> 351 return scale_klass(*args, **kwargs)

File /home/xx/venv/plotnine_test/lib/python3.9/site-packages/plotnine/scales/scale_color.py:57, in scale_color_hue.__init__(self, h, l, s, color_space, **kwargs)
     47 def __init__(
     48     self,
     49     h: float = 0.01,
   (...)
     53     **kwargs,
     54 ):
     55     from mizani.palettes import hue_pal
---> 57     self._palette = hue_pal(h, l, s, color_space=color_space)
     58     scale_discrete.__init__(self, **kwargs)

TypeError: hue_pal() takes no arguments