Skip to content

Commit da6f0e6

Browse files
authored
Merge pull request #1045 from arpastrana/fix/plotterzoom
Fixed uncentered viewbox and aspect ratio in `Plotter.zoom_extents`
2 parents abf7f1b + f324a89 commit da6f0e6

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3333
* Fixed type error in `compas_rhino.conversions._shapes.box_to_rhino`.
3434
* Moved from `autopep8` to `black`
3535
* Fixed bug in `compas.utilities.linspace` for number series with high precision start and stop values.
36+
* Fixed uncentered viewbox in `Plotter.zoom_extents()`
3637

3738
### Removed
3839

src/compas_plotters/plotter.py

Lines changed: 34 additions & 10 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

@@ -218,31 +219,54 @@ def pause(self, pause: float) -> None:
218219
plt.pause(pause)
219220

220221
def zoom_extents(self, padding: Optional[int] = None) -> None:
221-
"""Zoom the view to the bounding box of all objects."""
222-
padding = padding or 0
222+
"""Zoom the view to the bounding box of all objects.
223+
224+
Parameters
225+
----------
226+
padding : int, optional
227+
Extra padding around the bounding box of all objects.
228+
"""
229+
padding = padding or 0.0
223230
width, height = self.figsize
224231
fig_aspect = width / height
232+
225233
data = []
226234
for artist in self.artists:
227235
data += artist.data
236+
228237
x, y = zip(*data)
238+
229239
xmin = min(x)
230240
xmax = max(x)
231241
ymin = min(y)
232242
ymax = max(y)
233-
xspan = xmax - xmin + padding
234-
yspan = ymax - ymin + padding
243+
xdiff = xmax - xmin
244+
ydiff = ymax - ymin
245+
246+
xmin = xmin - 0.1 * xdiff - padding
247+
xmax = xmax + 0.1 * xdiff + padding
248+
ymin = ymin - 0.1 * ydiff - padding
249+
ymax = ymax + 0.1 * ydiff + padding
250+
251+
xspan = xmax - xmin
252+
yspan = ymax - ymin
235253
data_aspect = xspan / yspan
236-
xlim = [xmin - 0.1 * xspan, xmax + 0.1 * xspan]
237-
ylim = [ymin - 0.1 * yspan, ymax + 0.1 * yspan]
254+
238255
if data_aspect < fig_aspect:
239256
scale = fig_aspect / data_aspect
240-
xlim[0] *= scale
241-
xlim[1] *= scale
257+
xpad = (xspan * (scale - 1.0)) / 2.0
258+
xmin -= xpad
259+
xmax += xpad
242260
else:
243261
scale = data_aspect / fig_aspect
244-
ylim[0] *= scale
245-
ylim[1] *= scale
262+
ypad = (yspan * (scale - 1.0)) / 2.0
263+
ymin -= ypad
264+
ymax += ypad
265+
266+
assert allclose([fig_aspect], [(xmax - xmin) / (ymax - ymin)])
267+
268+
xlim = [xmin, xmax]
269+
ylim = [ymin, ymax]
246270
self.viewbox = (xlim, ylim)
247271
self.axes.set_xlim(*xlim)
248272
self.axes.set_ylim(*ylim)

0 commit comments

Comments
 (0)