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

Fix calling special Pandas plotting methods with the hvplot backend #1491

Merged
merged 6 commits into from
Feb 6, 2025

Conversation

maximlt
Copy link
Member

@maximlt maximlt commented Feb 5, 2025

Fixes #1483

When a plotting backend is configured on pandas via e.g. pd.options.plotting.backend = '<backend>', Pandas has two ways to use it:

  1. it will try to see if it is registered as an entry point and if so will load the registered module (holoviews = "hvplot:plotting" registers holoviews as an entry point pointing to the hvplot.plotting module).
  2. if 1/ doesn't succeed, it will load the module as is

In both cases, the only check Pandas performs is to verify that the backend has a plot attribute.

#347 exposed the plot function from hvplot.plotting in the top-level module, allowing users to register the backend with pd.options.plotting.backend = 'hvplot' (leveraging 2/). However, that is not enough to give users access to the special plot methods Pandas expects like boxplot or hist.

This PR fixes that by adding another hvplot entry point.

@maximlt maximlt marked this pull request as draft February 5, 2025 09:41
@maximlt maximlt marked this pull request as ready for review February 5, 2025 11:09
@@ -45,6 +45,7 @@ dependencies = [

[project.entry-points."pandas_plotting_backends"]
holoviews = "hvplot:plotting"
hvplot = "hvplot:plotting"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix.

@maximlt maximlt requested a review from Azaya89 February 6, 2025 09:17
@Azaya89
Copy link
Collaborator

Azaya89 commented Feb 6, 2025

There are multiple ways a hist plot can be plotted and some of them still don't work while the ones that work give different outputs. I checked out this branch and tried them:

Some data

import numpy as np
import pandas as pd

np.random.seed(123456)

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
  1. Using hvplot.pandas works
import hvplot.pandas

ts.hvplot.hist()

Default number of bins = 20
image

  1. Setting the plotting backend
    2a.
pd.options.plotting.backend = 'hvplot'

ts.hist()
AttributeError                            Traceback (most recent call last)
Cell In[3], line 3
      1 pd.options.plotting.backend = 'hvplot'
----> 3 ts.hist()

File ~/hvplot/.pixi/envs/default/lib/python3.12/site-packages/pandas/plotting/_core.py#line=128, in hist_series(self, by, ax, grid, xlabelsize, xrot, ylabelsize, yrot, figsize, bins, backend, legend, **kwargs)
     62 """
     63 Draw histogram of the input series using matplotlib.
     64 
   (...)
    126     >>> hist = ser.groupby(level=0).hist()
    127 """
    128 plot_backend = _get_plot_backend(backend)
--> 129 return plot_backend.hist_series(
    130     self,
    131     by=by,
    132     ax=ax,
    133     grid=grid,
    134     xlabelsize=xlabelsize,
    135     xrot=xrot,
    136     ylabelsize=ylabelsize,
    137     yrot=yrot,
    138     figsize=figsize,
    139     bins=bins,
    140     legend=legend,
    141     **kwargs,
    142 )

AttributeError: module 'hvplot' has no attribute 'hist_series'

2b. Prepending with plot works

pd.options.plotting.backend = 'hvplot'

ts.plot.hist()

Default number of bins = 10
image

@maximlt
Copy link
Member Author

maximlt commented Feb 6, 2025

Did you re-install hvplot in your environment? I think it needs to be done for the new entry-point to be registered.

@Azaya89
Copy link
Collaborator

Azaya89 commented Feb 6, 2025

Did you re-install hvplot in your environment? I think it needs to be done for the new entry-point to be registered.

Thank you. I didn't know that.

@maximlt
Copy link
Member Author

maximlt commented Feb 6, 2025

Did you re-install hvplot in your environment? I think it needs to be done for the new entry-point to be registered.

Thank you. I didn't know that.

Relieved to see it works as it's what is tested with https://github.com/holoviz/hvplot/pull/1491/files#diff-8d066bf9f51083145a38e3d3346ad7f86ac33f994b9008217f2cc92a91aa0b05R72-R77

@maximlt maximlt merged commit 1cf9e0e into main Feb 6, 2025
11 checks passed
@maximlt maximlt deleted the fix_pandas_backend_hvplot branch February 6, 2025 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using the hvplot backend for a hist() plot shows an AttributeError
2 participants