← 返回首页
added camera auto-scaling, examples work :D by kushalkolar · Pull Request #80 · fastplotlib/fastplotlib · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (4) All 1 file type selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
43 changes: 43 additions & 0 deletions fastplotlib/layouts/_base.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
from pygfx import Scene, OrthographicCamera, PerspectiveCamera, PanZoomController, OrbitController, \
Viewport, WgpuRenderer
from wgpu.gui.auto import WgpuCanvas
Expand Down Expand Up @@ -37,6 +38,13 @@ def __init__(
self.camera
)

# camera.far and camera.near clipping planes get
# wonky with setting controller.distance = 0
if isinstance(self.camera, OrthographicCamera):
self.controller.distance = 0
# also set a initial zoom
self.controller.zoom(0.8 / self.controller.zoom_value)

self.renderer.add_event_handler(self.set_viewport_rect, "resize")

self._graphics: List[Graphic] = list()
Expand Down Expand Up @@ -135,6 +143,15 @@ def center_graphic(self, graphic, zoom: float = 1.3):
self.controller.zoom(zoom)

def center_scene(self, zoom: float = 1.3):
"""
Auto-center the scene, does not scale.

Parameters
----------
zoom: float, default 1.3
apply a zoom after centering the scene

"""
if not len(self.scene.children) > 0:
return

Expand All @@ -148,6 +165,32 @@ def center_scene(self, zoom: float = 1.3):

self.controller.zoom(zoom)

def auto_scale(self, maintain_aspect: bool = False, zoom: float = 0.8):
"""
Auto-scale the camera w.r.t to the scene

Parameters
----------
maintain_aspect: bool, default ``False``
maintain the camera aspect ratio for all dimensions, if ``False`` the camera
is scaled according to the bounds in each dimension.

zoom: float, default 0.8
zoom value for the camera after auto-scaling, if zoom = 1.0 then the graphics
in the scene will fill the entire canvas.
"""
self.center_scene()
self.camera.maintain_aspect = maintain_aspect

width, height, depth = np.ptp(self.scene.get_world_bounding_box(), axis=0)

self.camera.width = width
self.camera.height = height

# self.controller.distance = 0

self.controller.zoom(zoom / self.controller.zoom_value)

def get_graphics(self):
return self._graphics

Expand Down
2 changes: 1 addition & 1 deletion fastplotlib/layouts/_gridplot.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def show(self):
self.canvas.request_draw(self.render)

for subplot in self:
subplot.center_scene()
subplot.auto_scale(maintain_aspect=True, zoom=0.95)

return self.canvas

Expand Down
6 changes: 5 additions & 1 deletion fastplotlib/layouts/_subplot.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ def __init__(
self.set_title(self.name)

def _create_graphic(self, graphic_class, *args, **kwargs):
if "center" in kwargs.keys():
center = kwargs.pop("center")
else:
center = False
graphic = graphic_class(*args, **kwargs)
self.add_graphic(graphic, center=False)
self.add_graphic(graphic, center=center)

return graphic

Expand Down
2 changes: 1 addition & 1 deletion fastplotlib/plot.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ def render(self):

def show(self):
self.canvas.request_draw(self.render)
self.center_scene()
self.auto_scale(maintain_aspect=True, zoom=0.95)

return self.canvas
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.