61
61
my_mpl_kivy_widget = FigureCanvas(fig)
62
62
fig.canvas.mpl_connect('button_press_event', callback_handler)
63
63
64
- 2. Use pyplot to write the application following matplotlib sintax as can be
64
+ 2. Use pyplot to write the application following matplotlib syntax as can be
65
65
seen in the second example below. In this case a Kivy application will be
66
66
created automatically from the matplotlib instructions and a NavigationToolbar
67
67
will be added to the main canvas.
@@ -229,79 +229,87 @@ def close(event):
229
229
unicode_literals ,
230
230
)
231
231
232
- import six
232
+ import numbers
233
233
import os
234
+ import textwrap
235
+ import uuid
236
+
237
+ import numpy as np
238
+ from packaging .version import Version
239
+ import six
240
+
234
241
import matplotlib
242
+ from matplotlib import _path , rcParams
235
243
from matplotlib ._pylab_helpers import Gcf
236
244
from matplotlib .backend_bases import (
237
- RendererBase ,
238
- GraphicsContextBase ,
239
- FigureManagerBase ,
245
+ Event ,
240
246
FigureCanvasBase ,
247
+ FigureManagerBase ,
248
+ GraphicsContextBase ,
249
+ KeyEvent ,
250
+ MouseEvent ,
241
251
NavigationToolbar2 ,
252
+ RendererBase ,
253
+ ResizeEvent ,
254
+ ShowBase ,
242
255
TimerBase ,
243
256
)
244
- from matplotlib .figure import Figure
245
- from matplotlib .transforms import Affine2D
246
- from matplotlib .backend_bases import (ShowBase ,
247
- Event ,
248
- ResizeEvent ,
249
- MouseEvent ,
250
- KeyEvent )
251
257
from matplotlib .backends .backend_agg import FigureCanvasAgg
258
+ from matplotlib .figure import Figure
252
259
from matplotlib .mathtext import MathTextParser
253
- from matplotlib import rcParams
254
- from matplotlib import _path
260
+ from matplotlib .transforms import Affine2D
255
261
256
262
try :
257
263
import kivy
258
264
except ImportError :
259
265
raise ImportError ("this backend requires Kivy to be installed." )
260
266
261
267
from kivy .app import App
268
+ from kivy .base import EventLoop
269
+ from kivy .clock import Clock
270
+ from kivy .core .image import Image
271
+ from kivy .core .text import Label as CoreLabel
272
+ from kivy .core .window import Window
273
+ from kivy .graphics import (
274
+ Color ,
275
+ Line ,
276
+ Mesh ,
277
+ Rectangle ,
278
+ Rotate ,
279
+ StencilPop ,
280
+ StencilPush ,
281
+ StencilUnUse ,
282
+ StencilUse ,
283
+ Translate ,
284
+ )
285
+ from kivy .graphics .context_instructions import PopMatrix , PushMatrix
286
+ from kivy .graphics .instructions import InstructionGroup
287
+ from kivy .graphics .tesselator import Tesselator
262
288
from kivy .graphics .texture import Texture
263
- from kivy .graphics import Rectangle
264
- from kivy .uix . widget import Widget
265
- from kivy .uix . floatlayout import FloatLayout
266
- from kivy .uix . behaviors import FocusBehavior
289
+ from kivy .lang import Builder
290
+ from kivy .logger import Logger
291
+ from kivy .properties import ObjectProperty
292
+ from kivy .resources import resource_find
267
293
from kivy .uix .actionbar import (
268
294
ActionBar ,
269
- ActionView ,
270
295
ActionButton ,
271
- ActionToggleButton ,
272
- ActionPrevious ,
273
296
ActionOverflow ,
297
+ ActionPrevious ,
274
298
ActionSeparator ,
299
+ ActionToggleButton ,
300
+ ActionView ,
275
301
)
276
- from kivy .base import EventLoop
277
- from kivy .core .text import Label as CoreLabel
278
- from kivy .core .image import Image
279
- from kivy .graphics import Color , Line
280
- from kivy .graphics import Rotate , Translate
281
- from kivy .graphics .instructions import InstructionGroup
282
- from kivy .graphics .tesselator import Tesselator
283
- from kivy .graphics .context_instructions import PopMatrix , PushMatrix
284
- from kivy .graphics import StencilPush , StencilPop , StencilUse , StencilUnUse
285
- from kivy .logger import Logger
286
- from kivy .graphics import Mesh
287
- from kivy .resources import resource_find
288
- from kivy .uix .stencilview import StencilView
289
- from kivy .core .window import Window
302
+ from kivy .uix .behaviors import FocusBehavior
303
+ from kivy .uix .floatlayout import FloatLayout
290
304
from kivy .uix .popup import Popup
291
- from kivy .properties import ObjectProperty
292
- from kivy .lang import Builder
293
- from kivy .clock import Clock
294
- from distutils .version import LooseVersion
305
+ from kivy .uix .stencilview import StencilView
306
+ from kivy .uix .widget import Widget
295
307
296
- _mpl_ge_1_5 = LooseVersion (matplotlib .__version__ ) >= LooseVersion ("1.5.0" )
297
- _mpl_ge_2_0 = LooseVersion (matplotlib .__version__ ) >= LooseVersion ("2.0.0" )
308
+ kivy .require ("1.9.1" )
298
309
299
- import numpy as np
300
- import textwrap
301
- import uuid
302
- import numbers
310
+ _mpl_ge_1_5 = Version (matplotlib .__version__ ) >= Version ("1.5.0" )
311
+ _mpl_ge_2_0 = Version (matplotlib .__version__ ) >= Version ("2.0.0" )
303
312
304
- kivy .require ("1.9.1" )
305
313
306
314
toolbar = None
307
315
my_canvas = None
@@ -347,7 +355,7 @@ class Show(ShowBase):
347
355
"""
348
356
349
357
@classmethod
350
- def mainloop (self ):
358
+ def mainloop (cls ):
351
359
app = App .get_running_app ()
352
360
if app is None :
353
361
app = MPLKivyApp (figure = my_canvas , toolbar = toolbar )
@@ -989,8 +997,10 @@ def _init_toolbar(self):
989
997
"""
990
998
basedir = os .path .join (rcParams ["datapath" ], "images" )
991
999
actionview = ActionView ()
992
- actionprevious = ActionPrevious (title = "Navigation" ,
993
- with_previous = False )
1000
+ actionprevious = ActionPrevious (
1001
+ title = "Navigation" ,
1002
+ with_previous = False ,
1003
+ )
994
1004
actionoverflow = ActionOverflow ()
995
1005
actionview .add_widget (actionprevious )
996
1006
actionview .add_widget (actionoverflow )
@@ -1212,13 +1222,16 @@ def on_touch_down(self, touch):
1212
1222
newcoord = self .to_widget (touch .x , touch .y , relative = True )
1213
1223
x = newcoord [0 ]
1214
1224
y = newcoord [1 ]
1225
+
1215
1226
if super (FigureCanvasKivy , self ).on_touch_down (touch ):
1216
1227
return True
1217
1228
if self .collide_point (* touch .pos ):
1218
1229
self .motion_notify_event (x , y )
1219
1230
touch .grab (self )
1220
- if 'button' in touch .profile and touch .button in ("scrollup" ,
1221
- "scrolldown" ):
1231
+ if "button" in touch .profile and touch .button in (
1232
+ "scrollup" ,
1233
+ "scrolldown" ,
1234
+ ):
1222
1235
self .scroll_event (x , y , 5 )
1223
1236
else :
1224
1237
self .button_press_event (x , y , self .get_mouse_button (touch ))
@@ -1269,8 +1282,10 @@ def on_touch_up(self, touch):
1269
1282
x = newcoord [0 ]
1270
1283
y = newcoord [1 ]
1271
1284
if touch .grab_current is self :
1272
- if 'button' in touch .profile and touch .button in ("scrollup" ,
1273
- "scrolldown" ):
1285
+ if "button" in touch .profile and touch .button in (
1286
+ "scrollup" ,
1287
+ "scrolldown" ,
1288
+ ):
1274
1289
self .scroll_event (x , y , 5 )
1275
1290
else :
1276
1291
self .button_release_event (x , y , self .get_mouse_button (touch ))
@@ -1283,13 +1298,13 @@ def keyboard_on_key_down(self, window, keycode, text, modifiers):
1283
1298
"""Kivy event to trigger matplotlib `key_press_event`."""
1284
1299
self .key_press_event (key = keycode [1 ])
1285
1300
return super (FigureCanvasKivy , self ).keyboard_on_key_down (
1286
- window , keycode , text , modifiers )
1301
+ window , keycode , text , modifiers
1302
+ )
1287
1303
1288
1304
def keyboard_on_key_up (self , window , keycode ):
1289
1305
"""Kivy event to trigger matplotlib `key_release_event`."""
1290
1306
self .key_release_event (key = keycode [1 ])
1291
- return super (FigureCanvasKivy , self ).keyboard_on_key_up (
1292
- window , keycode )
1307
+ return super (FigureCanvasKivy , self ).keyboard_on_key_up (window , keycode )
1293
1308
1294
1309
def _on_mouse_pos (self , * args ):
1295
1310
"""Kivy Event to trigger the following matplotlib events:
@@ -1319,67 +1334,58 @@ def leave_notify_event(self, gui_event=None):
1319
1334
self .callbacks .process ("figure_leave_event" , event )
1320
1335
1321
1336
def resize_event (self ):
1322
- event = ResizeEvent (' resize_event' , self )
1323
- self .callbacks .process (' resize_event' , event )
1337
+ event = ResizeEvent (" resize_event" , self )
1338
+ self .callbacks .process (" resize_event" , event )
1324
1339
1325
1340
def motion_notify_event (self , x , y , gui_event = None ):
1326
1341
event = MouseEvent (
1327
- 'motion_notify_event' ,
1328
- canvas = self ,
1329
- x = x ,
1330
- y = y ,
1331
- guiEvent = gui_event )
1332
- self .callbacks .process ('motion_notify_event' , event )
1342
+ "motion_notify_event" , canvas = self , x = x , y = y , guiEvent = gui_event
1343
+ )
1344
+ self .callbacks .process ("motion_notify_event" , event )
1333
1345
1334
- def button_press_event (self , x , y , button ,
1335
- dblclick = False , gui_event = None ):
1346
+ def button_press_event (self , x , y , button , dblclick = False , gui_event = None ):
1336
1347
event = MouseEvent (
1337
- ' button_press_event' ,
1348
+ " button_press_event" ,
1338
1349
canvas = self ,
1339
1350
x = x ,
1340
1351
y = y ,
1341
1352
button = button ,
1342
1353
dblclick = dblclick ,
1343
- guiEvent = gui_event )
1344
- self .callbacks .process ('button_press_event' , event )
1354
+ guiEvent = gui_event ,
1355
+ )
1356
+ self .callbacks .process ("button_press_event" , event )
1345
1357
1346
- def button_release_event (self , x , y , button ,
1347
- dblclick = False , gui_event = None ):
1358
+ def button_release_event (
1359
+ self , x , y , button , dblclick = False , gui_event = None
1360
+ ):
1348
1361
event = MouseEvent (
1349
- ' button_release_event' ,
1362
+ " button_release_event" ,
1350
1363
canvas = self ,
1351
1364
x = x ,
1352
1365
y = y ,
1353
1366
button = button ,
1354
1367
dblclick = dblclick ,
1355
- guiEvent = gui_event )
1356
- self .callbacks .process ('button_release_event' , event )
1368
+ guiEvent = gui_event ,
1369
+ )
1370
+ self .callbacks .process ("button_release_event" , event )
1357
1371
1358
1372
def scroll_event (self , x , y , step , gui_event = None ):
1359
1373
event = MouseEvent (
1360
- 'scroll_event' ,
1361
- canvas = self ,
1362
- x = x ,
1363
- y = y ,
1364
- step = step ,
1365
- guiEvent = gui_event )
1366
- self .callbacks .process ('scroll_event' , event )
1374
+ "scroll_event" , canvas = self , x = x , y = y , step = step , guiEvent = gui_event
1375
+ )
1376
+ self .callbacks .process ("scroll_event" , event )
1367
1377
1368
1378
def key_press_event (self , key , gui_event = None ):
1369
1379
event = KeyEvent (
1370
- 'key_press_event' ,
1371
- canvas = self ,
1372
- key = key ,
1373
- guiEvent = gui_event )
1374
- self .callbacks .process ('key_press_event' , event )
1380
+ "key_press_event" , canvas = self , key = key , guiEvent = gui_event
1381
+ )
1382
+ self .callbacks .process ("key_press_event" , event )
1375
1383
1376
1384
def key_release_event (self , key , gui_event = None ):
1377
1385
event = KeyEvent (
1378
- 'key_release_event' ,
1379
- canvas = self ,
1380
- key = key ,
1381
- guiEvent = gui_event )
1382
- self .callbacks .process ('key_release_event' , event )
1386
+ "key_release_event" , canvas = self , key = key , guiEvent = gui_event
1387
+ )
1388
+ self .callbacks .process ("key_release_event" , event )
1383
1389
1384
1390
def _on_pos_changed (self , * args ):
1385
1391
self .draw ()
0 commit comments