Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1d6adc6

Browse files
committed
update selector example nbs, still WIP
1 parent 4d4d636 commit 1d6adc6

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

examples/notebooks/linear_region_selector.ipynb

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
"source": [
1818
"import fastplotlib as fpl\n",
1919
"import numpy as np\n",
20-
"from ipywidgets import IntRangeSlider, FloatRangeSlider, VBox\n",
20+
"# from ipywidgets import IntRangeSlider, FloatRangeSlider, VBox\n",
2121
"\n",
2222
"fig = fpl.Figure((2, 2))\n",
2323
"\n",
2424
"# preallocated size for zoomed data\n",
2525
"zoomed_prealloc = 1_000\n",
2626
"\n",
2727
"# data to plot\n",
28-
"xs = np.linspace(0, 100, 1_000)\n",
29-
"sine = np.sin(xs) * 20\n",
28+
"xs = np.linspace(0, 10* np.pi, 1_000)\n",
29+
"sine = np.sin(xs)\n",
30+
"sine += 100\n",
3031
"\n",
3132
"# make sine along x axis\n",
32-
"sine_graphic_x = fig[0, 0].add_line(sine)\n",
33+
"sine_graphic_x = fig[0, 0].add_line(np.column_stack([xs, sine]), offset=(10, 0, 0))\n",
3334
"\n",
3435
"# just something that looks different for line along y-axis\n",
3536
"sine_y = sine\n",
@@ -47,7 +48,7 @@
4748
"ls_y = sine_graphic_y.add_linear_region_selector(axis=\"y\")\n",
4849
"\n",
4950
"# preallocate array for storing zoomed in data\n",
50-
"zoomed_init = np.column_stack([np.arange(zoomed_prealloc), np.random.rand(zoomed_prealloc)])\n",
51+
"zoomed_init = np.column_stack([np.arange(zoomed_prealloc), np.zeros(zoomed_prealloc)])\n",
5152
"\n",
5253
"# make line graphics for displaying zoomed data\n",
5354
"zoomed_x = fig[1, 0].add_line(zoomed_init)\n",
@@ -62,54 +63,54 @@
6263
" # interpolate to preallocated size\n",
6364
" return np.interp(x, xp, fp=subdata[:, axis]) # use the y-values\n",
6465
"\n",
65-
"\n",
66+
"@ls_x.add_event_handler(\"selection\")\n",
6667
"def set_zoom_x(ev):\n",
6768
" \"\"\"sets zoomed x selector data\"\"\"\n",
68-
" selected_data = ev.pick_info[\"selected_data\"]\n",
69-
" zoomed_x.data = interpolate(selected_data, axis=1) # use the y-values\n",
69+
" # get the selected data\n",
70+
" selected_data = ev.get_selected_data()\n",
71+
" if selected_data.size == 0:\n",
72+
" # no data selected\n",
73+
" zoomed_x.data[:, 1] = 0\n",
74+
"\n",
75+
" # set the y-values\n",
76+
" zoomed_x.data[:, 1] = interpolate(selected_data, axis=1)\n",
7077
" fig[1, 0].auto_scale()\n",
7178
"\n",
7279
"\n",
7380
"def set_zoom_y(ev):\n",
74-
" \"\"\"sets zoomed y selector data\"\"\"\n",
75-
" selected_data = ev.pick_info[\"selected_data\"]\n",
76-
" zoomed_y.data = -interpolate(selected_data, axis=0) # use the x-values\n",
81+
" \"\"\"sets zoomed x selector data\"\"\"\n",
82+
" # get the selected data\n",
83+
" selected_data = ev.get_selected_data()\n",
84+
" if selected_data.size == 0:\n",
85+
" # no data selected\n",
86+
" zoomed_y.data[:, 0] = 0\n",
87+
"\n",
88+
" # set the x-values\n",
89+
" zoomed_y.data[:, 0] = -interpolate(selected_data, axis=1)\n",
7790
" fig[1, 1].auto_scale()\n",
7891
"\n",
7992
"\n",
80-
"# update zoomed plots when bounds change\n",
81-
"ls_x.selection.add_event_handler(set_zoom_x)\n",
82-
"ls_y.selection.add_event_handler(set_zoom_y)\n",
83-
"\n",
84-
"fig.show()"
85-
]
86-
},
87-
{
88-
"cell_type": "markdown",
89-
"id": "0bad4a35-f860-4f85-9061-920154ab682b",
90-
"metadata": {},
91-
"source": [
92-
"### On the x-axis we have a 1-1 mapping from the data that we have passed and the line geometry positions. So the `bounds` min max corresponds directly to the data indices."
93+
"fig.show(maintain_aspect=False)"
9394
]
9495
},
9596
{
9697
"cell_type": "code",
9798
"execution_count": null,
98-
"id": "2c96a3ff-c2e7-4683-8097-8491e97dd6d3",
99+
"id": "2f29e913-c4f8-44a6-8692-eb14436849a5",
99100
"metadata": {},
100101
"outputs": [],
101102
"source": [
102-
"ls_x.selection()"
103+
"sine_graphic_x.data[:, 1].ptp()"
103104
]
104105
},
105106
{
106107
"cell_type": "code",
107108
"execution_count": null,
108-
"id": "3ec71e3f-291c-43c6-a954-0a082ba5981c",
109+
"id": "1947a477-5dd2-4df9-aecd-6967c6ab45fe",
109110
"metadata": {},
110111
"outputs": [],
111112
"source": [
112-
"ls_x.get_selected_indices()"
113+
"np.clip(-0.1, 0, 10)"
113114
]
114115
},
115116
{

examples/notebooks/linear_selector.ipynb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"id": "a06e1fd9-47df-42a3-a76c-19e23d7b89fd",
66
"metadata": {},
77
"source": [
8-
"## `LinearSelector`, draggable selector that can optionally associated with an ipywidget."
8+
"## `LinearSelector`, draggable selector that can also be linked to an ipywidget slider"
99
]
1010
},
1111
{
@@ -16,7 +16,6 @@
1616
"outputs": [],
1717
"source": [
1818
"import fastplotlib as fpl\n",
19-
"from fastplotlib.graphics.selectors import Synchronizer\n",
2019
"\n",
2120
"import numpy as np\n",
2221
"from ipywidgets import VBox, IntSlider, FloatSlider\n",
@@ -35,16 +34,14 @@
3534
"selector2 = sine_graphic.add_linear_selector(20)\n",
3635
"selector3 = sine_graphic.add_linear_selector(40)\n",
3736
"\n",
38-
"ss = Synchronizer(selector, selector2, selector3)\n",
39-
"\n",
37+
"# one of the selectors will change the line colors when it moves\n",
38+
"@selector.add_event_handler(\"selection\")\n",
4039
"def set_color_at_index(ev):\n",
4140
" # changes the color at the index where the slider is\n",
42-
" ix = ev.pick_info[\"selected_index\"]\n",
43-
" g = ev.pick_info[\"graphic\"].parent\n",
41+
" ix = ev.get_selected_index()\n",
42+
" g = ev.graphic.parent\n",
4443
" g.colors[ix] = \"green\"\n",
4544
"\n",
46-
"selector.selection.add_event_handler(set_color_at_index)\n",
47-
"\n",
4845
"# fastplotlib LineSelector can make an ipywidget slider and return it :D \n",
4946
"ipywidget_slider = selector.make_ipywidget_slider()\n",
5047
"ipywidget_slider.description = \"slider1\"\n",
@@ -57,7 +54,15 @@
5754
"selector3.add_ipywidget_handler(ipywidget_slider3, step=0.1)\n",
5855
"\n",
5956
"fig[0, 0].auto_scale()\n",
60-
"fig.show(add_widgets=[ipywidget_slider])"
57+
"VBox([fig.show(), ipywidget_slider, ipywidget_slider2, ipywidget_slider3])"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"id": "d83caca6-e9b6-45df-b93c-0dfe0498d20e",
63+
"metadata": {},
64+
"source": [
65+
"Double click the first selctor, and then use `Shift` + Right/Left Arrow Key to move it!"
6166
]
6267
},
6368
{
@@ -67,13 +72,16 @@
6772
"metadata": {},
6873
"outputs": [],
6974
"source": [
75+
"# this controls the step-size of arrow key movements\n",
7076
"selector.step = 0.1"
7177
]
7278
},
7379
{
7480
"cell_type": "markdown",
7581
"id": "3b0f448f-bbe4-4b87-98e3-093f561c216c",
76-
"metadata": {},
82+
"metadata": {
83+
"jp-MarkdownHeadingCollapsed": true
84+
},
7785
"source": [
7886
"### Drag linear selectors with the mouse, hold \"Shift\" to synchronize movement of all the selectors"
7987
]

0 commit comments

Comments
 (0)