Note
Go to the end to download the full example code.
Line Segments
Display line segments. Can be useful e.g. for visializing vector fields.

import numpy as np
from rendercanvas.auto import RenderCanvas, loop
import pygfx as gfx
canvas = RenderCanvas()
renderer = gfx.WgpuRenderer(canvas)
scene = gfx.Scene()
x = np.linspace(20, 980, 200, dtype=np.float32)
y = np.sin(x / 30) * 4
positions = np.column_stack([x, y, np.zeros_like(x)])
colors = np.random.uniform(0, 1, (x.size, 4)).astype(np.float32)
colors[:, 3] = 1
line = gfx.Points(
gfx.Geometry(positions=positions, colors=colors),
gfx.PointsMarkerMaterial(size=20, marker="pin", rotation_mode="curve"),
)
scene.add(line)
camera = gfx.OrthographicCamera(maintain_aspect=False)
camera.show_rect(0, 1000, -5, 5)
controller = gfx.PanZoomController(camera, register_events=renderer)
if __name__ == "__main__":
canvas.request_draw(lambda: renderer.render(scene, camera))
loop.run()
Total running time of the script: (0 minutes 0.189 seconds)