|
17 | 17 | "source": [
|
18 | 18 | "import fastplotlib as fpl\n",
|
19 | 19 | "import numpy as np\n",
|
20 |
| - "from ipywidgets import IntRangeSlider, FloatRangeSlider, VBox\n", |
| 20 | + "# from ipywidgets import IntRangeSlider, FloatRangeSlider, VBox\n", |
21 | 21 | "\n",
|
22 | 22 | "fig = fpl.Figure((2, 2))\n",
|
23 | 23 | "\n",
|
24 | 24 | "# preallocated size for zoomed data\n",
|
25 | 25 | "zoomed_prealloc = 1_000\n",
|
26 | 26 | "\n",
|
27 | 27 | "# 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", |
30 | 31 | "\n",
|
31 | 32 | "# 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", |
33 | 34 | "\n",
|
34 | 35 | "# just something that looks different for line along y-axis\n",
|
35 | 36 | "sine_y = sine\n",
|
|
47 | 48 | "ls_y = sine_graphic_y.add_linear_region_selector(axis=\"y\")\n",
|
48 | 49 | "\n",
|
49 | 50 | "# 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", |
51 | 52 | "\n",
|
52 | 53 | "# make line graphics for displaying zoomed data\n",
|
53 | 54 | "zoomed_x = fig[1, 0].add_line(zoomed_init)\n",
|
|
62 | 63 | " # interpolate to preallocated size\n",
|
63 | 64 | " return np.interp(x, xp, fp=subdata[:, axis]) # use the y-values\n",
|
64 | 65 | "\n",
|
65 |
| - "\n", |
| 66 | + "@ls_x.add_event_handler(\"selection\")\n", |
66 | 67 | "def set_zoom_x(ev):\n",
|
67 | 68 | " \"\"\"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", |
70 | 77 | " fig[1, 0].auto_scale()\n",
|
71 | 78 | "\n",
|
72 | 79 | "\n",
|
73 | 80 | "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", |
77 | 90 | " fig[1, 1].auto_scale()\n",
|
78 | 91 | "\n",
|
79 | 92 | "\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)" |
93 | 94 | ]
|
94 | 95 | },
|
95 | 96 | {
|
96 | 97 | "cell_type": "code",
|
97 | 98 | "execution_count": null,
|
98 |
| - "id": "2c96a3ff-c2e7-4683-8097-8491e97dd6d3", |
| 99 | + "id": "2f29e913-c4f8-44a6-8692-eb14436849a5", |
99 | 100 | "metadata": {},
|
100 | 101 | "outputs": [],
|
101 | 102 | "source": [
|
102 |
| - "ls_x.selection()" |
| 103 | + "sine_graphic_x.data[:, 1].ptp()" |
103 | 104 | ]
|
104 | 105 | },
|
105 | 106 | {
|
106 | 107 | "cell_type": "code",
|
107 | 108 | "execution_count": null,
|
108 |
| - "id": "3ec71e3f-291c-43c6-a954-0a082ba5981c", |
| 109 | + "id": "1947a477-5dd2-4df9-aecd-6967c6ab45fe", |
109 | 110 | "metadata": {},
|
110 | 111 | "outputs": [],
|
111 | 112 | "source": [
|
112 |
| - "ls_x.get_selected_indices()" |
| 113 | + "np.clip(-0.1, 0, 10)" |
113 | 114 | ]
|
114 | 115 | },
|
115 | 116 | {
|
|
0 commit comments