pola-rs / polars

Dataframes powered by a multithreaded, vectorized query engine, written in Rust
https://docs.pola.rs
Other
30.26k stars 1.96k forks source link

bug: plotting breaks when `axis` is passed to `alt.X` #18830

Closed MarcoGorelli closed 1 month ago

MarcoGorelli commented 1 month ago

Checks

Reproducible example

In [8]: df = pl.DataFrame({'a':[1,2,3], 'b':[4,5,6], 'c': [7,8,9]})

In [9]: df.plot.line(x=alt.X('a', axis=alt.Axis(labelAngle=-90))).to_json()
---------------------------------------------------------------------------
SchemaValidationError                     Traceback (most recent call last)
Cell In[9], line 1
----> 1 df.plot.line(x=alt.X('a', axis=alt.Axis(labelAngle=-90))).to_json()

File ~/scratch/.venv/lib/python3.12/site-packages/altair/vegalite/v5/api.py:1899, in TopLevelMixin.to_json(self, validate, indent, sort_keys, format, ignore, context, **kwargs)
   1897 if context is None:
   1898     context = {}
-> 1899 spec = self.to_dict(
   1900     validate=validate, format=format, ignore=ignore, context=context
   1901 )
   1902 return json.dumps(spec, indent=indent, sort_keys=sort_keys, **kwargs)

File ~/scratch/.venv/lib/python3.12/site-packages/altair/vegalite/v5/api.py:3778, in Chart.to_dict(self, validate, format, ignore, context)
   3776     copy.data = core.InlineData(values=[{}])
   3777     return super(Chart, copy).to_dict(**kwds)
-> 3778 return super().to_dict(**kwds)

File ~/scratch/.venv/lib/python3.12/site-packages/altair/vegalite/v5/api.py:1813, in TopLevelMixin.to_dict(self, validate, format, ignore, context)
   1808 context["top_level"] = False
   1810 # TopLevelMixin instance does not necessarily have to_dict defined
   1811 # but due to how Altair is set up this should hold.
   1812 # Too complex to type hint right now
-> 1813 vegalite_spec: Any = super(TopLevelMixin, copy).to_dict(  # type: ignore[misc]
   1814     validate=validate, ignore=ignore, context=dict(context, pre_transform=False)
   1815 )
   1817 # TODO: following entries are added after validation. Should they be validated?
   1818 if is_top_level:
   1819     # since this is top-level we add $schema if it's missing

File ~/scratch/.venv/lib/python3.12/site-packages/altair/utils/schemapi.py:1076, in SchemaBase.to_dict(self, validate, ignore, context)
   1069         self.validate(result)
   1070     except jsonschema.ValidationError as err:
   1071         # We do not raise `from err` as else the resulting
   1072         # traceback is very long as it contains part
   1073         # of the Vega-Lite schema. It would also first
   1074         # show the less helpful ValidationError instead of
   1075         # the more user friendly SchemaValidationError
-> 1076         raise SchemaValidationError(self, err) from None
   1077 return result

SchemaValidationError: `Tooltip` has no parameter named 'axis'

Existing parameter names are:
shorthand      bin         format       title
aggregate      condition   formatType   type
bandPosition   field       timeUnit

See the help for `Tooltip` to read the full description of these parameters

Log output

No response

Issue description

this raises

Expected behavior

it shouldn't raise

Installed versions

``` --------Version info--------- Polars: 1.7.1 Index type: UInt32 Platform: Linux-5.15.153.1-microsoft-standard-WSL2-x86_64-with-glibc2.35 Python: 3.12.5 (main, Aug 14 2024, 05:08:31) [Clang 18.1.8 ] ----Optional dependencies---- adbc_driver_manager altair 5.4.1 cloudpickle connectorx deltalake fastexcel fsspec gevent great_tables 0.11.0 matplotlib nest_asyncio 1.6.0 numpy 2.1.1 openpyxl pandas 2.2.2 pyarrow 17.0.0 pydantic pyiceberg sqlalchemy torch xlsx2csv xlsxwriter ```
AdrienDart commented 1 month ago

Thank you for creating the issue and looking into it. FYI, I also got similar issues with .line(..., color=alt.Color(...).scale(...)) where parameter "scale" gets the same error

bzm3r commented 1 month ago

I can confirm this issue: https://stackoverflow.com/questions/79010541

Also, whatever broke happened recently, because just about two to three weeks ago, this code worked okay.

MarcoGorelli commented 1 month ago

yup, it regressed in https://github.com/pola-rs/polars/pull/18625, may be fixed by https://github.com/pola-rs/polars/pull/18836