Skip to content

0.15.0 - geom_sina and facet_grid results in ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() #979

@benif

Description

@benif

When using geom_sina and facet_grid together in plotnine 0.15.0, ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() is thrown. Rolling back to 0.14.6 results in no error.

Code to reproduce issue:

from sklearn import datasets
iris = datasets.load_iris()
iris_df = pd.DataFrame(iris.data, columns = iris.feature_names)
iris_df['species'] = iris.target_names[iris.target]

(
  ggplot(data = iris_df, mapping = aes(x = "species", y = "sepal length (cm)")) +
  facet_grid(cols = "species") +
  geom_sina(mapping = aes(color = "species"))
)

The error message is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File /data/miniconda3/envs/ml/lib/python3.10/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 /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/ggplot.py:148, in ggplot._ipython_display_(self)
    141 def _ipython_display_(self):
    142     """
    143     Display plot in the output of the cell
    144 
    145     This method will always be called when a ggplot object is the
    146     last in the cell.
    147     """
--> 148     self._display()

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/ggplot.py:189, in ggplot._display(self)
    186     self.theme = self.theme.to_retina()
    188 buf = BytesIO()
--> 189 self.save(buf, "png" if format == "retina" else format, verbose=False)
    190 figure_size_px = self.theme._figure_size_px
    191 display_func = get_display_function(format, figure_size_px)

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/ggplot.py:688, in ggplot.save(self, filename, format, path, width, height, units, dpi, limitsize, verbose, **kwargs)
    639 def save(
    640     self,
    641     filename: Optional[str | Path | BytesIO] = None,
   (...)
    650     **kwargs: Any,
    651 ):
    652     """
    653     Save a ggplot object as an image file
    654 
   (...)
    686         Additional arguments to pass to matplotlib `savefig()`.
    687     """
--> 688     sv = self.save_helper(
    689         filename=filename,
    690         format=format,
    691         path=path,
    692         width=width,
    693         height=height,
    694         units=units,
    695         dpi=dpi,
    696         limitsize=limitsize,
    697         verbose=verbose,
    698         **kwargs,
    699     )
    701     with plot_context(self).rc_context:
    702         sv.figure.savefig(**sv.kwargs)

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/ggplot.py:636, in ggplot.save_helper(self, filename, format, path, width, height, units, dpi, limitsize, verbose, **kwargs)
    633 if dpi is not None:
    634     self.theme = self.theme + theme(dpi=dpi)
--> 636 figure = self.draw(show=False)
    637 return mpl_save_view(figure, fig_kwargs)

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/ggplot.py:308, in ggplot.draw(self, show)
    305     self._create_figure()
    306 figure = self.figure
--> 308 self._build()
    310 # setup
    311 self.axs = self.facet.setup(self)

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/ggplot.py:385, in ggplot._build(self)
    382 layout.map_position(layers)
    384 # Apply and map statistics
--> 385 layers.compute_statistic(layout)
    386 layers.map_statistic(self)
    388 # Prepare data in geoms
    389 # e.g. from y and width to ymin and ymax

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/layer.py:480, in Layers.compute_statistic(self, layout)
    478 def compute_statistic(self, layout: Layout):
    479     for l in self:
--> 480         l.compute_statistic(layout)

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/layer.py:293, in layer.compute_statistic(self, layout)
    291 data = self.stat.use_defaults(data)
    292 data = self.stat.setup_data(data)
--> 293 data = self.stat.compute_layer(data, layout)
    294 self.data = data

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/stats/stat.py:303, in stat.compute_layer(self, data, layout)
    300     pscales = layout.get_scales(pdata["PANEL"].iloc[0])
    301     return self.compute_panel(pdata, pscales)
--> 303 return groupby_apply(data, "PANEL", fn)

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/_utils/__init__.py:571, in groupby_apply(df, cols, func, *args, **kwargs)
    567 lst = []
    568 for _, d in df.groupby(cols, observed=True):
    569     # function fn should be free to modify dataframe d, therefore
    570     # do not mark d as a slice of df i.e no SettingWithCopyWarning
--> 571     lst.append(func(d, *args, **kwargs))
    572 return pd.concat(lst, axis=axis, ignore_index=True)

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/stats/stat.py:301, in stat.compute_layer.<locals>.fn(pdata)
    299     return pdata
    300 pscales = layout.get_scales(pdata["PANEL"].iloc[0])
--> 301 return self.compute_panel(pdata, pscales)

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/stats/stat_sina.py:154, in stat_sina.compute_panel(self, data, scales)
    150     params["bins"] = breaks_from_binwidth(
    151         y_dim_fuzzed, params["binwidth"]
    152     )
    153 else:
--> 154     params["bins"] = breaks_from_bins(y_dim_fuzzed, params["bins"])
    156 data = super().compute_panel(data, scales)
    158 if not len(data):

File /data/miniconda3/envs/ml/lib/python3.10/site-packages/plotnine/stats/binning.py:122, in breaks_from_bins(x_range, bins, center, boundary)
     96 def breaks_from_bins(
     97     x_range: tuple[float, float],
     98     bins: int = 30,
     99     center: Optional[float] = None,
    100     boundary: Optional[float] = None,
    101 ):
    102     """
    103     Calculate breaks given binwidth
    104 
   (...)
    120         Sequence of break points.
    121     """
--> 122     if bins < 1:
    123         raise PlotnineError("Need at least one bin.")
    124     elif bins == 1:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions