6161 my_mpl_kivy_widget = FigureCanvas(fig)
6262 fig.canvas.mpl_connect('button_press_event', callback_handler)
6363
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
6565seen in the second example below. In this case a Kivy application will be
6666created automatically from the matplotlib instructions and a NavigationToolbar
6767will be added to the main canvas.
@@ -229,79 +229,87 @@ def close(event):
229229 unicode_literals ,
230230)
231231
232- import six
232+ import numbers
233233import os
234+ import textwrap
235+ import uuid
236+
237+ import numpy as np
238+ from packaging .version import Version
239+ import six
240+
234241import matplotlib
242+ from matplotlib import _path , rcParams
235243from matplotlib ._pylab_helpers import Gcf
236244from matplotlib .backend_bases import (
237- RendererBase ,
238- GraphicsContextBase ,
239- FigureManagerBase ,
245+ Event ,
240246 FigureCanvasBase ,
247+ FigureManagerBase ,
248+ GraphicsContextBase ,
249+ KeyEvent ,
250+ MouseEvent ,
241251 NavigationToolbar2 ,
252+ RendererBase ,
253+ ResizeEvent ,
254+ ShowBase ,
242255 TimerBase ,
243256)
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 )
251257from matplotlib .backends .backend_agg import FigureCanvasAgg
258+ from matplotlib .figure import Figure
252259from matplotlib .mathtext import MathTextParser
253- from matplotlib import rcParams
254- from matplotlib import _path
260+ from matplotlib .transforms import Affine2D
255261
256262try :
257263 import kivy
258264except ImportError :
259265 raise ImportError ("this backend requires Kivy to be installed." )
260266
261267from 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
262288from 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
267293from kivy .uix .actionbar import (
268294 ActionBar ,
269- ActionView ,
270295 ActionButton ,
271- ActionToggleButton ,
272- ActionPrevious ,
273296 ActionOverflow ,
297+ ActionPrevious ,
274298 ActionSeparator ,
299+ ActionToggleButton ,
300+ ActionView ,
275301)
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
290304from 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
295307
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" )
298309
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" )
303312
304- kivy .require ("1.9.1" )
305313
306314toolbar = None
307315my_canvas = None
@@ -347,7 +355,7 @@ class Show(ShowBase):
347355 """
348356
349357 @classmethod
350- def mainloop (self ):
358+ def mainloop (cls ):
351359 app = App .get_running_app ()
352360 if app is None :
353361 app = MPLKivyApp (figure = my_canvas , toolbar = toolbar )
@@ -989,8 +997,10 @@ def _init_toolbar(self):
989997 """
990998 basedir = os .path .join (rcParams ["datapath" ], "images" )
991999 actionview = ActionView ()
992- actionprevious = ActionPrevious (title = "Navigation" ,
993- with_previous = False )
1000+ actionprevious = ActionPrevious (
1001+ title = "Navigation" ,
1002+ with_previous = False ,
1003+ )
9941004 actionoverflow = ActionOverflow ()
9951005 actionview .add_widget (actionprevious )
9961006 actionview .add_widget (actionoverflow )
@@ -1212,13 +1222,16 @@ def on_touch_down(self, touch):
12121222 newcoord = self .to_widget (touch .x , touch .y , relative = True )
12131223 x = newcoord [0 ]
12141224 y = newcoord [1 ]
1225+
12151226 if super (FigureCanvasKivy , self ).on_touch_down (touch ):
12161227 return True
12171228 if self .collide_point (* touch .pos ):
12181229 self .motion_notify_event (x , y )
12191230 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+ ):
12221235 self .scroll_event (x , y , 5 )
12231236 else :
12241237 self .button_press_event (x , y , self .get_mouse_button (touch ))
@@ -1269,8 +1282,10 @@ def on_touch_up(self, touch):
12691282 x = newcoord [0 ]
12701283 y = newcoord [1 ]
12711284 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+ ):
12741289 self .scroll_event (x , y , 5 )
12751290 else :
12761291 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):
12831298 """Kivy event to trigger matplotlib `key_press_event`."""
12841299 self .key_press_event (key = keycode [1 ])
12851300 return super (FigureCanvasKivy , self ).keyboard_on_key_down (
1286- window , keycode , text , modifiers )
1301+ window , keycode , text , modifiers
1302+ )
12871303
12881304 def keyboard_on_key_up (self , window , keycode ):
12891305 """Kivy event to trigger matplotlib `key_release_event`."""
12901306 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 )
12931308
12941309 def _on_mouse_pos (self , * args ):
12951310 """Kivy Event to trigger the following matplotlib events:
@@ -1319,67 +1334,58 @@ def leave_notify_event(self, gui_event=None):
13191334 self .callbacks .process ("figure_leave_event" , event )
13201335
13211336 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 )
13241339
13251340 def motion_notify_event (self , x , y , gui_event = None ):
13261341 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 )
13331345
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 ):
13361347 event = MouseEvent (
1337- ' button_press_event' ,
1348+ " button_press_event" ,
13381349 canvas = self ,
13391350 x = x ,
13401351 y = y ,
13411352 button = button ,
13421353 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 )
13451357
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+ ):
13481361 event = MouseEvent (
1349- ' button_release_event' ,
1362+ " button_release_event" ,
13501363 canvas = self ,
13511364 x = x ,
13521365 y = y ,
13531366 button = button ,
13541367 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 )
13571371
13581372 def scroll_event (self , x , y , step , gui_event = None ):
13591373 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 )
13671377
13681378 def key_press_event (self , key , gui_event = None ):
13691379 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 )
13751383
13761384 def key_release_event (self , key , gui_event = None ):
13771385 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 )
13831389
13841390 def _on_pos_changed (self , * args ):
13851391 self .draw ()
0 commit comments