Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e4bf042
Port the matplotlylib tests from plotly.py
robertoffmoura Dec 3, 2026
fdf3bbb
Remove the unused NumpyTestUtilsMixin test helper
robertoffmoura Dec 4, 2026
b6efd17
Remove the unused TestCaseNoTemplate test helper
robertoffmoura Dec 4, 2026
600f81b
Remove the setup_package hook that pytest never calls
robertoffmoura Dec 4, 2026
b8d1685
Remove redundant matplotlib backend setup from ported tests
robertoffmoura Dec 4, 2026
ab750f4
Remove debugging leftovers from the ported tests
robertoffmoura Dec 4, 2026
15cea18
Call mpl_to_plotly in the ported tests
robertoffmoura Dec 4, 2026
1ff7fe2
Use plain pytest functions in test_date_times
robertoffmoura Dec 4, 2026
e455eab
Compare numbers with pytest.approx in compare_dict
robertoffmoura Dec 4, 2026
dbfe0d0
Use fixed y values in the pandas date test
robertoffmoura Dec 4, 2026
2bc207a
Take the figure from the pandas plot in the pandas date test
robertoffmoura Dec 4, 2026
0f1511f
Close matplotlib figures after each test
robertoffmoura Dec 4, 2026
abd1afb
Move the ported tests into tests/
robertoffmoura Dec 4, 2026
95066e5
Update the ported tests' setup for current matplotlib
robertoffmoura Dec 4, 2026
167e090
Rewrite test_data with direct assertions
robertoffmoura Dec 4, 2026
cf416c4
Rewrite test_bars with targeted assertions
robertoffmoura Dec 4, 2026
e7a6d5d
Rewrite test_scatter with targeted assertions
robertoffmoura Dec 4, 2026
3ef71a7
Rewrite test_lines with targeted assertions
robertoffmoura Dec 4, 2026
a10d439
Rewrite test_annotations with targeted assertions
robertoffmoura Dec 4, 2026
fe8fd9d
Rewrite test_axis_scales with targeted assertions
robertoffmoura Dec 4, 2026
30f5f84
Rewrite test_subplots with targeted assertions
robertoffmoura Dec 4, 2026
a0eca95
Rewrite test_date_times and mark the pandas date bug as xfail
robertoffmoura Dec 4, 2026
5f11856
Remove the unused tests/utils.py
robertoffmoura Dec 4, 2026
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: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies = [
]

[project.optional-dependencies]
dev = ["pytest", "packaging", "ruff==0.11.12"]
dev = ["pytest", "packaging", "pandas", "ruff==0.11.12"]

[project.urls]
Homepage = "https://github.com/plotly/matplotlylib"
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import matplotlib.pyplot as plt
import pytest


@pytest.fixture(autouse=True)
def close_figures():
"""Close all matplotlib figures after each test."""
yield
plt.close("all")
47 changes: 47 additions & 0 deletions tests/test_annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import matplotlib.pyplot as plt
import pytest

from matplotlylib import mpl_to_plotly


def test_annotations():
fig, ax = plt.subplots()
ax.plot([1, 2, 3], "b-")
ax.plot([3, 2, 1], "b-")
ax.text(0.001, 0.999, "top-left", transform=ax.transAxes, va="top", ha="left")
ax.text(
0.001, 0.001, "bottom-left", transform=ax.transAxes, va="baseline", ha="left"
)
ax.text(0.999, 0.999, "top-right", transform=ax.transAxes, va="top", ha="right")
ax.text(
0.999, 0.001, "bottom-right", transform=ax.transAxes, va="baseline", ha="right"
)

plotly_fig = mpl_to_plotly(fig)

