Skip to content

Commit f83dd4e

Browse files
committed
change some normal attribute for kivy property attribute. These attribute can now be set in Kv directly. Add matplotib formatter to cursor values display.
update twinx exmple nd scatter example.
1 parent 023fa4c commit f83dd4e

File tree

21 files changed

+385
-168
lines changed

21 files changed

+385
-168
lines changed

example_all_interactive_options/graph_widget.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ class MatplotFigure(Widget):
5353
do_pan_x = BooleanProperty(True)
5454
do_pan_y = BooleanProperty(True)
5555
do_zoom_x = BooleanProperty(True)
56-
do_zoom_y = BooleanProperty(True)
56+
do_zoom_y = BooleanProperty(True)
57+
fast_draw = BooleanProperty(True) #True will don't draw axis
58+
xsorted = BooleanProperty(False) #to manage x sorted data
59+
minzoom = NumericProperty(dp(40))
5760

5861
def on_figure(self, obj, value):
5962
self.figcanvas = _FigureCanvas(self.figure, self)
@@ -94,15 +97,13 @@ def __init__(self, **kwargs):
9497
self.xmax = None
9598
self.ymin = None
9699
self.ymax = None
100+
self.lines = []
97101

98102
#option
99-
self.zoompan = None
100-
self.fast_draw=True
101-
self.draw_left_spline=False #available only when fast_draw is True
102103
self.touch_mode='pan'
103104
self.hover_on = False
104-
self.xsorted = False #to manage x sorted data
105-
self.minzoom = dp(40) #minimum pixel distance to apply zoom
105+
self.cursor_xaxis_formatter=None #used matplotlib formatter to display x cursor value
106+
self.cursor_yaxis_formatter=None #used matplotlib formatter to display y cursor value
106107

107108
#zoom box coordonnate
108109
self.x0_box = None
@@ -246,6 +247,10 @@ def hover(self, event) -> None:
246247
self.vertical_line.set_xdata(x)
247248

248249
#x y label
250+
if self.cursor_xaxis_formatter:
251+
x = self.cursor_xaxis_formatter.format_data(x)
252+
if self.cursor_yaxis_formatter:
253+
y = self.cursor_yaxis_formatter.format_data(y)
249254
self.text.set_text(f"x={x}, y={y}")
250255

251256
#blit method (always use because same visual effect as draw)
@@ -481,7 +486,7 @@ def on_touch_down(self, event):
481486
""" Manage Mouse/touch press """
482487
x, y = event.x, event.y
483488

484-
if self.collide_point(x, y):
489+
if self.collide_point(x, y) and self.figure:
485490
if self.legend_instance:
486491
if self.legend_instance.box.collide_point(x, y):
487492
if self.touch_mode!='drag_legend':
@@ -586,7 +591,7 @@ def on_touch_up(self, event):
586591
return True
587592

588593
# stop propagating if its within our bounds
589-
if self.collide_point(x, y):
594+
if self.collide_point(x, y) and self.figure:
590595

591596
if self.do_update:
592597
self.update_lim()
@@ -909,6 +914,7 @@ def draw_box(self, event, x0, y0, x1, y1) -> None:
909914
x1=x0_max[0][0]+pos_x
910915

911916
self._alpha_ver=1
917+
self._alpha_hor=0
912918

913919
elif abs(y1-y0)<dp(20) and abs(x1-x0)>self.minzoom:
914920
self.pos_x_rect_hor=x0
@@ -921,6 +927,7 @@ def draw_box(self, event, x0, y0, x1, y1) -> None:
921927
y1=y0_max[0][1]+pos_y
922928

923929
self._alpha_hor=1
930+
self._alpha_ver=0
924931

925932
else:
926933
self._alpha_hor=0

example_all_interactive_options/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
app.set_zoom_behavior('zoom_y',self.state)
8585
MatplotFigure:
8686
id:figure_wgt
87+
#update axis during pan/zoom
88+
fast_draw:False
8789
'''
8890

8991

@@ -98,7 +100,6 @@ def on_start(self, *args):
98100
mygraph = GraphGenerator()
99101

100102
self.screen.figure_wgt.figure = mygraph.fig
101-
self.screen.figure_wgt.fast_draw = False #update axis during pan/zoom
102103

103104
#register lines instance if need to be update
104105
self.lines.append(mygraph.line1)

example_basic/graph_widget.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ class MatplotFigure(Widget):
5353
do_pan_x = BooleanProperty(True)
5454
do_pan_y = BooleanProperty(True)
5555
do_zoom_x = BooleanProperty(True)
56-
do_zoom_y = BooleanProperty(True)
56+
do_zoom_y = BooleanProperty(True)
57+
fast_draw = BooleanProperty(True) #True will don't draw axis
58+
xsorted = BooleanProperty(False) #to manage x sorted data
59+
minzoom = NumericProperty(dp(40))
5760

5861
def on_figure(self, obj, value):
5962
self.figcanvas = _FigureCanvas(self.figure, self)
@@ -94,15 +97,13 @@ def __init__(self, **kwargs):
9497
self.xmax = None
9598
self.ymin = None
9699
self.ymax = None
100+
self.lines = []
97101

98102
#option
99-
self.zoompan = None
100-
self.fast_draw=True
101-
self.draw_left_spline=False #available only when fast_draw is True
102103
self.touch_mode='pan'
103104
self.hover_on = False
104-
self.xsorted = False #to manage x sorted data
105-
self.minzoom = dp(40) #minimum pixel distance to apply zoom
105+
self.cursor_xaxis_formatter=None #used matplotlib formatter to display x cursor value
106+
self.cursor_yaxis_formatter=None #used matplotlib formatter to display y cursor value
106107

107108
#zoom box coordonnate
108109
self.x0_box = None
@@ -246,6 +247,10 @@ def hover(self, event) -> None:
246247
self.vertical_line.set_xdata(x)
247248

248249
#x y label
250+
if self.cursor_xaxis_formatter:
251+
x = self.cursor_xaxis_formatter.format_data(x)
252+
if self.cursor_yaxis_formatter:
253+
y = self.cursor_yaxis_formatter.format_data(y)
249254
self.text.set_text(f"x={x}, y={y}")
250255

251256
#blit method (always use because same visual effect as draw)
@@ -481,7 +486,7 @@ def on_touch_down(self, event):
481486
""" Manage Mouse/touch press """
482487
x, y = event.x, event.y
483488

484-
if self.collide_point(x, y):
489+
if self.collide_point(x, y) and self.figure:
485490
if self.legend_instance:
486491
if self.legend_instance.box.collide_point(x, y):
487492
if self.touch_mode!='drag_legend':
@@ -586,7 +591,7 @@ def on_touch_up(self, event):
586591
return True
587592

588593
# stop propagating if its within our bounds
589-
if self.collide_point(x, y):
594+
if self.collide_point(x, y) and self.figure:
590595

591596
if self.do_update:
592597
self.update_lim()
@@ -909,6 +914,7 @@ def draw_box(self, event, x0, y0, x1, y1) -> None:
909914
x1=x0_max[0][0]+pos_x
910915

911916
self._alpha_ver=1
917+
self._alpha_hor=0
912918

913919
elif abs(y1-y0)<dp(20) and abs(x1-x0)>self.minzoom:
914920
self.pos_x_rect_hor=x0
@@ -921,6 +927,7 @@ def draw_box(self, event, x0, y0, x1, y1) -> None:
921927
y1=y0_max[0][1]+pos_y
922928

923929
self._alpha_hor=1
930+
self._alpha_ver=0
924931

925932
else:
926933
self._alpha_hor=0

example_basic/main.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ def on_start(self, *args):
5151
mygraph = GraphGenerator()
5252

5353
self.screen.figure_wgt.figure = mygraph.fig
54-
self.screen.figure_wgt.axes = mygraph.ax1
55-
self.screen.figure_wgt.xmin = mygraph.xmin
56-
self.screen.figure_wgt.xmax = mygraph.xmax
57-
self.screen.figure_wgt.ymin = mygraph.ymin
58-
self.screen.figure_wgt.ymax = mygraph.ymax
59-
self.screen.figure_wgt.fast_draw = False #update axis during pan/zoom
60-
61-
#register lines instance if need to be update
62-
self.lines.append(mygraph.line1)
63-
self.lines.append(mygraph.line2)
6454

6555
def set_touch_mode(self,mode):
6656
self.screen.figure_wgt.touch_mode=mode

example_big_data/graph_widget.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ class MatplotFigure(Widget):
5353
do_pan_x = BooleanProperty(True)
5454
do_pan_y = BooleanProperty(True)
5555
do_zoom_x = BooleanProperty(True)
56-
do_zoom_y = BooleanProperty(True)
56+
do_zoom_y = BooleanProperty(True)
57+
fast_draw = BooleanProperty(True) #True will don't draw axis
58+
xsorted = BooleanProperty(False) #to manage x sorted data
59+
minzoom = NumericProperty(dp(40))
5760

5861
def on_figure(self, obj, value):
5962
self.figcanvas = _FigureCanvas(self.figure, self)
@@ -94,15 +97,13 @@ def __init__(self, **kwargs):
9497
self.xmax = None
9598
self.ymin = None
9699
self.ymax = None
100+
self.lines = []
97101

98102
#option
99-
self.zoompan = None
100-
self.fast_draw=True
101-
self.draw_left_spline=False #available only when fast_draw is True
102103
self.touch_mode='pan'
103104
self.hover_on = False
104-
self.xsorted = False #to manage x sorted data
105-
self.minzoom = dp(40) #minimum pixel distance to apply zoom
105+
self.cursor_xaxis_formatter=None #used matplotlib formatter to display x cursor value
106+
self.cursor_yaxis_formatter=None #used matplotlib formatter to display y cursor value
106107

107108
#zoom box coordonnate
108109
self.x0_box = None
@@ -246,6 +247,10 @@ def hover(self, event) -> None:
246247
self.vertical_line.set_xdata(x)
247248

248249
#x y label
250+
if self.cursor_xaxis_formatter:
251+
x = self.cursor_xaxis_formatter.format_data(x)
252+
if self.cursor_yaxis_formatter:
253+
y = self.cursor_yaxis_formatter.format_data(y)
249254
self.text.set_text(f"x={x}, y={y}")
250255

251256
#blit method (always use because same visual effect as draw)
@@ -481,7 +486,7 @@ def on_touch_down(self, event):
481486
""" Manage Mouse/touch press """
482487
x, y = event.x, event.y
483488

484-
if self.collide_point(x, y):
489+
if self.collide_point(x, y) and self.figure:
485490
if self.legend_instance:
486491
if self.legend_instance.box.collide_point(x, y):
487492
if self.touch_mode!='drag_legend':
@@ -586,7 +591,7 @@ def on_touch_up(self, event):
586591
return True
587592

588593
# stop propagating if its within our bounds
589-
if self.collide_point(x, y):
594+
if self.collide_point(x, y) and self.figure:
590595

591596
if self.do_update:
592597
self.update_lim()
@@ -909,6 +914,7 @@ def draw_box(self, event, x0, y0, x1, y1) -> None:
909914
x1=x0_max[0][0]+pos_x
910915

911916
self._alpha_ver=1
917+
self._alpha_hor=0
912918

913919
elif abs(y1-y0)<dp(20) and abs(x1-x0)>self.minzoom:
914920
self.pos_x_rect_hor=x0
@@ -921,6 +927,7 @@ def draw_box(self, event, x0, y0, x1, y1) -> None:
921927
y1=y0_max[0][1]+pos_y
922928

923929
self._alpha_hor=1
930+
self._alpha_ver=0
924931

925932
else:
926933
self._alpha_hor=0

example_big_data/main.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ def on_start(self, *args):
5050
mygraph = GraphGenerator()
5151

5252
self.screen.figure_wgt.figure = mygraph.fig
53-
self.screen.figure_wgt.axes = mygraph.ax1
54-
self.screen.figure_wgt.xmin = mygraph.xmin
55-
self.screen.figure_wgt.xmax = mygraph.xmax
56-
self.screen.figure_wgt.ymin = mygraph.ymin
57-
self.screen.figure_wgt.ymax = mygraph.ymax
5853

5954
def set_touch_mode(self,mode):
6055
self.screen.figure_wgt.touch_mode=mode

example_cursor/graph_widget.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ class MatplotFigure(Widget):
5353
do_pan_x = BooleanProperty(True)
5454
do_pan_y = BooleanProperty(True)
5555
do_zoom_x = BooleanProperty(True)
56-
do_zoom_y = BooleanProperty(True)
56+
do_zoom_y = BooleanProperty(True)
57+
fast_draw = BooleanProperty(True) #True will don't draw axis
58+
xsorted = BooleanProperty(False) #to manage x sorted data
59+
minzoom = NumericProperty(dp(40))
5760

5861
def on_figure(self, obj, value):
5962
self.figcanvas = _FigureCanvas(self.figure, self)
@@ -94,15 +97,13 @@ def __init__(self, **kwargs):
9497
self.xmax = None
9598
self.ymin = None
9699
self.ymax = None
100+
self.lines = []
97101

98102
#option
99-
self.zoompan = None
100-
self.fast_draw=True
101-
self.draw_left_spline=False #available only when fast_draw is True
102103
self.touch_mode='pan'
103104
self.hover_on = False
104-
self.xsorted = False #to manage x sorted data
105-
self.minzoom = dp(40) #minimum pixel distance to apply zoom
105+
self.cursor_xaxis_formatter=None #used matplotlib formatter to display x cursor value
106+
self.cursor_yaxis_formatter=None #used matplotlib formatter to display y cursor value
106107

107108
#zoom box coordonnate
108109
self.x0_box = None
@@ -246,6 +247,10 @@ def hover(self, event) -> None:
246247
self.vertical_line.set_xdata(x)
247248

248249
#x y label
250+
if self.cursor_xaxis_formatter:
251+
x = self.cursor_xaxis_formatter.format_data(x)
252+
if self.cursor_yaxis_formatter:
253+
y = self.cursor_yaxis_formatter.format_data(y)
249254
self.text.set_text(f"x={x}, y={y}")
250255

251256
#blit method (always use because same visual effect as draw)
@@ -481,7 +486,7 @@ def on_touch_down(self, event):
481486
""" Manage Mouse/touch press """
482487
x, y = event.x, event.y
483488

484-
if self.collide_point(x, y):
489+
if self.collide_point(x, y) and self.figure:
485490
if self.legend_instance:
486491
if self.legend_instance.box.collide_point(x, y):
487492
if self.touch_mode!='drag_legend':
@@ -586,7 +591,7 @@ def on_touch_up(self, event):
586591
return True
587592

588593
# stop propagating if its within our bounds
589-
if self.collide_point(x, y):
594+
if self.collide_point(x, y) and self.figure:
590595

591596
if self.do_update:
592597
self.update_lim()
@@ -909,6 +914,7 @@ def draw_box(self, event, x0, y0, x1, y1) -> None:
909914
x1=x0_max[0][0]+pos_x
910915

911916
self._alpha_ver=1
917+
self._alpha_hor=0
912918

913919
elif abs(y1-y0)<dp(20) and abs(x1-x0)>self.minzoom:
914920
self.pos_x_rect_hor=x0
@@ -921,6 +927,7 @@ def draw_box(self, event, x0, y0, x1, y1) -> None:
921927
y1=y0_max[0][1]+pos_y
922928

923929
self._alpha_hor=1
930+
self._alpha_ver=0
924931

925932
else:
926933
self._alpha_hor=0

example_cursor/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
self.state='down'
3838
MatplotFigure:
3939
id:figure_wgt
40+
#update axis during pan/zoom
41+
fast_draw:False
4042
'''
4143

