Skip to content

Commit 18d2389

Browse files
authored
add text to hlut (#348)
1 parent ce5edd0 commit 18d2389

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

fastplotlib/widgets/histogram_lut.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
from typing import *
12
import weakref
23

34
import numpy as np
45

56
from pygfx import Group
67

7-
from ..graphics import LineGraphic, ImageGraphic
8+
from ..graphics import LineGraphic, ImageGraphic, TextGraphic
89
from ..graphics._base import Graphic
910
from ..graphics.selectors import LinearRegionSelector
1011

@@ -66,19 +67,63 @@ def __init__(
6667
self._vmin = self.image_graphic.cmap.vmin
6768
self._vmax = self.image_graphic.cmap.vmax
6869

70+
vmin_str, vmax_str = self._get_vmin_vmax_str()
71+
72+
self._text_vmin = TextGraphic(
73+
text=vmin_str,
74+
size=16,
75+
position=(0, 0),
76+
anchor="top-left",
77+
outline_color="black",
78+
outline_thickness=1,
79+
)
80+
81+
self._text_vmax = TextGraphic(
82+
text=vmax_str,
83+
size=16,
84+
position=(0, 0),
85+
anchor="bottom-left",
86+
outline_color="black",
87+
outline_thickness=1,
88+
)
89+
6990
widget_wo = Group()
70-
widget_wo.add(self.line.world_object, self.linear_region.world_object)
91+
widget_wo.add(
92+
self.line.world_object,
93+
self.linear_region.world_object,
94+
self._text_vmin.world_object,
95+
self._text_vmax.world_object,
96+
)
7197

7298
self._set_world_object(widget_wo)
7399

74100
self.world_object.local.scale_x *= -1
75101

102+
self._text_vmin.position_x = -120
103+
self._text_vmin.position_y = self.linear_region.selection()[0]
104+
105+
self._text_vmax.position_x = -120
106+
self._text_vmax.position_y = self.linear_region.selection()[1]
107+
76108
self.linear_region.selection.add_event_handler(
77109
self._linear_region_handler
78110
)
79111

80112
self.image_graphic.cmap.add_event_handler(self._image_cmap_handler)
81113

114+
def _get_vmin_vmax_str(self) -> Tuple[str, str]:
115+
if self.vmin < 0.001 or self.vmin > 99_999:
116+
vmin_str = f"{self.vmin:.2e}"
117+
else:
118+
vmin_str = f"{self.vmin:.2f}"
119+
120+
if self.vmax < 0.001 or self.vmax > 99_999:
121+
vmax_str = f"{self.vmax:.2e}"
122+
else:
123+
vmax_str = f"{self.vmax:.2f}"
124+
125+
return vmin_str, vmax_str
126+
82127
def _add_plot_area_hook(self, plot_area):
83128
self._plot_area = plot_area
84129
self.linear_region._add_plot_area_hook(plot_area)
@@ -168,6 +213,10 @@ def vmin(self, value: float):
168213

169214
self._vmin = value
170215

216+
vmin_str, vmax_str = self._get_vmin_vmax_str()
217+
self._text_vmin.position_y = self.linear_region.selection()[0]
218+
self._text_vmin.text = vmin_str
219+
171220
@property
172221
def vmax(self) -> float:
173222
return self._vmax
@@ -185,6 +234,10 @@ def vmax(self, value: float):
185234

186235
self._vmax = value
187236

237+
vmin_str, vmax_str = self._get_vmin_vmax_str()
238+
self._text_vmax.position_y = self.linear_region.selection()[1]
239+
self._text_vmax.text = vmax_str
240+
188241
def set_data(self, data, reset_vmin_vmax: bool = True):
189242
hist, edges, hist_scaled, edges_flanked = self._calculate_histogram(data)
190243

fastplotlib/widgets/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def __init__(
559559
)
560560

561561
subplot.docks["right"].add_graphic(hlut)
562-
subplot.docks["right"].size = 50
562+
subplot.docks["right"].size = 80
563563
subplot.docks["right"].auto_scale(maintain_aspect=False)
564564
subplot.docks["right"].controller.enabled = False
565565

0 commit comments

Comments
 (0)