Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotting not interacting well with Altair schema wrappers (x=alt.X(...)) #19159

Closed
2 tasks done
AdrienDart opened this issue Oct 9, 2024 · 4 comments · Fixed by #19213
Closed
2 tasks done

Plotting not interacting well with Altair schema wrappers (x=alt.X(...)) #19159

AdrienDart opened this issue Oct 9, 2024 · 4 comments · Fixed by #19213
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@AdrienDart
Copy link

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

import polars as pl
import altair as alt
import numpy as np

np.random.seed (42) 

df =pl.DataFrame({
"value": np.random.normal(0,1,1000)
})

df.plot.bar(x=alt.X("value", bin=alt.BinParams(step=1)), y="count()") # bad behavior

alt.Chart(df).mark_bar().encode(x=alt.X("value", bin=alt.BinParams(step=1)), y="count()") # good behavior (similar to hvplot.hist)

Log output

No response

Issue description

Polars plot not using the altair wrapper the right way.

Expected behavior

We should get a usual histogram plot.

Installed versions

Polars 1.9.0
Python 3.9.13
@AdrienDart AdrienDart added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Oct 9, 2024
@MarcoGorelli
Copy link
Collaborator

Thanks for the report, will take a look

@dangotbanned
Copy link
Contributor

Following up as promised in vega/altair#3635 (comment)

The issue is stemming from #18625 (comment)

Retrospective read

@joelostblom flagged _add_tooltip(), providing link as an alternative.
However, that appears to be pointing to the wrong place, it should be api.py#L3994-L3999

Applying that change would have looked like:

Expected diff

def _add_tooltip(chart: alt.Chart, /) -> alt.Chart:
-    chart.mark = {"type": chart.mark, "tooltip": True}
+    if isinstance(chart.mark, str):
+        chart.mark = {"type": chart.mark, "tooltip": True}
+    else:
+        chart.mark.tooltip = True
     return chart

In the same thread, I was focusing on the testing aspect in (#18625 (comment)) and (#18625 (review))

I definitely overlooked the change from alt.Chart.mark -> alt.Chart.encoding, really dropped the ball on that one.
Sorry @MarcoGorelli

Suggestion

As mentioned in vega/altair#3635 (comment), simply pass in tooltip=True for calls like

self._chart.mark_...(tooltip=True).encode(**encodings, **kwargs).interactive()

I don't think the consideration of alt.Chart.mark's type is relevant for you; since you're simply calling the alt.Chart.mark_... method(s) directly.

You would only need to handle that case if you parameterized this:

class DataFramePlot:
"""DataFrame.plot namespace."""
def __init__(self, df: DataFrame) -> None:
self._chart = alt.Chart(df)

Like:

alt.Chart(df, mark="bar")

@MarcoGorelli
Copy link
Collaborator

thanks so much @dangotbanned ! can confirm this works well

@AdrienDart
Copy link
Author

Thank you @MarcoGorelli @dangotbanned for looking into this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants