Skip to content

more cmap handling, add cmap_values #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 21, 2023
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
7 changes: 1 addition & 6 deletions examples/line/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@
from fastplotlib import Plot
import numpy as np

from wgpu.gui.offscreen import WgpuCanvas
from pygfx import WgpuRenderer

canvas = WgpuCanvas()
renderer = WgpuRenderer(canvas)

plot = Plot(canvas=canvas, renderer=renderer)
plot = Plot()

xs = np.linspace(-10, 10, 100)
# sine wave
Expand Down
46 changes: 46 additions & 0 deletions examples/line/line_cmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Line Plot
============
Example showing cosine, sine, sinc lines.
"""

# test_example = true

import fastplotlib as fpl
import numpy as np


plot = fpl.Plot()

xs = np.linspace(-10, 10, 100)
# sine wave
ys = np.sin(xs)
sine = np.dstack([xs, ys])[0]

# cosine wave
ys = np.cos(xs) - 5
cosine = np.dstack([xs, ys])[0]

# cmap_values from an array, so the colors on the sine line will be based on the sine y-values
sine_graphic = plot.add_line(
data=sine,
thickness=10,
cmap="plasma",
cmap_values=sine[:, 1]
)

# qualitative colormaps, useful for cluster labels or other types of categorical labels
cmap_values = [0] * 25 + [5] * 10 + [1] * 35 + [2] * 30
cosine_graphic = plot.add_line(
data=cosine,
thickness=10,
cmap="tab10",
cmap_values=cmap_values
)

plot.show()

plot.canvas.set_logical_size(800, 800)

if __name__ == "__main__":
fpl.run()
7 changes: 1 addition & 6 deletions examples/line/line_colorslice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@
from fastplotlib import Plot
import numpy as np

from wgpu.gui.offscreen import WgpuCanvas
from pygfx import WgpuRenderer

canvas = WgpuCanvas()
renderer = WgpuRenderer(canvas)

plot = Plot(canvas=canvas, renderer=renderer)
plot = Plot()

xs = np.linspace(-10, 10, 100)
# sine wave
Expand Down
7 changes: 1 addition & 6 deletions examples/line/line_dataslice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@
from fastplotlib import Plot
import numpy as np

from wgpu.gui.offscreen import WgpuCanvas
from pygfx import WgpuRenderer

canvas = WgpuCanvas()
renderer = WgpuRenderer(canvas)

plot = Plot(canvas=canvas, renderer=renderer)
plot = Plot()

xs = np.linspace(-10, 10, 100)
# sine wave
Expand Down
7 changes: 1 addition & 6 deletions examples/line/line_present_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@
from fastplotlib import Plot
import numpy as np

from wgpu.gui.offscreen import WgpuCanvas
from pygfx import WgpuRenderer

canvas = WgpuCanvas()
renderer = WgpuRenderer(canvas)

plot = Plot(canvas=canvas, renderer=renderer)
plot = Plot()

xs = np.linspace(-10, 10, 100)
# sine wave
Expand Down
Empty file.
39 changes: 39 additions & 0 deletions examples/line_collection/line_collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Line Plot
============
Example showing how to plot line collections
"""

# test_example = true

from itertools import product
import numpy as np
import fastplotlib as fpl


def make_circle(center, radius: float, n_points: int = 75) -> np.ndarray:
theta = np.linspace(0, 2 * np.pi, n_points)
xs = radius * np.sin(theta)
ys = radius * np.cos(theta)

return np.column_stack([xs, ys]) + center


spatial_dims = (100, 100)

circles = list()
for center in product(range(0, spatial_dims[0], 15), range(0, spatial_dims[1], 15)):
circles.append(make_circle(center, 5, n_points=75))

pos_xy = np.vstack(circles)

plot = fpl.Plot()

plot.add_line_collection(circles, cmap="jet", thickness=5)

plot.show()

plot.canvas.set_logical_size(800, 800)

if __name__ == "__main__":
fpl.run()
50 changes: 50 additions & 0 deletions examples/line_collection/line_collection_cmap_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Line Plot
============
Example showing how to plot line collections
"""

# test_example = true

from itertools import product
import numpy as np
import fastplotlib as fpl


def make_circle(center, radius: float, n_points: int = 75) -> np.ndarray:
theta = np.linspace(0, 2 * np.pi, n_points)
xs = radius * np.sin(theta)
ys = radius * np.cos(theta)

return np.column_stack([xs, ys]) + center


spatial_dims = (50, 50)

circles = list()
for center in product(range(0, spatial_dims[0], 15), range(0, spatial_dims[1], 15)):
circles.append(make_circle(center, 5, n_points=75))

pos_xy = np.vstack(circles)

# this makes 16 circles, so we can create 16 cmap values, so it will use these values to set the
# color of the line based by using the cmap as a LUT with the corresponding cmap_value

# highest values, lowest values, mid-high values, mid values
cmap_values = [10] * 4 + [0] * 4 + [7] * 4 + [5] * 4

plot = fpl.Plot()

plot.add_line_collection(
circles,
cmap="bwr",
cmap_values=cmap_values,
thickness=10
)

plot.show()

plot.canvas.set_logical_size(800, 800)

if __name__ == "__main__":
fpl.run()
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Line Plot
============
Example showing how to plot line collections
"""

# test_example = true

from itertools import product
import numpy as np
import fastplotlib as fpl


def make_circle(center, radius: float, n_points: int = 75) -> np.ndarray:
theta = np.linspace(0, 2 * np.pi, n_points)
xs = radius * np.sin(theta)
ys = radius * np.cos(theta)

return np.column_stack([xs, ys]) + center


spatial_dims = (50, 50)

circles = list()
for center in product(range(0, spatial_dims[0], 15), range(0, spatial_dims[1], 15)):
circles.append(make_circle(center, 5, n_points=75))

pos_xy = np.vstack(circles)

# this makes 16 circles, so we can create 16 cmap values, so it will use these values to set the
# color of the line based by using the cmap as a LUT with the corresponding cmap_value

# qualitative colormap used for mapping 16 cmap values for each line
# for example, these could be cluster labels
cmap_values = [
0, 1, 1, 2,
0, 0, 1, 1,
2, 2, 3, 3,
1, 1, 1, 5
]

plot = fpl.Plot()

plot.add_line_collection(
circles,
cmap="tab10",
cmap_values=cmap_values,
thickness=10
)

plot.show()

plot.canvas.set_logical_size(800, 800)

if __name__ == "__main__":
fpl.run()
43 changes: 43 additions & 0 deletions examples/line_collection/line_collection_colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Line Plot
============
Example showing how to plot line collections
"""

# test_example = true

from itertools import product
import numpy as np
import fastplotlib as fpl


def make_circle(center, radius: float, n_points: int = 75) -> np.ndarray:
theta = np.linspace(0, 2 * np.pi, n_points)
xs = radius * np.sin(theta)
ys = radius * np.cos(theta)

return np.column_stack([xs, ys]) + center


spatial_dims = (50, 50)

circles = list()
for center in product(range(0, spatial_dims[0], 15), range(0, spatial_dims[1], 15)):
circles.append(make_circle(center, 5, n_points=75))

pos_xy = np.vstack(circles)

# set line collection colors manually
# this will produce 16 circles so we will define 16 colors
colors = ["blue"] * 4 + ["red"] * 4 + ["yellow"] * 4 + ["w"] * 4

plot = fpl.Plot()

plot.add_line_collection(circles, colors=colors, thickness=10)

plot.show()

plot.canvas.set_logical_size(800, 800)

if __name__ == "__main__":
fpl.run()
30 changes: 30 additions & 0 deletions examples/line_collection/line_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Line Plot
============
Example showing how to plot line collections
"""

# test_example = true

import numpy as np
import fastplotlib as fpl


xs = np.linspace(0, 100, 1000)
# sine wave
ys = np.sin(xs) * 20

# make 25 lines
data = np.vstack([ys] * 25)

plot = fpl.Plot()

# line stack takes all the same arguments as line collection and behaves similarly
plot.add_line_stack(data, cmap="jet")

plot.show(maintain_aspect=False)

plot.canvas.set_logical_size(900, 600)

if __name__ == "__main__":
fpl.run()
30 changes: 18 additions & 12 deletions examples/scatter/scatter_cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,41 @@

# test_example = true

from fastplotlib import Plot
from fastplotlib import Plot, run
import numpy as np
from pathlib import Path
from sklearn.cluster import AgglomerativeClustering

from wgpu.gui.offscreen import WgpuCanvas
from pygfx import WgpuRenderer

canvas = WgpuCanvas()
renderer = WgpuRenderer(canvas)

plot = Plot(canvas=canvas, renderer=renderer)
plot = Plot()

data_path = Path(__file__).parent.parent.joinpath("data", "iris.npy")
data = np.load(data_path)

n_points = 50
colors = ["yellow"] * n_points + ["cyan"] * n_points + ["magenta"] * n_points

scatter_graphic = plot.add_scatter(data=data[:, :-1], sizes=6, alpha=0.7, colors=colors)
agg = AgglomerativeClustering(n_clusters=3)

agg.fit_predict(data)


scatter_graphic = plot.add_scatter(
data=data[:, :-1],
sizes=15,
alpha=0.7,
cmap="Set1",
cmap_values=agg.labels_
)

plot.show()

plot.canvas.set_logical_size(800, 800)

plot.auto_scale()

scatter_graphic.cmap = "viridis"
scatter_graphic.cmap = "tab10"

img = np.asarray(plot.renderer.target.draw())
# img = np.asarray(plot.renderer.target.draw())

if __name__ == "__main__":
print(__doc__)
run()
3 changes: 3 additions & 0 deletions examples/screenshots/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/line_collection.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_collection_cmap_values.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading