-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathrect_frac_layout.py
More file actions
74 lines (55 loc) · 1.91 KB
/
rect_frac_layout.py
File metadata and controls
74 lines (55 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Rect Fractional Layout
======================
Create subplots using rects given as fractions of the canvas.
This example plots two images and their histograms in separate subplots
"""
# test_example = true
# sphinx_gallery_pygfx_docs = 'screenshot'
import numpy as np
import imageio.v3 as iio
import fastplotlib as fpl
# load images
img1 = iio.imread("imageio:astronaut.png")
img2 = iio.imread("imageio:wikkie.png")
# calculate histograms
hist_1, edges_1 = np.histogram(img1)
centers_1 = edges_1[:-1] + np.diff(edges_1) / 2
hist_2, edges_2 = np.histogram(img2)
centers_2 = edges_2[:-1] + np.diff(edges_2) / 2
# figure size in pixels
size = (700, 560)
# rect is (x, y, width, height)
# here it is defined as fractions of the canvas
rects = [
(0, 0, 0.3, 0.5), # for image1
(0, 0.5, 0.3, 0.5), # for image2
(0.3, 0, 0.7, 0.5), # for image1 histogram
(0.3, 0.5, 0.7, 0.5), # for image2 histogram
]
# create a figure using the rects and size
# also give each subplot a name
figure = fpl.Figure(
rects=rects,
names=["astronaut image", "wikkie image", "astronaut histogram", "wikkie histogram"],
size=size
)
# add image to the corresponding subplots
figure["astronaut image"].add_image(img1)
figure["wikkie image"].add_image(img2)
# add histogram to the corresponding subplots
figure["astronaut histogram"].add_line(np.column_stack([centers_1, hist_1]))
figure["wikkie histogram"].add_line(np.column_stack([centers_2, hist_2]))
for subplot in figure:
if "image" in subplot.name:
# remove axes from image subplots to reduce clutter
subplot.axes.visible = False
continue
# don't maintain aspect ratio for the histogram subplots
subplot.camera.maintain_aspect = False
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()