.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/validation/validate_ruler.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_validation_validate_ruler.py: Validate ruler ============== Showing off some ruler tricks. .. GENERATED FROM PYTHON SOURCE LINES 7-100 .. image-sg:: /_gallery/validation/images/sphx_glr_validate_ruler_001.webp :alt: validate ruler :srcset: /_gallery/validation/images/sphx_glr_validate_ruler_001.webp :class: sphx-glr-single-img .. code-block:: Python import numpy as np from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx canvas = RenderCanvas() renderer = gfx.WgpuRenderer(canvas) scene = gfx.Scene() background = gfx.Background.from_color("#000") x = np.linspace(20, 980, 200, dtype=np.float32) y = np.sin(x / 30) * 4 positions = np.column_stack([x, y, np.ones_like(x)]) line = gfx.Line( gfx.Geometry(positions=positions), gfx.LineMaterial(thickness=4.0, color="#aaf", aa=True), ) scene.add(background, line) # Normal horizontal ruler rulerx1 = gfx.Ruler( tick_side="right", tick_marker="tick_left", tick_size=12, start_value=0, start_pos=(0, 0, 0), end_pos=(1000, 0, 0), ) # Normal vertical ruler rulery1 = gfx.Ruler( tick_side="left", tick_marker="tick_right", tick_size=12, start_value=-5, start_pos=(0, -5, 0), end_pos=(0, 5, 0), ) # Alternative formatting and tick distance rulerx2 = gfx.Ruler( tick_side="right", start_pos=(0, -6, 0), end_pos=(1000, -6, 0), tick_format="0.1f", min_tick_distance=100, ) # Ticks specified using list, and formatting via a function rulerx3 = gfx.Ruler( tick_side="right", start_pos=(0, -8, 0), end_pos=(1000, -8, 0), ticks=[0, 250, 500, 750, 950], tick_format=lambda v, mi, ma: str(v / 1000) + "K", ) # Ticks specified using a dict, and showing ticks on other side. # Note how the dict can contain both strings and floats. rulerx4 = gfx.Ruler( tick_side="left", start_pos=(0, -10, 0), end_pos=(1000, -10, 0), ticks={250: "25%", 500: "50%", 750: "75%", 0: 0, 1000: 1000, 400: "POI"}, tick_format="0.1f", ) scene.add(rulerx1, rulerx2, rulerx3, rulerx4, rulery1) camera = gfx.OrthographicCamera(maintain_aspect=False) camera.show_rect(-100, 1100, -12, 6) controller = gfx.PanZoomController(camera, register_events=renderer) def animate(): for ob in scene.children: if isinstance(ob, gfx.Ruler): ob.update(camera, canvas.get_logical_size()) renderer.render(scene, camera) canvas.request_draw(animate) if __name__ == "__main__": print(__doc__) loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.246 seconds) .. _sphx_glr_download__gallery_validation_validate_ruler.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: validate_ruler.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: validate_ruler.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: validate_ruler.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_ .. only:: html Interactive example ------------------- Try this example in your browser using Pyodide. Might not work with all examples and all devices. Check the output and your browser's console for details. .. raw:: html