annotations = {a.text: a for a in plotly_fig.layout.annotations}
assert sorted(annotations) == [
"bottom-left",
"bottom-right",
"top-left",
"top-right",
]
expected = {
"top-left": (0.001, 0.999, "left", "top"),
"bottom-left": (0.001, 0.001, "left", "bottom"),
"top-right": (0.999, 0.999, "right", "top"),
"bottom-right": (0.999, 0.001, "right", "bottom"),
}
for text, (x, y, xanchor, yanchor) in expected.items():
annotation = annotations[text]
assert annotation.xref == "paper"
assert annotation.yref == "paper"
assert annotation.showarrow is False
assert annotation.xanchor == xanchor
assert annotation.yanchor == yanchor
assert annotation.align == xanchor
# The plotly plot area is not exactly the size of the matplotlib axes
# (497 vs 496 pixels wide here), so paper coordinates can be off by a
# pixel or two
assert annotation.x == pytest.approx(x, abs=0.005)
assert annotation.y == pytest.approx(y, abs=0.005)
29 changes: 29 additions & 0 deletions tests/test_axis_scales.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import matplotlib.pyplot as plt
import pytest

from matplotlylib import mpl_to_plotly


def test_even_linear_scale():
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [10, 3, 100, 6, 45, 4, 80, 45, 3, 59])
# Minor ticks beyond the data widen the view limits
ax.set_xticks(list(range(0, 20, 3)), minor=True)
ax.set_yticks(list(range(0, 200, 13)), minor=True)

plotly_fig = mpl_to_plotly(fig)

xaxis, yaxis = plotly_fig.layout.xaxis, plotly_fig.layout.yaxis
assert xaxis.type == "linear"
assert yaxis.type == "linear"
assert list(xaxis.range) == pytest.approx([0, 18])
assert list(yaxis.range) == pytest.approx([-1.85, 195])
# The major ticks come from matplotlib's automatic locator, so plotly gets
# their number (10 on each axis over the widened limits) rather than
# explicit tick values
assert xaxis.nticks == 10
assert yaxis.nticks == 10
assert xaxis.tickvals is None
assert yaxis.tickvals is None
assert xaxis.dtick is None
assert yaxis.dtick is None
80 changes: 80 additions & 0 deletions tests/test_bars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import matplotlib.pyplot as plt
import pytest

from matplotlylib import mpl_to_plotly


def test_vertical_bar():
fig, ax = plt.subplots()
ax.bar(x=[0, 1, 2, 3, 4, 5], height=[10, 20, 50, 80, 100, 200])

plotly_fig = mpl_to_plotly(fig)

assert len(plotly_fig.data) == 1
bar = plotly_fig.data[0]
assert bar.type == "bar"
assert bar.orientation == "v"
assert list(bar.x) == [0, 1, 2, 3, 4, 5]
assert list(bar.y) == [10, 20, 50, 80, 100, 200]
assert bar.marker.color == "#1F77B4"
assert bar.opacity == 1
# Default bar width 0.8 at spacing 1
assert plotly_fig.layout.bargap == pytest.approx(0.2)


def test_horizontal_bar():
fig, ax = plt.subplots()
ax.barh(y=[0, 1, 2, 3, 4, 5, 6], width=[1, 4, 8, 16, 32, 64, 128])

plotly_fig = mpl_to_plotly(fig)

assert len(plotly_fig.data) == 1
bar = plotly_fig.data[0]
assert bar.type == "bar"
assert bar.orientation == "h"
assert list(bar.x) == [1, 4, 8, 16, 32, 64, 128]
assert list(bar.y) == [0, 1, 2, 3, 4, 5, 6]
assert bar.marker.color == "#1F77B4"
assert bar.opacity == 1
# Default bar height 0.8 at spacing 1
assert plotly_fig.layout.bargap == pytest.approx(0.2)


def test_h_and_v_bars():
fig, ax = plt.subplots()
ax.bar(
x=[0, 10, 20, 30, 40, 50],
height=[1, 4, 8, 16, 32, 64],
width=10,
color="green",
alpha=0.5,
)
ax.barh(
y=[15, 30, 45, 60, 75, 90],
width=[30, 60, 20, 50, 60, 30],
height=14,
color="red",
alpha=0.5,
)

