Skip to content

Commit eb168b7

Browse files
TomAugspurgerjorisvandenbossche
authored andcommitted
ERR: Better error message for missing matplotlib (#20538)
Closes #19810
1 parent 6d610a4 commit eb168b7

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ I/O
11031103
Plotting
11041104
^^^^^^^^
11051105

1106+
- Better error message when attempting to plot but matplotlib is not installed (:issue:`19810`).
11061107
- :func:`DataFrame.plot` now raises a ``ValueError`` when the ``x`` or ``y`` argument is improperly formed (:issue:`18671`)
11071108
- Bug in :func:`DataFrame.plot` when ``x`` and ``y`` arguments given as positions caused incorrect referenced columns for line, bar and area plots (:issue:`20056`)
11081109
- Bug in formatting tick labels with ``datetime.time()`` and fractional seconds (:issue:`18478`).

pandas/plotting/_core.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,19 @@
4444
try:
4545
from pandas.plotting import _converter
4646
except ImportError:
47-
pass
47+
_HAS_MPL = False
4848
else:
49+
_HAS_MPL = True
4950
if get_option('plotting.matplotlib.register_converters'):
5051
_converter.register(explicit=True)
5152

5253

54+
def _raise_if_no_mpl():
55+
# TODO(mpl_converter): remove once converter is explicit
56+
if not _HAS_MPL:
57+
raise ImportError("matplotlib is required for plotting.")
58+
59+
5360
def _get_standard_kind(kind):
5461
return {'density': 'kde'}.get(kind, kind)
5562

@@ -97,6 +104,7 @@ def __init__(self, data, kind=None, by=None, subplots=False, sharex=None,
97104
secondary_y=False, colormap=None,
98105
table=False, layout=None, **kwds):
99106

107+
_raise_if_no_mpl()
100108
_converter._WARN = False
101109
self.data = data
102110
self.by = by
@@ -2378,6 +2386,7 @@ def hist_frame(data, column=None, by=None, grid=True, xlabelsize=None,
23782386
... }, index= ['pig', 'rabbit', 'duck', 'chicken', 'horse'])
23792387
>>> hist = df.hist(bins=3)
23802388
"""
2389+
_raise_if_no_mpl()
23812390
_converter._WARN = False
23822391
if by is not None:
23832392
axes = grouped_hist(data, column=column, by=by, ax=ax, grid=grid,
@@ -2517,6 +2526,7 @@ def grouped_hist(data, column=None, by=None, ax=None, bins=50, figsize=None,
25172526
-------
25182527
axes: collection of Matplotlib Axes
25192528
"""
2529+
_raise_if_no_mpl()
25202530
_converter._WARN = False
25212531

25222532
def plot_group(group, ax):
@@ -2583,6 +2593,7 @@ def boxplot_frame_groupby(grouped, subplots=True, column=None, fontsize=None,
25832593
>>> grouped = df.unstack(level='lvl1').groupby(level=0, axis=1)
25842594
>>> boxplot_frame_groupby(grouped, subplots=False)
25852595
"""
2596+
_raise_if_no_mpl()
25862597
_converter._WARN = False
25872598
if subplots is True:
25882599
naxes = len(grouped)

pandas/tests/plotting/test_misc.py

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
from pandas.tests.plotting.common import TestPlotBase, _check_plot_works
1818

1919

20+
@td.skip_if_mpl
21+
def test_import_error_message():
22+
# GH-19810
23+
df = DataFrame({"A": [1, 2]})
24+
25+
with tm.assert_raises_regex(ImportError, 'matplotlib is required'):
26+
df.plot()
27+
28+
2029
@td.skip_if_no_mpl
2130
class TestSeriesPlots(TestPlotBase):
2231

pandas/util/_test_decorators.py

+2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ def decorated_func(func):
160160

161161
skip_if_no_mpl = pytest.mark.skipif(_skip_if_no_mpl(),
162162
reason="Missing matplotlib dependency")
163+
skip_if_mpl = pytest.mark.skipif(not _skip_if_no_mpl(),
164+
reason="matplotlib is present")
163165
skip_if_mpl_1_5 = pytest.mark.skipif(_skip_if_mpl_1_5(),
164166
reason="matplotlib 1.5")
165167
xfail_if_mpl_2_2 = pytest.mark.xfail(_skip_if_mpl_2_2(),

0 commit comments

Comments
 (0)