Skip to content

Commit 2056b3f

Browse files
committed
Fixed uncentered viewbox and aspect ratio in Plotter.zoom_extents
1 parent a084f03 commit 2056b3f

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
* Rebuild part index after deserialization in `Assembly`.
2828
* Fixed bug in `compas.artists.colordict.ColorDict`.
2929
* Change `Mesh.mesh_dual` with option of including the boundary.
30+
* Fixed uncentered viewbox in `Plotter.zoom_extents()`
3031

3132
### Removed
3233

src/compas_plotters/plotter.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from PIL import Image
2626

2727
import compas
28+
from compas.geometry import allclose
2829
from .artists import PlotterArtist
2930

3031

@@ -213,31 +214,53 @@ def pause(self, pause: float) -> None:
213214
plt.pause(pause)
214215

215216
def zoom_extents(self, padding: Optional[int] = None) -> None:
216-
"""Zoom the view to the bounding box of all objects."""
217-
padding = padding or 0
217+
"""Zoom the view to the bounding box of all objects.
218+
219+
Parameters
220+
----------
221+
padding : int, optional
222+
Extra padding around the bounding box of all objects.
223+
"""
224+
padding = padding or 0.0
218225
width, height = self.figsize
219226
fig_aspect = width / height
227+
220228
data = []
221229
for artist in self.artists:
222230
data += artist.data
223-
x, y = zip(* data)
231+
232+
x, y = zip(*data)
233+
224234
xmin = min(x)
225235
xmax = max(x)
226236
ymin = min(y)
227237
ymax = max(y)
228-
xspan = xmax - xmin + padding
229-
yspan = ymax - ymin + padding
238+
xdiff = xmax - xmin
239+
ydiff = ymax - ymin
240+
241+
xmin = xmin - 0.1 * xdiff - padding
242+
xmax = xmax + 0.1 * xdiff + padding
243+
ymin = ymin - 0.1 * ydiff - padding
244+
ymax = ymax + 0.1 * ydiff + padding
245+
246+
xspan = xmax - xmin
247+
yspan = ymax - ymin
230248
data_aspect = xspan / yspan
231-
xlim = [xmin - 0.1 * xspan, xmax + 0.1 * xspan]
232-
ylim = [ymin - 0.1 * yspan, ymax + 0.1 * yspan]
249+
233250
if data_aspect < fig_aspect:
234251
scale = fig_aspect / data_aspect
235-
xlim[0] *= scale
236-
xlim[1] *= scale
252+
xpad = (xspan * (scale - 1.0)) / 2.0
253+
xmin -= xpad
254+
xmax += xpad
237255
else:
238256
scale = data_aspect / fig_aspect
239-
ylim[0] *= scale
240-
ylim[1] *= scale
257+
ypad = (yspan * (scale - 1.0)) / 2.0
258+
ymin -= ypad
259+
ymax += ypad
260+
assert allclose([fig_aspect], [(xmax - xmin) / (ymax - ymin)])
261+
262+
xlim = [xmin, xmax]
263+
ylim = [ymin, ymax]
241264
self.viewbox = (xlim, ylim)
242265
self.axes.set_xlim(*xlim)
243266
self.axes.set_ylim(*ylim)

0 commit comments

Comments
 (0)