diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dba830298..02e64b5725 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 0000000000..4cda7b34e4 --- /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("