Skip to content

Commit 0397692

Browse files
committed
fix(mpl): use rounding for dimensions and improve DPI test precision
1 parent e778170 commit 0397692

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

marimo/_output/mpl.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,15 @@ def _render_figure_mimebundle(
9595
try:
9696
# Extract dimensions from the PNG
9797
width, height = _extract_png_dimensions(png_bytes)
98-
# Normalize to Matplotlib's default 100 DPI for consistent display size
98+
# Normalize to a fixed 100 DPI reference for consistent display size
99+
# https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html
99100
factor = dpi / 100
100101
mimebundle = {
101102
"image/png": data_url,
102103
METADATA_KEY: {
103104
"image/png": {
104-
"width": int(width / factor),
105-
"height": int(height / factor),
105+
"width": round(width / factor),
106+
"height": round(height / factor),
106107
}
107108
},
108109
}

tests/_output/formatters/test_matplotlib.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ async def test_matplotlib_image_resolution_respects_dpi(
101101
f"""
102102
import matplotlib.pyplot as plt
103103
104-
# Create a simple figure
105-
fig, ax = plt.subplots(figsize=(4, 3), dpi={dpi})
106-
ax.plot([1, 2, 3], [1, 2, 3])
104+
# Create an empty figure (no content) to isolate DPI effects
105+
fig = plt.figure(figsize=(4, 3), dpi={dpi})
107106
108107
# Get the formatted output
109108
result = fig._mime_()
@@ -126,8 +125,14 @@ async def test_matplotlib_image_resolution_respects_dpi(
126125
# Extract PNG data and check dimensions
127126
png_data_url = mimebundle["image/png"]
128127
width, height = _extract_png_dimensions(png_data_url)
129-
assert 0.9 * 4 * dpi < width < 1.1 * 4 * dpi
130-
assert 0.9 * 3 * dpi < height < 1.1 * 3 * dpi
128+
129+
# https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.savefig.html
130+
pad_inches = 0.1
131+
calc_width = round((4 + 2 * pad_inches) * dpi)
132+
calc_height = round((3 + 2 * pad_inches) * dpi)
133+
134+
assert calc_width - 5 < width < calc_width + 5
135+
assert calc_height - 5 < height < calc_height + 5
131136

132137
# Verify aspect ratio is preserved (4:3 ratio)
133138
aspect_ratio = width / height
@@ -153,9 +158,8 @@ async def test_matplotlib_display_size_remains_constant(
153158
f"""
154159
import matplotlib.pyplot as plt
155160
156-
# Create a simple figure
157-
fig, ax = plt.subplots(figsize=(4, 3), dpi={dpi})
158-
ax.plot([1, 2, 3], [1, 2, 3])
161+
# Create an empty figure (no content) to isolate DPI effects
162+
fig = plt.figure(figsize=(4, 3), dpi={dpi})
159163
result = fig._mime_()
160164
"""
161165
)
@@ -184,8 +188,13 @@ async def test_matplotlib_display_size_remains_constant(
184188
metadata_width = png_metadata["width"]
185189
metadata_height = png_metadata["height"]
186190

187-
assert 0.9 * 4 * 100 < metadata_width < 1.1 * 4 * 100
188-
assert 0.9 * 3 * 100 < metadata_height < 1.1 * 3 * 100
191+
# https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.savefig.html
192+
pad_inches = 0.1
193+
calc_width = round((4 + 2 * pad_inches) * 100)
194+
calc_height = round((3 + 2 * pad_inches) * 100)
195+
196+
assert calc_width - 5 < metadata_width < calc_width + 5
197+
assert calc_height - 5 < metadata_height < calc_height + 5
189198

190199

191200
@pytest.mark.skipif(not HAS_MPL, reason="optional dependencies not installed")

0 commit comments

Comments
 (0)