plotly_fig = mpl_to_plotly(fig)

assert len(plotly_fig.data) == 2
vertical, horizontal = plotly_fig.data
assert vertical.type == "bar"
assert vertical.orientation == "v"
assert list(vertical.x) == [0, 10, 20, 30, 40, 50]
assert list(vertical.y) == [1, 4, 8, 16, 32, 64]
assert vertical.marker.color == "rgba(0, 128, 0, 0.5)"
assert vertical.opacity == 0.5

assert horizontal.type == "bar"
assert horizontal.orientation == "h"
assert list(horizontal.x) == [30, 60, 20, 50, 60, 30]
assert list(horizontal.y) == [15, 30, 45, 60, 75, 90]
assert horizontal.marker.color == "rgba(255, 0, 0, 0.5)"
assert horizontal.opacity == 0.5

# Plotly has one bargap per figure, and the bars drawn last set it: the
# horizontal bars are 14 high at spacing 15, a gap of 1/15
assert plotly_fig.layout.bargap == pytest.approx(1 / 15)
60 changes: 60 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import matplotlib.pyplot as plt

from matplotlylib import mpl_to_plotly

X1 = [0, 1, 2, 3, 4, 5]
Y1 = [10, 20, 50, 80, 100, 200]
X2 = [0, 1, 2, 3, 4, 5, 6]
Y2 = [1, 4, 8, 16, 32, 64, 128]


def test_line_data():
fig, ax = plt.subplots()
ax.plot(X1, Y1)

plotly_fig = mpl_to_plotly(fig)

assert len(plotly_fig.data) == 1
assert list(plotly_fig.data[0].x) == X1
assert list(plotly_fig.data[0].y) == Y1


def test_lines_data():
fig, ax = plt.subplots()
ax.plot(X1, Y1)
ax.plot(X2, Y2)

plotly_fig = mpl_to_plotly(fig)

assert len(plotly_fig.data) == 2
assert list(plotly_fig.data[0].x) == X1
assert list(plotly_fig.data[0].y) == Y1
assert list(plotly_fig.data[1].x) == X2
assert list(plotly_fig.data[1].y) == Y2


def test_bar_data():
fig, ax = plt.subplots()
ax.bar(X1, Y1)

plotly_fig = mpl_to_plotly(fig)

assert len(plotly_fig.data) == 1
assert list(plotly_fig.data[0].x) == X1
assert list(plotly_fig.data[0].y) == Y1


def test_bars_data():
fig, ax = plt.subplots()
ax.bar(X1, Y1, color="r")
ax.barh(X2, Y2, color="b")

plotly_fig = mpl_to_plotly(fig)

assert len(plotly_fig.data) == 2
# Vertical bars: positions on x, heights on y
assert list(plotly_fig.data[0].x) == X1
assert list(plotly_fig.data[0].y) == Y1
# Horizontal bars: widths on x, positions on y
assert list(plotly_fig.data[1].x) == Y2
assert list(plotly_fig.data[1].y) == X2
53 changes: 53 additions & 0 deletions tests/test_date_times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import datetime

import matplotlib.pyplot as plt
import pandas as pd
import pytest
from matplotlib.dates import date2num

from matplotlylib import mpl_to_plotly


def test_normal_mpl_dates():
date_strings = [
"2010-01-04 00:00:00",
"2010-01-04 10:00:00",
"2010-01-04 23:00:59",
"2010-01-05 00:00:00",
]
dates = [datetime.datetime.fromisoformat(s) for s in date_strings]

# Plot matplotlib date numbers on a date axis, as plot_date used to
fig, ax = plt.subplots()
ax.xaxis_date()
ax.plot(date2num(dates), [1, 2, 3, 4], "o")

plotly_fig = mpl_to_plotly(fig)