4244

@@ -51,8 +53,7 @@ def on_start(self, *args):
5153
mygraph = GraphGenerator()
5254

5355
self.screen.figure_wgt.figure = mygraph.fig
54-
self.screen.figure_wgt.fast_draw = False #update axis during pan/zoom
55-
56+
5657
#register lines instance if need to be update
5758
self.lines.append(mygraph.line1)
5859
self.lines.append(mygraph.line2)

example_cursor_scatter_experimental/graph_generator.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,12 @@ def __init__(self):
3535
self.x = [2,4,5,7,6,8,9,11,12,12]
3636
self.y = [1,2,3,4,5,6,7,8,9,10]
3737

38-
self.line1 = self.ax1.scatter(self.x, self.y, s=30, color='magenta', alpha=0.7, marker='x', picker=3)
38+
self.scatter1 = self.ax1.scatter(self.x, self.y, s=30, color='magenta', alpha=0.7, marker='x', picker=3)
3939
for i, ptlbl in enumerate(self.ptid):
4040
self.ax1.annotate(ptlbl, (self.x[i], self.y[i]),xytext=(5,0),textcoords='offset points',size=8,color='darkslategrey')
41-
42-
self.xmin,self.xmax = self.ax1.get_xlim()
43-
self.ymin,self.ymax = self.ax1.get_ylim()
4441

4542
self.fig.subplots_adjust(left=0.13,top=0.96,right=0.93,bottom=0.2)
46-
47-
self.ax1.set_xlim(self.xmin, self.xmax)
48-
self.ax1.set_ylim(self.ymin, self.ymax)
43+
4944
self.ax1.set_xlabel("axis_x",fontsize=font_size_axis_title)
5045
self.ax1.set_ylabel("axis_y",fontsize=font_size_axis_title)
5146

0 commit comments

Comments
 (0)