Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions examples/controllers/partial_camera_linking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Partial camera linking
======================

You can customize the camera axes that a controller acts on. In this example with two subplots you can pan and zoom
in x-y in each individual subplot, but only the x-axis panning is linked between the two subplots. The y-axis pan
and zoom in independent on each subplot.
"""

# test_example = false
# sphinx_gallery_pygfx_docs = 'screenshot'

import numpy as np
import fastplotlib as fpl
import pygfx

xs = np.linspace(0, 2 * np.pi, 100)
ys = np.sin(xs)

ys_big = np.random.rand(100) * 10

# create cameras, fov=0 means Orthographic projection
camera1 = pygfx.PerspectiveCamera(fov=0)
camera2 = pygfx.PerspectiveCamera(fov=0)

# create controllers, first add the "main" camera for the subplot
controller1 = pygfx.PanZoomController(camera1)
controller2 = pygfx.PanZoomController(camera2)

# add the other camera to each controller, but only include the 'x' state, i.e. 'y' for height is not included
# this must be done only after adding the "main" cameras to the controller as done above
controller1.add_camera(camera2, include_state={"x", "width"})
controller2.add_camera(camera1, include_state={"x", "width"})

# create figure using these cameras and controllers
figure = fpl.Figure(
shape=(2, 1),
cameras=[camera1, camera2],
controllers=[controller1, controller2],
size=(700, 560)
)

figure[0, 0].add_line(np.column_stack([xs, ys_big]))
figure[1, 0].add_line(np.column_stack([xs, ys]))

for subplot in figure:
subplot.camera.zoom = 1.0

figure.show(maintain_aspect=False, autoscale=True)

# NOTE: fpl.loop.run() should not be used for interactive sessions
# See the "JupyterLab and IPython" section in the user guide
if __name__ == "__main__":
print(__doc__)
fpl.loop.run()
46 changes: 46 additions & 0 deletions examples/line/inf_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Infinite Lines
==============

Draw infinite vertical and horizontal lines to mark positions on a plot.
"""

# test_example = true
# sphinx_gallery_pygfx_docs = 'screenshot'

import fastplotlib as fpl
import numpy as np

figure = fpl.Figure(size=(700, 560))

xs = np.linspace(0, 4 * np.pi, 100)
ys = np.sin(xs)
data = np.column_stack([xs, ys])

figure[0, 0].add_line(data, thickness=2, colors="w")

# vertical lines at the zero-crossings, one color per line by passing a list of colors
zero_crossings = np.array([0, np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi])
figure[0, 0].add_inf_line(
zero_crossings, axis="x", colors=["r", "g", "b", "c", "m"], thickness=2
)

# dashed horizontal lines at the sine bounds, provided as a 1D array of y-values
figure[0, 0].add_inf_line(
np.array([-1.0, 1.0]),
axis="y",
colors="gray",
thickness=2,
dash_pattern="--",
)

figure[0, 0].axes.intersection = (0, 0, 0)

figure.show()


# NOTE: fpl.loop.run() should not be used for interactive sessions
# See the "JupyterLab and IPython" section in the user guide
if __name__ == "__main__":
print(__doc__)
fpl.loop.run()
27 changes: 27 additions & 0 deletions examples/line/inf_line_cmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Infinite Lines Colormap
=======================

Apply a colormap across a set of infinite lines, one color per line.
"""

# test_example = true
# sphinx_gallery_pygfx_docs = 'screenshot'

import fastplotlib as fpl
import numpy as np

figure = fpl.Figure(size=(700, 560))

# vertical lines colored by a colormap, one color per line
positions = np.arange(10)
figure[0, 0].add_inf_line(positions, axis="x", cmap="viridis", thickness=3)

figure.show()


# NOTE: fpl.loop.run() should not be used for interactive sessions
# See the "JupyterLab and IPython" section in the user guide
if __name__ == "__main__":
print(__doc__)
fpl.loop.run()
33 changes: 33 additions & 0 deletions examples/line/inf_line_cmap_transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Infinite Lines Colormap Transform
=================================

Use a ``cmap_transform`` to color infinite lines by an associated value rather than by their sequential
order. Here each line at an x-position is colored according to the sine value at that x-axis position.
"""

# test_example = true
# sphinx_gallery_pygfx_docs = 'screenshot'

import fastplotlib as fpl
import numpy as np

figure = fpl.Figure(size=(700, 560))

# evenly spaced vertical lines
positions = np.linspace(0, 6 * np.pi, 32)

# color each line by an associated value using the colormap transform
values = np.sin(positions)
figure[0, 0].add_inf_line(
positions, axis="x", cmap="plasma", cmap_transform=values, thickness=3
)

figure.show()


# NOTE: fpl.loop.run() should not be used for interactive sessions
# See the "JupyterLab and IPython" section in the user guide
if __name__ == "__main__":
print(__doc__)
fpl.loop.run()
34 changes: 34 additions & 0 deletions examples/line/inf_line_pairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Infinite Lines from Point Pairs
===============================

Define infinite lines directly from pairs of points using ``axis=None``. Each two consecutive
points define one line. Here pairs of points sampled around the unit circle are used to produce
lines that are roughly tangent to the circle.
"""

# test_example = true
# sphinx_gallery_pygfx_docs = 'screenshot'

import fastplotlib as fpl
import numpy as np

figure = fpl.Figure(size=(700, 560))

# an even number of points sampled around a circle; each consecutive pair of points defines an infinite line
t = np.linspace(0, 2 * np.pi, 64, endpoint=False)
xs = np.sin(t)
ys = np.cos(t)
positions = np.column_stack([xs, ys, np.zeros_like(xs)])

figure[0, 0].add_inf_line(positions, axis=None, cmap="hsv", thickness=2)
figure[0, 0].axes.intersection = (0, 0, 0)

figure.show()


# NOTE: fpl.loop.run() should not be used for interactive sessions
# See the "JupyterLab and IPython" section in the user guide
if __name__ == "__main__":
print(__doc__)
fpl.loop.run()
35 changes: 35 additions & 0 deletions examples/line/line_dash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Line Dash Patterns
==================

Draw lines with different dash patterns using matplotlib-style strings.
"""

# test_example = true
# sphinx_gallery_pygfx_docs = 'screenshot'

import fastplotlib as fpl
import numpy as np

figure = fpl.Figure(size=(700, 560))

xs = np.linspace(0, 4 * np.pi, 100)

# a matplotlib-style string, or a sequence of floats, sets the dash pattern
patterns = ["-", "--", "-.", ":"]

for i, pattern in enumerate(patterns):
ys = np.sin(xs) + i * 3
data = np.column_stack([xs, ys])
figure[0, 0].add_line(
data, thickness=5, dash_pattern=pattern, name=pattern
)

figure.show()


# NOTE: fpl.loop.run() should not be used for interactive sessions
# See the "JupyterLab and IPython" section in the user guide
if __name__ == "__main__":
print(__doc__)
fpl.loop.run()
3 changes: 3 additions & 0 deletions examples/screenshots/inf_line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/screenshots/inf_line_cmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/screenshots/inf_line_cmap_transform.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/screenshots/inf_line_pairs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/screenshots/line_dash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions fastplotlib/graphics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ._base import Graphic
from .line import LineGraphic
from .inf_line import InfLineGraphic
from .scatter import ScatterGraphic
from .image import ImageGraphic, ImageYUVGraphic
from .image_volume import ImageVolumeGraphic
Expand All @@ -12,6 +13,7 @@
__all__ = [
"Graphic",
"LineGraphic",
"InfLineGraphic",
"ScatterGraphic",
"ImageGraphic",
"ImageYUVGraphic",
Expand Down
17 changes: 13 additions & 4 deletions fastplotlib/graphics/_positions_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
class PositionsGraphic(Graphic):
"""Base class for LineGraphic and ScatterGraphic"""

# the feature used to manage a per-vertex color buffer, subclasses may override
_VertexColorsCls = VertexColors

@property
def data(self) -> VertexPositions:
"""
Expand Down Expand Up @@ -155,7 +158,7 @@ def _create_colors_buffer(self, colors, color_mode) -> UniformColor | VertexColo
if color_mode in ("auto", "uniform"):
new_colors = UniformColor(colors)
else:
new_colors = VertexColors(
new_colors = self._VertexColorsCls(
colors, n_colors=self._data.value.shape[0]
)

Expand All @@ -166,7 +169,9 @@ def _create_colors_buffer(self, colors, color_mode) -> UniformColor | VertexColo
"You passed `color_mode` = 'uniform', but specified a sequence of multiple colors. Use "
"`color_mode` = 'auto' or 'vertex' for multiple colors."
)
new_colors = VertexColors(colors, n_colors=self._data.value.shape[0])
new_colors = self._VertexColorsCls(
colors, n_colors=self._data.value.shape[0]
)

elif len(colors) > 4:
# sequence of multiple colors, must again ensure color_mode is not uniform
Expand All @@ -175,7 +180,9 @@ def _create_colors_buffer(self, colors, color_mode) -> UniformColor | VertexColo
"You passed `color_mode` = 'uniform', but specified a sequence of multiple colors. Use "
"`color_mode` = 'auto' or 'vertex' for multiple colors."
)
new_colors = VertexColors(colors, n_colors=self._data.value.shape[0])
new_colors = self._VertexColorsCls(
colors, n_colors=self._data.value.shape[0]
)
else:
raise ValueError(
"`colors` must be a str, pygfx.Color, array, list or tuple indicating an RGB(A) color, or a "
Expand Down Expand Up @@ -225,7 +232,9 @@ def __init__(
self._colors = colors
else:
# create vertex colors buffer
self._colors = VertexColors("w", n_colors=self._data.value.shape[0])
self._colors = self._VertexColorsCls(
"w", n_colors=self._data.value.shape[0]
)
# make cmap using vertex colors buffer
self._cmap = VertexCmap(
self._colors,
Expand Down
7 changes: 6 additions & 1 deletion fastplotlib/graphics/features/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
SizeSpace,
VertexPositions,
VertexCmap,
InfLineAxisData,
InfLineColors,
)
from ._mesh import (
MeshIndices,
Expand All @@ -14,7 +16,7 @@
surface_data_to_mesh,
triangulate_polygon,
)
from ._line import Thickness
from ._line import Thickness, DashPattern, parse_dash_pattern
from ._scatter import (
VertexMarkers,
UniformMarker,
Expand Down Expand Up @@ -83,10 +85,13 @@
"SizeSpace",
"VertexPositions",
"VertexCmap",
"InfLineAxisData",
"InfLineColors",
"MeshIndices",
"MeshCmap",
"SurfaceData",
"Thickness",
"DashPattern",
"VertexMarkers",
"UniformMarker",
"UniformEdgeColor",
Expand Down
Loading