From c6c85afa10fe6ecf32c6b6630171ad99594ddafa Mon Sep 17 00:00:00 2001 From: Roberto Moura Date: Fri, 4 Dec 2026 14:30:53 +0000 Subject: [PATCH] Use matplotlylib in mpl_to_plotly when installed, warn otherwise The matplotlib converter is moving to the separate matplotlylib package (https://github.com/plotly/matplotlylib). When matplotlylib is importable, plotly.tools.mpl_to_plotly returns matplotlylib.mpl_to_plotly(fig, resize, strip_style, verbose), so plot_mpl, iplot_mpl and enable_mpl_offline, which call it, use the new package too. Otherwise it emits a FutureWarning asking users to install matplotlylib and import mpl_to_plotly from it, and converts with the bundled copy. --- CHANGELOG.md | 2 + plotly/matplotlylib/tests/test_deprecation.py | 86 +++++++++++++++++++ plotly/tools.py | 17 ++++ 3 files changed, 105 insertions(+) create mode 100644 plotly/matplotlylib/tests/test_deprecation.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dba830298e..02e64b57259 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +### Deprecated +- Deprecate the matplotlib converter bundled with plotly.py in favor of the separate [`matplotlylib`](https://github.com/plotly/matplotlylib) package. `plotly.tools.mpl_to_plotly`, and the `plotly.offline` functions that call it, use `matplotlylib.mpl_to_plotly` when `matplotlylib` is installed, and otherwise emit a `FutureWarning` and use the bundled converter. ## [7.2.0] - 2026-11-24 diff --git a/plotly/matplotlylib/tests/test_deprecation.py b/plotly/matplotlylib/tests/test_deprecation.py new file mode 100644 index 00000000000..4cda7b34e43 --- /dev/null +++ b/plotly/matplotlylib/tests/test_deprecation.py @@ -0,0 +1,86 @@ +import sys +import types +import warnings + +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt +import pytest + +import plotly.graph_objects as go +import plotly.offline +import plotly.tools as tls +from _plotly_utils import optional_imports + +MESSAGE = ( + "plotly.tools.mpl_to_plotly is deprecated and will be removed in the future. " + "Install matplotlylib (pip install matplotlylib) and use " + "`from matplotlylib import mpl_to_plotly` instead. " + "See https://github.com/plotly/matplotlylib" +) + + +@pytest.fixture +def fig(): + fig, ax = plt.subplots() + ax.plot([0, 1], [0, 2]) + yield fig + plt.close(fig) + + +@pytest.fixture +def matplotlylib_missing(monkeypatch): + monkeypatch.setattr(optional_imports, "_not_importable", set()) + monkeypatch.setitem(sys.modules, "matplotlylib", None) + + +@pytest.fixture +def matplotlylib_installed(monkeypatch): + calls = [] + result = go.Figure() + + def mpl_to_plotly(fig, resize=False, strip_style=False, verbose=False): + calls.append((fig, resize, strip_style, verbose)) + return result + + module = types.ModuleType("matplotlylib") + module.mpl_to_plotly = mpl_to_plotly + monkeypatch.setattr(optional_imports, "_not_importable", set()) + monkeypatch.setitem(sys.modules, "matplotlylib", module) + return calls, result + + +def test_mpl_to_plotly_warns_when_matplotlylib_is_missing(fig, matplotlylib_missing): + with pytest.warns(FutureWarning) as record: + plotly_fig = tls.mpl_to_plotly(fig) + future_warnings = [w for w in record if w.category is FutureWarning] + assert [str(w.message) for w in future_warnings] == [MESSAGE] + assert future_warnings[0].filename == __file__ + assert isinstance(plotly_fig, go.Figure) + assert list(plotly_fig.data[0].y) == [0, 2] + + +def test_plot_mpl_warns_when_matplotlylib_is_missing(fig, matplotlylib_missing): + with pytest.warns(FutureWarning, match="plotly.tools.mpl_to_plotly is deprecated"): + div = plotly.offline.plot_mpl(fig, output_type="div", include_plotlyjs=False) + assert div.startswith("