Skip to content

Allow plotting backend option context without matplotlib #27517

Closed
@TomAugspurger

Description

@TomAugspurger

Right now, using pd.option_context to set the backend fails on __exit__ when matplotlib isn't installed.

In [1]: import pandas as pd

In [2]: with pd.option_context('plotting.backend', 'test'):
   ...:     pass
   ...:
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
~/sandbox/pandas/pandas/core/config_init.py in register_plotting_backend_cb(key)
    593         try:
--> 594             import pandas.plotting._matplotlib  # noqa
    595         except ImportError:

~/sandbox/pandas/pandas/plotting/_matplotlib/__init__.py in <module>
      2
----> 3 from pandas.plotting._matplotlib.boxplot import (
      4     BoxPlot,

~/sandbox/pandas/pandas/plotting/_matplotlib/boxplot.py in <module>
      3
----> 4 from matplotlib.artist import setp
      5 import numpy as np

ModuleNotFoundError: No module named 'matplotlib'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-2-08ebc0f8d5ad> in <module>
      1 with pd.option_context('plotting.backend', 'test'):
----> 2     pass
      3

~/sandbox/pandas/pandas/_config/config.py in __exit__(self, *args)
    410         if self.undo:
    411             for pat, val in self.undo:
--> 412                 _set_option(pat, val, silent=True)
    413
    414

~/sandbox/pandas/pandas/_config/config.py in _set_option(*args, **kwargs)
    134             if silent:
    135                 with warnings.catch_warnings(record=True):
--> 136                     o.cb(key)
    137             else:
    138                 o.cb(key)

~/sandbox/pandas/pandas/core/config_init.py in register_plotting_backend_cb(key)
    595         except ImportError:
    596             raise ImportError(
--> 597                 "matplotlib is required for plotting when the "
    598                 'default backend "matplotlib" is selected.'
    599             )

ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected.

I don't believe that should raise an error. We may need to make the default option None, and then resole that to matplotlib later on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions