Skip to content

API: Add entrypoint for plotting #27488

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

Merged
merged 11 commits into from
Jul 25, 2019
17 changes: 17 additions & 0 deletions doc/source/development/extending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,22 @@ This would be more or less equivalent to:
The backend module can then use other visualization tools (Bokeh, Altair,...)
to generate the plots.

Libraries implementing the plotting backend should use `entry points <https://setuptools.readthedocs.io/en/latest/setuptools.html#dynamic-discovery-of-services-and-plugins>`__
to make their backend discoverable to pandas. The key is ``"pandas_plotting_backends"``. For example, pandas
registers the default "matplotlib" backend as follows.

.. code-block:: python

# in setup.py
setup(
...
entry_points={
"pandas_plotting_backends": [
"matplotlib = pandas:plotting._matplotlib",
],
},
)


More information on how to implement a third-party plotting backend can be found at
https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/__init__.py#L1.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ I/O
Plotting
^^^^^^^^

-
- Added a pandas_plotting_backends entrypoint group for registering plot backends. See :ref:`extending.plotting-backends` for more (:issue:`26747`).
-
-

Expand Down
35 changes: 30 additions & 5 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,10 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
return self(kind="hexbin", x=x, y=y, C=C, **kwargs)


def _get_plot_backend(backend=None):
_backends = {}


def _get_plot_backend(backend="matplotlib"):
"""
Return the plotting backend to use (e.g. `pandas.plotting._matplotlib`).

Expand All @@ -1546,7 +1549,29 @@ def _get_plot_backend(backend=None):
The backend is imported lazily, as matplotlib is a soft dependency, and
pandas can be used without it being installed.
"""
backend_str = backend or pandas.get_option("plotting.backend")
if backend_str == "matplotlib":
backend_str = "pandas.plotting._matplotlib"
return importlib.import_module(backend_str)
import pkg_resources # Delay import for performance.

if backend in _backends:
return _backends[backend]

if backend == "matplotlib":
# Because matplotlib is an optional dependency and first party backend,
# we need to attempt an import here. Without this import, we get an
# AttributeError raised by pkg_resources.
import matplotlib # noqa

for entry_point in pkg_resources.iter_entry_points("pandas_plotting_backends"):
_backends[entry_point.name] = entry_point.load()

try:
return _backends[backend]
except KeyError:
try:
module = importlib.import_module(backend)
except ImportError:
pass
else:
_backends[backend] = module
return module

raise ValueError("No backend {}".format(backend))
47 changes: 47 additions & 0 deletions pandas/tests/plotting/test_backend.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import sys
import types

import pkg_resources
import pytest

import pandas.util._test_decorators as td

import pandas


Expand Down Expand Up @@ -36,3 +42,44 @@ def test_backend_is_correct(monkeypatch):
pandas.set_option("plotting.backend", "matplotlib")
except ImportError:
pass


@td.skip_if_no_mpl
def test_register_entrypoint():
Copy link
Member

Choose a reason for hiding this comment

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

this is failing for me locally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What are you seeing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I'm guessing it's a KeyError: 'pandas_plotting_backends'?

You'll need to re-run python -m pip install -e . in your pandas directory. This adds the entrypoint.

Copy link
Member

Choose a reason for hiding this comment

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

Yep that does fix it, thanks

mod = types.ModuleType("my_backend")
mod.plot = lambda *args, **kwargs: 1

backends = pkg_resources.get_entry_map("pandas")
my_entrypoint = pkg_resources.EntryPoint(
"pandas_plotting_backend",
mod.__name__,
dist=pkg_resources.get_distribution("pandas"),
)
backends["pandas_plotting_backends"]["my_backend"] = my_entrypoint
# TODO: the docs recommend importlib.util.module_from_spec. But this works for now.
sys.modules["my_backend"] = mod

result = pandas.plotting._core._get_plot_backend("my_backend")
assert result is mod


@td.skip_if_no_mpl
def test_register_import():
mod = types.ModuleType("my_backend2")
mod.plot = lambda *args, **kwargs: 1
sys.modules["my_backend2"] = mod

result = pandas.plotting._core._get_plot_backend("my_backend2")
assert result is mod


def test_no_matplotlib_ok():
try:
import matplotlib # noqa
except ImportError:
pass
else:
raise pytest.skip("matplotlib installed.")

with pytest.raises(ImportError):
pandas.plotting._core._get_plot_backend("matplotlib")
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,5 +830,10 @@ def srcpath(name=None, suffix=".pyx", subdir="src"):
"hypothesis>=3.58",
]
},
entry_points={
"pandas_plotting_backends": [
"matplotlib = pandas:plotting._matplotlib",
],
},
**setuptools_kwargs
)