assert plotly_fig.layout.xaxis.type == "date"
assert len(plotly_fig.data) == 1
trace = plotly_fig.data[0]
assert trace.mode == "markers"
assert list(trace.x) == date_strings
assert list(trace.y) == [1, 2, 3, 4]


@pytest.mark.xfail(
strict=True,
reason="pandas time series plots keep matplotlib's day numbers as x values "
"on a date axis, so plotly reads them as milliseconds since 1970",
)
def test_pandas_time_series_date_formatter():
df = pd.DataFrame(
{"a": [0, 1, 2], "Date": pd.date_range("2001-01-01", periods=3, freq="D")}
)
fig = df.plot(x="Date").figure

plotly_fig = mpl_to_plotly(fig)

assert plotly_fig.layout.xaxis.type == "date"
assert list(plotly_fig.data[0].x) == [
"2001-01-01 00:00:00",
"2001-01-02 00:00:00",
"2001-01-03 00:00:00",
]
74 changes: 74 additions & 0 deletions tests/test_lines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import matplotlib.pyplot as plt

from matplotlylib import mpl_to_plotly

X1 = [0, 1, 2, 3, 4, 5]
Y1 = [10, 20, 50, 80, 100, 200]
X2 = [0, 1, 2, 3, 4, 5, 6]
Y2 = [1, 4, 8, 16, 32, 64, 128]


def test_simple_line():
fig, ax = plt.subplots()
ax.plot(X1, Y1, label="simple")

plotly_fig = mpl_to_plotly(fig)

assert len(plotly_fig.data) == 1
trace = plotly_fig.data[0]
assert trace.type == "scatter"
assert trace.mode == "lines"
assert trace.name == "simple"
assert list(trace.x) == X1
assert list(trace.y) == Y1
assert trace.line.color == "rgba(31, 119, 180, 1)"
assert trace.line.dash == "solid"
assert trace.line.width == 1.5


def test_complicated_line():
fig, ax = plt.subplots()
ax.plot(X1, Y1, "ro", markersize=10, alpha=0.5, label="one")
ax.plot(X1, Y1, "-b", linewidth=2, alpha=0.7, label="two")
ax.plot(X2, Y2, "b+", markeredgewidth=2, markersize=10, alpha=0.6, label="three")
ax.plot(X2, Y2, "--r", linewidth=2, alpha=0.8, label="four")

plotly_fig = mpl_to_plotly(fig)

assert len(plotly_fig.data) == 4
red_circles, blue_line, blue_crosses, red_dashes = plotly_fig.data

assert red_circles.mode == "markers"
assert red_circles.name == "one"
assert list(red_circles.x) == X1
assert list(red_circles.y) == Y1
assert red_circles.marker.symbol == "circle"
assert red_circles.marker.size == 10
assert red_circles.marker.color == "#FF0000"
assert red_circles.marker.opacity == 0.5

assert blue_line.mode == "lines"
assert blue_line.name == "two"
assert list(blue_line.x) == X1
assert list(blue_line.y) == Y1
assert blue_line.line.color == "rgba(0, 0, 255, 0.7)"
assert blue_line.line.dash == "solid"
assert blue_line.line.width == 2

assert blue_crosses.mode == "markers"
assert blue_crosses.name == "three"
assert list(blue_crosses.x) == X2
assert list(blue_crosses.y) == Y2
assert blue_crosses.marker.symbol == "cross"
assert blue_crosses.marker.size == 10
assert blue_crosses.marker.color == "#0000FF"
assert blue_crosses.marker.opacity == 0.6
assert blue_crosses.marker.line.width == 2

assert red_dashes.mode == "lines"
assert red_dashes.name == "four"
assert list(red_dashes.x) == X2
assert list(red_dashes.y) == Y2
assert red_dashes.line.color == "rgba(255, 0, 0, 0.8)"
assert red_dashes.line.dash == "dash"
assert red_dashes.line.width == 2
Loading