Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
86 changes: 86 additions & 0 deletions plotly/matplotlylib/tests/test_deprecation.py
Original file line number Diff line number Diff line change
@@ -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("<div")


def test_mpl_to_plotly_calls_matplotlylib_when_installed(fig, matplotlylib_installed):
calls, result = matplotlylib_installed
with warnings.catch_warnings():
warnings.simplefilter("error", FutureWarning)
plotly_fig = tls.mpl_to_plotly(fig, resize=True, verbose=True)
assert plotly_fig is result
assert calls == [(fig, True, False, True)]


def test_plot_mpl_calls_matplotlylib_when_installed(fig, matplotlylib_installed):
calls, _ = matplotlylib_installed
with warnings.catch_warnings():
warnings.simplefilter("error", FutureWarning)
plotly.offline.plot_mpl(
fig, strip_style=True, output_type="div", include_plotlyjs=False
)
assert calls == [(fig, False, True, False)]
17 changes: 17 additions & 0 deletions plotly/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
def mpl_to_plotly(fig, resize=False, strip_style=False, verbose=False):
"""Convert a matplotlib figure to plotly dictionary and send.

When the matplotlylib package (https://github.com/plotly/matplotlylib) is
installed, this function returns
`matplotlylib.mpl_to_plotly(fig, resize, strip_style, verbose)`.

All available information about matplotlib visualizations are stored
within a matplotlib.figure.Figure object. You can create a plot in python
using matplotlib, store the figure object, and then pass this object to
Expand Down Expand Up @@ -90,6 +94,19 @@ def mpl_to_plotly(fig, resize=False, strip_style=False, verbose=False):
renderer.layout -- a plotly layout dictionary
renderer.data -- a list of plotly data dictionaries
"""
matplotlylib = optional_imports.get_module("matplotlylib")
if matplotlylib:
return matplotlylib.mpl_to_plotly(
fig, resize=resize, strip_style=strip_style, verbose=verbose
)
warnings.warn(
"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",
FutureWarning,
stacklevel=2,
)
matplotlylib = optional_imports.get_module("plotly.matplotlylib")
if matplotlylib:
renderer = matplotlylib.PlotlyRenderer()
Expand Down