Skip to content

enh: support DataFrame.plot.hist #18639

Description

@fonnesbeck

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 numpy as np

# Simulated DataFrame
np.random.seed(0)
data = {
    "shotRebound": np.random.randint(0, 2, size=1000)  # Simulating shot rebounds (0 or 1)
}
simulated_df = pl.DataFrame(data)

# Plotting the histogram
simulated_df.select(pl.col("shotRebound")).plot.hist(bins=30, title="Histogram of Shot Rebounds", xlabel="Shot Rebound", ylabel="Frequency")

Log output

AttributeError                            Traceback (most recent call last)
Cell In[26], line 12
      9 simulated_df = pl.DataFrame(data)
     11 # Plotting the histogram
---> 12 simulated_df.select(pl.col("shotRebound")).plot.hist(bins=30, title="Histogram of Shot Rebounds", xlabel="Shot Rebound", ylabel="Frequency")

File ~/repos/xg_model/.pixi/envs/default/lib/python3.12/site-packages/polars/dataframe/plotting.py:255, in DataFramePlot.__getattr__(self, attr)
    253 if method is None:
    254     msg = "Altair has no method 'mark_{attr}'"
--> 255     raise AttributeError(msg)
    256 return lambda **kwargs: method().encode(**kwargs).interactive()

AttributeError: Altair has no method 'mark_{attr}'

Issue description

Despite up-to-date versions of polars and altair being installed, plotting a histogram fails, apparently due to incompatibility between the two libraries.

Expected behavior

Plotted histogram

Installed versions

Details
--------Version info---------
Polars:              1.6.0
Index type:          UInt32
Platform:            Linux-6.9.12-200.fc40.x86_64-x86_64-with-glibc2.39
Python:              3.12.5 | packaged by conda-forge | (main, Aug  8 2024, 18:36:51) [GCC 12.4.0]

----Optional dependencies----
adbc_driver_manager  <not installed>
altair               5.4.1
cloudpickle          3.0.0
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               <not installed>
gevent               <not installed>
great_tables         <not installed>
matplotlib           3.9.2
nest_asyncio         1.6.0
numpy                1.26.4
openpyxl             <not installed>
pandas               2.2.2
pyarrow              17.0.0
pydantic             <not installed>
pyiceberg            <not installed>
sqlalchemy           <not installed>
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>

Metadata

Metadata

Assignees

No one assigned

    Labels

    pythonRelated to Python Polars

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions