Commit 8fc591d
authored
feat: decouple Matplotlib render resolution (DPI) from display size (#9144)
## 📝 Summary
Closes #9124
This PR decouples the internal rendering resolution (DPI) of Matplotlib
figures from their display size in the marimo notebook.
The fundamental principle is that DPI should control image quality
(sharpness), not the physical display size.
Previously, increasing `fig.dpi` to achieve a sharper image caused the
plot to expand linearly, often breaking the notebook layout. This change
ensures that figures maintain a consistent display size based on a 100
DPI reference (Matplotlib's default), regardless of the rendering DPI.
## Changes
- **Simplified Rendering Logic**: Removed the hardcoded "retina" logic
(`*2` DPI and `//2` dimensions).
- **Dynamic Scaling**: The rendering now respects the figure's actual
DPI setting.
- **Metadata Normalization**: Display dimensions (width/height) sent to
the frontend are now scaled by `dpi / 100`. This ensures that a figure
with a given `figsize` occupies the same space in the notebook even if
the DPI is increased for higher quality.
- **Enhanced Testing**: Added parameterized tests in
`tests/_output/formatters/test_matplotlib.py` to verify that:
1. The actual PNG pixel count increases with DPI.
2. The metadata display size remains consistent regardless of DPI.
## Verification
The following image demonstrates the fix:
<img width="400"
src="https://github.com/user-attachments/assets/4cacf0f4-f94a-4f9f-9926-2ab84d27f832"
/>
- **Top**: `fig.dpi = 20` (Low resolution, correct display size)
- **Middle**: `fig.dpi = 300` (High resolution, **same display size**)
- **Bottom**: `savefig.format = "svg"` (Consistent behavior with SVG)
As shown, the display size remains stable even when the DPI is changed
significantly, allowing users to control image quality without affecting
the notebook layout.
## 📋 Pre-Review Checklist
<!-- These checks need to be completed before a PR is reviewed -->
- [x] For large changes, or changes that affect the public API: this
change was discussed or approved through an issue, on
[Discord](https://marimo.io/discord?ref=pr), or the community
[discussions](https://github.com/marimo-team/marimo/discussions) (Please
provide a link if applicable).
- [ ] Any AI generated code has been reviewed line-by-line by the human
PR author, who stands by it.
- [x] Video or media evidence is provided for any visual changes
(optional). <!-- PR is more likely to be merged if evidence is provided
for changes made -->
## ✅ Merge Checklist
- [x] I have read the [contributor
guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md).
- [ ] Documentation has been updated where applicable, including
docstrings for API changes.
- [x] Tests have been added for the changes made.1 parent b95d26d commit 8fc591d
3 files changed
Lines changed: 40 additions & 41 deletions
File tree
- frontend/src/components/editor
- marimo/_output
- tests/_output/formatters
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
263 | 263 | | |
264 | 264 | | |
265 | 265 | | |
266 | | - | |
| 266 | + | |
267 | 267 | | |
268 | 268 | | |
269 | 269 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
| 86 | + | |
| 87 | + | |
91 | 88 | | |
92 | 89 | | |
93 | 90 | | |
| |||
98 | 95 | | |
99 | 96 | | |
100 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
106 | | - | |
| 105 | + | |
| 106 | + | |
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
88 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
89 | 92 | | |
90 | | - | |
| 93 | + | |
91 | 94 | | |
92 | 95 | | |
93 | 96 | | |
94 | 97 | | |
95 | 98 | | |
96 | 99 | | |
97 | 100 | | |
98 | | - | |
| 101 | + | |
99 | 102 | | |
100 | 103 | | |
101 | | - | |
102 | | - | |
103 | | - | |
| 104 | + | |
| 105 | + | |
104 | 106 | | |
105 | 107 | | |
106 | 108 | | |
| |||
124 | 126 | | |
125 | 127 | | |
126 | 128 | | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
133 | 136 | | |
134 | 137 | | |
135 | 138 | | |
| |||
140 | 143 | | |
141 | 144 | | |
142 | 145 | | |
143 | | - | |
144 | | - | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
145 | 149 | | |
146 | | - | |
| 150 | + | |
147 | 151 | | |
148 | 152 | | |
149 | 153 | | |
150 | 154 | | |
151 | 155 | | |
152 | 156 | | |
153 | 157 | | |
154 | | - | |
| 158 | + | |
155 | 159 | | |
156 | 160 | | |
157 | | - | |
158 | | - | |
159 | | - | |
| 161 | + | |
| 162 | + | |
160 | 163 | | |
161 | 164 | | |
162 | 165 | | |
| |||
177 | 180 | | |
178 | 181 | | |
179 | 182 | | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
| 183 | + | |
185 | 184 | | |
186 | 185 | | |
187 | 186 | | |
188 | 187 | | |
189 | 188 | | |
190 | 189 | | |
191 | 190 | | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
199 | 198 | | |
200 | 199 | | |
201 | 200 | | |
202 | 201 | | |
203 | 202 | | |
204 | 203 | | |
205 | | - | |
| 204 | + | |
206 | 205 | | |
207 | 206 | | |
208 | 207 | | |
| |||
0 commit comments