@@ -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