widgets.py 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185
  1. """
  2. GUI neutral widgets
  3. ===================
  4. Widgets that are designed to work for any of the GUI backends.
  5. All of these widgets require you to predefine an `~.axes.Axes`
  6. instance and pass that as the first parameter. Matplotlib doesn't try to
  7. be too smart with respect to layout -- you will have to figure out how
  8. wide and tall you want your Axes to be to accommodate your widget.
  9. """
  10. from contextlib import ExitStack
  11. import copy
  12. import itertools
  13. from numbers import Integral, Number
  14. from cycler import cycler
  15. import numpy as np
  16. import matplotlib as mpl
  17. from . import (_api, _docstring, backend_tools, cbook, collections, colors,
  18. text as mtext, ticker, transforms)
  19. from .lines import Line2D
  20. from .patches import Rectangle, Ellipse, Polygon
  21. from .transforms import TransformedPatchPath, Affine2D
  22. class LockDraw:
  23. """
  24. Some widgets, like the cursor, draw onto the canvas, and this is not
  25. desirable under all circumstances, like when the toolbar is in zoom-to-rect
  26. mode and drawing a rectangle. To avoid this, a widget can acquire a
  27. canvas' lock with ``canvas.widgetlock(widget)`` before drawing on the
  28. canvas; this will prevent other widgets from doing so at the same time (if
  29. they also try to acquire the lock first).
  30. """
  31. def __init__(self):
  32. self._owner = None
  33. def __call__(self, o):
  34. """Reserve the lock for *o*."""
  35. if not self.available(o):
  36. raise ValueError('already locked')
  37. self._owner = o
  38. def release(self, o):
  39. """Release the lock from *o*."""
  40. if not self.available(o):
  41. raise ValueError('you do not own this lock')
  42. self._owner = None
  43. def available(self, o):
  44. """Return whether drawing is available to *o*."""
  45. return not self.locked() or self.isowner(o)
  46. def isowner(self, o):
  47. """Return whether *o* owns this lock."""
  48. return self._owner is o
  49. def locked(self):
  50. """Return whether the lock is currently held by an owner."""
  51. return self._owner is not None
  52. class Widget:
  53. """
  54. Abstract base class for GUI neutral widgets.
  55. """
  56. drawon = True
  57. eventson = True
  58. _active = True
  59. def set_active(self, active):
  60. """Set whether the widget is active."""
  61. self._active = active
  62. def get_active(self):
  63. """Get whether the widget is active."""
  64. return self._active
  65. # set_active is overridden by SelectorWidgets.
  66. active = property(get_active, set_active, doc="Is the widget active?")
  67. def ignore(self, event):
  68. """
  69. Return whether *event* should be ignored.
  70. This method should be called at the beginning of any event callback.
  71. """
  72. return not self.active
  73. class AxesWidget(Widget):
  74. """
  75. Widget connected to a single `~matplotlib.axes.Axes`.
  76. To guarantee that the widget remains responsive and not garbage-collected,
  77. a reference to the object should be maintained by the user.
  78. This is necessary because the callback registry
  79. maintains only weak-refs to the functions, which are member
  80. functions of the widget. If there are no references to the widget
  81. object it may be garbage collected which will disconnect the callbacks.
  82. Attributes
  83. ----------
  84. ax : `~matplotlib.axes.Axes`
  85. The parent Axes for the widget.
  86. canvas : `~matplotlib.backend_bases.FigureCanvasBase`
  87. The parent figure canvas for the widget.
  88. active : bool
  89. If False, the widget does not respond to events.
  90. """
  91. def __init__(self, ax):
  92. self.ax = ax
  93. self._cids = []
  94. canvas = property(lambda self: self.ax.get_figure(root=True).canvas)
  95. def connect_event(self, event, callback):
  96. """
  97. Connect a callback function with an event.
  98. This should be used in lieu of ``figure.canvas.mpl_connect`` since this
  99. function stores callback ids for later clean up.
  100. """
  101. cid = self.canvas.mpl_connect(event, callback)
  102. self._cids.append(cid)
  103. def disconnect_events(self):
  104. """Disconnect all events created by this widget."""
  105. for c in self._cids:
  106. self.canvas.mpl_disconnect(c)
  107. def _get_data_coords(self, event):
  108. """Return *event*'s data coordinates in this widget's Axes."""
  109. # This method handles the possibility that event.inaxes != self.ax (which may
  110. # occur if multiple Axes are overlaid), in which case event.xdata/.ydata will
  111. # be wrong. Note that we still special-case the common case where
  112. # event.inaxes == self.ax and avoid re-running the inverse data transform,
  113. # because that can introduce floating point errors for synthetic events.
  114. return ((event.xdata, event.ydata) if event.inaxes is self.ax
  115. else self.ax.transData.inverted().transform((event.x, event.y)))
  116. class Button(AxesWidget):
  117. """
  118. A GUI neutral button.
  119. For the button to remain responsive you must keep a reference to it.
  120. Call `.on_clicked` to connect to the button.
  121. Attributes
  122. ----------
  123. ax
  124. The `~.axes.Axes` the button renders into.
  125. label
  126. A `.Text` instance.
  127. color
  128. The color of the button when not hovering.
  129. hovercolor
  130. The color of the button when hovering.
  131. """
  132. def __init__(self, ax, label, image=None,
  133. color='0.85', hovercolor='0.95', *, useblit=True):
  134. """
  135. Parameters
  136. ----------
  137. ax : `~matplotlib.axes.Axes`
  138. The `~.axes.Axes` instance the button will be placed into.
  139. label : str
  140. The button text.
  141. image : array-like or PIL Image
  142. The image to place in the button, if not *None*. The parameter is
  143. directly forwarded to `~.axes.Axes.imshow`.
  144. color : :mpltype:`color`
  145. The color of the button when not activated.
  146. hovercolor : :mpltype:`color`
  147. The color of the button when the mouse is over it.
  148. useblit : bool, default: True
  149. Use blitting for faster drawing if supported by the backend.
  150. See the tutorial :ref:`blitting` for details.
  151. .. versionadded:: 3.7
  152. """
  153. super().__init__(ax)
  154. if image is not None:
  155. ax.imshow(image)
  156. self.label = ax.text(0.5, 0.5, label,
  157. verticalalignment='center',
  158. horizontalalignment='center',
  159. transform=ax.transAxes)
  160. self._useblit = useblit and self.canvas.supports_blit
  161. self._observers = cbook.CallbackRegistry(signals=["clicked"])
  162. self.connect_event('button_press_event', self._click)
  163. self.connect_event('button_release_event', self._release)
  164. self.connect_event('motion_notify_event', self._motion)
  165. ax.set_navigate(False)
  166. ax.set_facecolor(color)
  167. ax.set_xticks([])
  168. ax.set_yticks([])
  169. self.color = color
  170. self.hovercolor = hovercolor
  171. def _click(self, event):
  172. if not self.eventson or self.ignore(event) or not self.ax.contains(event)[0]:
  173. return
  174. if event.canvas.mouse_grabber != self.ax:
  175. event.canvas.grab_mouse(self.ax)
  176. def _release(self, event):
  177. if self.ignore(event) or event.canvas.mouse_grabber != self.ax:
  178. return
  179. event.canvas.release_mouse(self.ax)
  180. if self.eventson and self.ax.contains(event)[0]:
  181. self._observers.process('clicked', event)
  182. def _motion(self, event):
  183. if self.ignore(event):
  184. return
  185. c = self.hovercolor if self.ax.contains(event)[0] else self.color
  186. if not colors.same_color(c, self.ax.get_facecolor()):
  187. self.ax.set_facecolor(c)
  188. if self.drawon:
  189. if self._useblit:
  190. self.ax.draw_artist(self.ax)
  191. self.canvas.blit(self.ax.bbox)
  192. else:
  193. self.canvas.draw()
  194. def on_clicked(self, func):
  195. """
  196. Connect the callback function *func* to button click events.
  197. Returns a connection id, which can be used to disconnect the callback.
  198. """
  199. return self._observers.connect('clicked', lambda event: func(event))
  200. def disconnect(self, cid):
  201. """Remove the callback function with connection id *cid*."""
  202. self._observers.disconnect(cid)
  203. class SliderBase(AxesWidget):
  204. """
  205. The base class for constructing Slider widgets. Not intended for direct
  206. usage.
  207. For the slider to remain responsive you must maintain a reference to it.
  208. """
  209. def __init__(self, ax, orientation, closedmin, closedmax,
  210. valmin, valmax, valfmt, dragging, valstep):
  211. if ax.name == '3d':
  212. raise ValueError('Sliders cannot be added to 3D Axes')
  213. super().__init__(ax)
  214. _api.check_in_list(['horizontal', 'vertical'], orientation=orientation)
  215. self.orientation = orientation
  216. self.closedmin = closedmin
  217. self.closedmax = closedmax
  218. self.valmin = valmin
  219. self.valmax = valmax
  220. self.valstep = valstep
  221. self.drag_active = False
  222. self.valfmt = valfmt
  223. if orientation == "vertical":
  224. ax.set_ylim((valmin, valmax))
  225. axis = ax.yaxis
  226. else:
  227. ax.set_xlim((valmin, valmax))
  228. axis = ax.xaxis
  229. self._fmt = axis.get_major_formatter()
  230. if not isinstance(self._fmt, ticker.ScalarFormatter):
  231. self._fmt = ticker.ScalarFormatter()
  232. self._fmt.set_axis(axis)
  233. self._fmt.set_useOffset(False) # No additive offset.
  234. self._fmt.set_useMathText(True) # x sign before multiplicative offset.
  235. ax.set_axis_off()
  236. ax.set_navigate(False)
  237. self.connect_event("button_press_event", self._update)
  238. self.connect_event("button_release_event", self._update)
  239. if dragging:
  240. self.connect_event("motion_notify_event", self._update)
  241. self._observers = cbook.CallbackRegistry(signals=["changed"])
  242. def _stepped_value(self, val):
  243. """Return *val* coerced to closest number in the ``valstep`` grid."""
  244. if isinstance(self.valstep, Number):
  245. val = (self.valmin
  246. + round((val - self.valmin) / self.valstep) * self.valstep)
  247. elif self.valstep is not None:
  248. valstep = np.asanyarray(self.valstep)
  249. if valstep.ndim != 1:
  250. raise ValueError(
  251. f"valstep must have 1 dimension but has {valstep.ndim}"
  252. )
  253. val = valstep[np.argmin(np.abs(valstep - val))]
  254. return val
  255. def disconnect(self, cid):
  256. """
  257. Remove the observer with connection id *cid*.
  258. Parameters
  259. ----------
  260. cid : int
  261. Connection id of the observer to be removed.
  262. """
  263. self._observers.disconnect(cid)
  264. def reset(self):
  265. """Reset the slider to the initial value."""
  266. if np.any(self.val != self.valinit):
  267. self.set_val(self.valinit)
  268. class Slider(SliderBase):
  269. """
  270. A slider representing a floating point range.
  271. Create a slider from *valmin* to *valmax* in Axes *ax*. For the slider to
  272. remain responsive you must maintain a reference to it. Call
  273. :meth:`on_changed` to connect to the slider event.
  274. Attributes
  275. ----------
  276. val : float
  277. Slider value.
  278. """
  279. def __init__(self, ax, label, valmin, valmax, *, valinit=0.5, valfmt=None,
  280. closedmin=True, closedmax=True, slidermin=None,
  281. slidermax=None, dragging=True, valstep=None,
  282. orientation='horizontal', initcolor='r',
  283. track_color='lightgrey', handle_style=None, **kwargs):
  284. """
  285. Parameters
  286. ----------
  287. ax : Axes
  288. The Axes to put the slider in.
  289. label : str
  290. Slider label.
  291. valmin : float
  292. The minimum value of the slider.
  293. valmax : float
  294. The maximum value of the slider.
  295. valinit : float, default: 0.5
  296. The slider initial position.
  297. valfmt : str, default: None
  298. %-format string used to format the slider value. If None, a
  299. `.ScalarFormatter` is used instead.
  300. closedmin : bool, default: True
  301. Whether the slider interval is closed on the bottom.
  302. closedmax : bool, default: True
  303. Whether the slider interval is closed on the top.
  304. slidermin : Slider, default: None
  305. Do not allow the current slider to have a value less than
  306. the value of the Slider *slidermin*.
  307. slidermax : Slider, default: None
  308. Do not allow the current slider to have a value greater than
  309. the value of the Slider *slidermax*.
  310. dragging : bool, default: True
  311. If True the slider can be dragged by the mouse.
  312. valstep : float or array-like, default: None
  313. If a float, the slider will snap to multiples of *valstep*.
  314. If an array the slider will snap to the values in the array.
  315. orientation : {'horizontal', 'vertical'}, default: 'horizontal'
  316. The orientation of the slider.
  317. initcolor : :mpltype:`color`, default: 'r'
  318. The color of the line at the *valinit* position. Set to ``'none'``
  319. for no line.
  320. track_color : :mpltype:`color`, default: 'lightgrey'
  321. The color of the background track. The track is accessible for
  322. further styling via the *track* attribute.
  323. handle_style : dict
  324. Properties of the slider handle. Default values are
  325. ========= ===== ======= ========================================
  326. Key Value Default Description
  327. ========= ===== ======= ========================================
  328. facecolor color 'white' The facecolor of the slider handle.
  329. edgecolor color '.75' The edgecolor of the slider handle.
  330. size int 10 The size of the slider handle in points.
  331. ========= ===== ======= ========================================
  332. Other values will be transformed as marker{foo} and passed to the
  333. `~.Line2D` constructor. e.g. ``handle_style = {'style'='x'}`` will
  334. result in ``markerstyle = 'x'``.
  335. Notes
  336. -----
  337. Additional kwargs are passed on to ``self.poly`` which is the
  338. `~matplotlib.patches.Rectangle` that draws the slider knob. See the
  339. `.Rectangle` documentation for valid property names (``facecolor``,
  340. ``edgecolor``, ``alpha``, etc.).
  341. """
  342. super().__init__(ax, orientation, closedmin, closedmax,
  343. valmin, valmax, valfmt, dragging, valstep)
  344. if slidermin is not None and not hasattr(slidermin, 'val'):
  345. raise ValueError(
  346. f"Argument slidermin ({type(slidermin)}) has no 'val'")
  347. if slidermax is not None and not hasattr(slidermax, 'val'):
  348. raise ValueError(
  349. f"Argument slidermax ({type(slidermax)}) has no 'val'")
  350. self.slidermin = slidermin
  351. self.slidermax = slidermax
  352. valinit = self._value_in_bounds(valinit)
  353. if valinit is None:
  354. valinit = valmin
  355. self.val = valinit
  356. self.valinit = valinit
  357. defaults = {'facecolor': 'white', 'edgecolor': '.75', 'size': 10}
  358. handle_style = {} if handle_style is None else handle_style
  359. marker_props = {
  360. f'marker{k}': v for k, v in {**defaults, **handle_style}.items()
  361. }
  362. if orientation == 'vertical':
  363. self.track = Rectangle(
  364. (.25, 0), .5, 1,
  365. transform=ax.transAxes,
  366. facecolor=track_color
  367. )
  368. ax.add_patch(self.track)
  369. self.poly = ax.axhspan(valmin, valinit, .25, .75, **kwargs)
  370. # Drawing a longer line and clipping it to the track avoids
  371. # pixelation-related asymmetries.
  372. self.hline = ax.axhline(valinit, 0, 1, color=initcolor, lw=1,
  373. clip_path=TransformedPatchPath(self.track))
  374. handleXY = [[0.5], [valinit]]
  375. else:
  376. self.track = Rectangle(
  377. (0, .25), 1, .5,
  378. transform=ax.transAxes,
  379. facecolor=track_color
  380. )
  381. ax.add_patch(self.track)
  382. self.poly = ax.axvspan(valmin, valinit, .25, .75, **kwargs)
  383. self.vline = ax.axvline(valinit, 0, 1, color=initcolor, lw=1,
  384. clip_path=TransformedPatchPath(self.track))
  385. handleXY = [[valinit], [0.5]]
  386. self._handle, = ax.plot(
  387. *handleXY,
  388. "o",
  389. **marker_props,
  390. clip_on=False
  391. )
  392. if orientation == 'vertical':
  393. self.label = ax.text(0.5, 1.02, label, transform=ax.transAxes,
  394. verticalalignment='bottom',
  395. horizontalalignment='center')
  396. self.valtext = ax.text(0.5, -0.02, self._format(valinit),
  397. transform=ax.transAxes,
  398. verticalalignment='top',
  399. horizontalalignment='center')
  400. else:
  401. self.label = ax.text(-0.02, 0.5, label, transform=ax.transAxes,
  402. verticalalignment='center',
  403. horizontalalignment='right')
  404. self.valtext = ax.text(1.02, 0.5, self._format(valinit),
  405. transform=ax.transAxes,
  406. verticalalignment='center',
  407. horizontalalignment='left')
  408. self.set_val(valinit)
  409. def _value_in_bounds(self, val):
  410. """Makes sure *val* is with given bounds."""
  411. val = self._stepped_value(val)
  412. if val <= self.valmin:
  413. if not self.closedmin:
  414. return
  415. val = self.valmin
  416. elif val >= self.valmax:
  417. if not self.closedmax:
  418. return
  419. val = self.valmax
  420. if self.slidermin is not None and val <= self.slidermin.val:
  421. if not self.closedmin:
  422. return
  423. val = self.slidermin.val
  424. if self.slidermax is not None and val >= self.slidermax.val:
  425. if not self.closedmax:
  426. return
  427. val = self.slidermax.val
  428. return val
  429. def _update(self, event):
  430. """Update the slider position."""
  431. if self.ignore(event) or event.button != 1:
  432. return
  433. if event.name == 'button_press_event' and self.ax.contains(event)[0]:
  434. self.drag_active = True
  435. event.canvas.grab_mouse(self.ax)
  436. if not self.drag_active:
  437. return
  438. if (event.name == 'button_release_event'
  439. or event.name == 'button_press_event' and not self.ax.contains(event)[0]):
  440. self.drag_active = False
  441. event.canvas.release_mouse(self.ax)
  442. return
  443. xdata, ydata = self._get_data_coords(event)
  444. val = self._value_in_bounds(
  445. xdata if self.orientation == 'horizontal' else ydata)
  446. if val not in [None, self.val]:
  447. self.set_val(val)
  448. def _format(self, val):
  449. """Pretty-print *val*."""
  450. if self.valfmt is not None:
  451. return self.valfmt % val
  452. else:
  453. _, s, _ = self._fmt.format_ticks([self.valmin, val, self.valmax])
  454. # fmt.get_offset is actually the multiplicative factor, if any.
  455. return s + self._fmt.get_offset()
  456. def set_val(self, val):
  457. """
  458. Set slider value to *val*.
  459. Parameters
  460. ----------
  461. val : float
  462. """
  463. if self.orientation == 'vertical':
  464. self.poly.set_height(val - self.poly.get_y())
  465. self._handle.set_ydata([val])
  466. else:
  467. self.poly.set_width(val - self.poly.get_x())
  468. self._handle.set_xdata([val])
  469. self.valtext.set_text(self._format(val))
  470. if self.drawon:
  471. self.ax.get_figure(root=True).canvas.draw_idle()
  472. self.val = val
  473. if self.eventson:
  474. self._observers.process('changed', val)
  475. def on_changed(self, func):
  476. """
  477. Connect *func* as callback function to changes of the slider value.
  478. Parameters
  479. ----------
  480. func : callable
  481. Function to call when slider is changed.
  482. The function must accept a single float as its arguments.
  483. Returns
  484. -------
  485. int
  486. Connection id (which can be used to disconnect *func*).
  487. """
  488. return self._observers.connect('changed', lambda val: func(val))
  489. class RangeSlider(SliderBase):
  490. """
  491. A slider representing a range of floating point values. Defines the min and
  492. max of the range via the *val* attribute as a tuple of (min, max).
  493. Create a slider that defines a range contained within [*valmin*, *valmax*]
  494. in Axes *ax*. For the slider to remain responsive you must maintain a
  495. reference to it. Call :meth:`on_changed` to connect to the slider event.
  496. Attributes
  497. ----------
  498. val : tuple of float
  499. Slider value.
  500. """
  501. def __init__(
  502. self,
  503. ax,
  504. label,
  505. valmin,
  506. valmax,
  507. *,
  508. valinit=None,
  509. valfmt=None,
  510. closedmin=True,
  511. closedmax=True,
  512. dragging=True,
  513. valstep=None,
  514. orientation="horizontal",
  515. track_color='lightgrey',
  516. handle_style=None,
  517. **kwargs,
  518. ):
  519. """
  520. Parameters
  521. ----------
  522. ax : Axes
  523. The Axes to put the slider in.
  524. label : str
  525. Slider label.
  526. valmin : float
  527. The minimum value of the slider.
  528. valmax : float
  529. The maximum value of the slider.
  530. valinit : tuple of float or None, default: None
  531. The initial positions of the slider. If None the initial positions
  532. will be at the 25th and 75th percentiles of the range.
  533. valfmt : str, default: None
  534. %-format string used to format the slider values. If None, a
  535. `.ScalarFormatter` is used instead.
  536. closedmin : bool, default: True
  537. Whether the slider interval is closed on the bottom.
  538. closedmax : bool, default: True
  539. Whether the slider interval is closed on the top.
  540. dragging : bool, default: True
  541. If True the slider can be dragged by the mouse.
  542. valstep : float, default: None
  543. If given, the slider will snap to multiples of *valstep*.
  544. orientation : {'horizontal', 'vertical'}, default: 'horizontal'
  545. The orientation of the slider.
  546. track_color : :mpltype:`color`, default: 'lightgrey'
  547. The color of the background track. The track is accessible for
  548. further styling via the *track* attribute.
  549. handle_style : dict
  550. Properties of the slider handles. Default values are
  551. ========= ===== ======= =========================================
  552. Key Value Default Description
  553. ========= ===== ======= =========================================
  554. facecolor color 'white' The facecolor of the slider handles.
  555. edgecolor color '.75' The edgecolor of the slider handles.
  556. size int 10 The size of the slider handles in points.
  557. ========= ===== ======= =========================================
  558. Other values will be transformed as marker{foo} and passed to the
  559. `~.Line2D` constructor. e.g. ``handle_style = {'style'='x'}`` will
  560. result in ``markerstyle = 'x'``.
  561. Notes
  562. -----
  563. Additional kwargs are passed on to ``self.poly`` which is the
  564. `~matplotlib.patches.Polygon` that draws the slider knob. See the
  565. `.Polygon` documentation for valid property names (``facecolor``,
  566. ``edgecolor``, ``alpha``, etc.).
  567. """
  568. super().__init__(ax, orientation, closedmin, closedmax,
  569. valmin, valmax, valfmt, dragging, valstep)
  570. # Set a value to allow _value_in_bounds() to work.
  571. self.val = (valmin, valmax)
  572. if valinit is None:
  573. # Place at the 25th and 75th percentiles
  574. extent = valmax - valmin
  575. valinit = np.array([valmin + extent * 0.25,
  576. valmin + extent * 0.75])
  577. else:
  578. valinit = self._value_in_bounds(valinit)
  579. self.val = valinit
  580. self.valinit = valinit
  581. defaults = {'facecolor': 'white', 'edgecolor': '.75', 'size': 10}
  582. handle_style = {} if handle_style is None else handle_style
  583. marker_props = {
  584. f'marker{k}': v for k, v in {**defaults, **handle_style}.items()
  585. }
  586. if orientation == "vertical":
  587. self.track = Rectangle(
  588. (.25, 0), .5, 2,
  589. transform=ax.transAxes,
  590. facecolor=track_color
  591. )
  592. ax.add_patch(self.track)
  593. poly_transform = self.ax.get_yaxis_transform(which="grid")
  594. handleXY_1 = [.5, valinit[0]]
  595. handleXY_2 = [.5, valinit[1]]
  596. else:
  597. self.track = Rectangle(
  598. (0, .25), 1, .5,
  599. transform=ax.transAxes,
  600. facecolor=track_color
  601. )
  602. ax.add_patch(self.track)
  603. poly_transform = self.ax.get_xaxis_transform(which="grid")
  604. handleXY_1 = [valinit[0], .5]
  605. handleXY_2 = [valinit[1], .5]
  606. self.poly = Polygon(np.zeros([5, 2]), **kwargs)
  607. self._update_selection_poly(*valinit)
  608. self.poly.set_transform(poly_transform)
  609. self.poly.get_path()._interpolation_steps = 100
  610. self.ax.add_patch(self.poly)
  611. self.ax._request_autoscale_view()
  612. self._handles = [
  613. ax.plot(
  614. *handleXY_1,
  615. "o",
  616. **marker_props,
  617. clip_on=False
  618. )[0],
  619. ax.plot(
  620. *handleXY_2,
  621. "o",
  622. **marker_props,
  623. clip_on=False
  624. )[0]
  625. ]
  626. if orientation == "vertical":
  627. self.label = ax.text(
  628. 0.5,
  629. 1.02,
  630. label,
  631. transform=ax.transAxes,
  632. verticalalignment="bottom",
  633. horizontalalignment="center",
  634. )
  635. self.valtext = ax.text(
  636. 0.5,
  637. -0.02,
  638. self._format(valinit),
  639. transform=ax.transAxes,
  640. verticalalignment="top",
  641. horizontalalignment="center",
  642. )
  643. else:
  644. self.label = ax.text(
  645. -0.02,
  646. 0.5,
  647. label,
  648. transform=ax.transAxes,
  649. verticalalignment="center",
  650. horizontalalignment="right",
  651. )
  652. self.valtext = ax.text(
  653. 1.02,
  654. 0.5,
  655. self._format(valinit),
  656. transform=ax.transAxes,
  657. verticalalignment="center",
  658. horizontalalignment="left",
  659. )
  660. self._active_handle = None
  661. self.set_val(valinit)
  662. def _update_selection_poly(self, vmin, vmax):
  663. """
  664. Update the vertices of the *self.poly* slider in-place
  665. to cover the data range *vmin*, *vmax*.
  666. """
  667. # The vertices are positioned
  668. # 1 ------ 2
  669. # | |
  670. # 0, 4 ---- 3
  671. verts = self.poly.xy
  672. if self.orientation == "vertical":
  673. verts[0] = verts[4] = .25, vmin
  674. verts[1] = .25, vmax
  675. verts[2] = .75, vmax
  676. verts[3] = .75, vmin
  677. else:
  678. verts[0] = verts[4] = vmin, .25
  679. verts[1] = vmin, .75
  680. verts[2] = vmax, .75
  681. verts[3] = vmax, .25
  682. def _min_in_bounds(self, min):
  683. """Ensure the new min value is between valmin and self.val[1]."""
  684. if min <= self.valmin:
  685. if not self.closedmin:
  686. return self.val[0]
  687. min = self.valmin
  688. if min > self.val[1]:
  689. min = self.val[1]
  690. return self._stepped_value(min)
  691. def _max_in_bounds(self, max):
  692. """Ensure the new max value is between valmax and self.val[0]."""
  693. if max >= self.valmax:
  694. if not self.closedmax:
  695. return self.val[1]
  696. max = self.valmax
  697. if max <= self.val[0]:
  698. max = self.val[0]
  699. return self._stepped_value(max)
  700. def _value_in_bounds(self, vals):
  701. """Clip min, max values to the bounds."""
  702. return (self._min_in_bounds(vals[0]), self._max_in_bounds(vals[1]))
  703. def _update_val_from_pos(self, pos):
  704. """Update the slider value based on a given position."""
  705. idx = np.argmin(np.abs(self.val - pos))
  706. if idx == 0:
  707. val = self._min_in_bounds(pos)
  708. self.set_min(val)
  709. else:
  710. val = self._max_in_bounds(pos)
  711. self.set_max(val)
  712. if self._active_handle:
  713. if self.orientation == "vertical":
  714. self._active_handle.set_ydata([val])
  715. else:
  716. self._active_handle.set_xdata([val])
  717. def _update(self, event):
  718. """Update the slider position."""
  719. if self.ignore(event) or event.button != 1:
  720. return
  721. if event.name == "button_press_event" and self.ax.contains(event)[0]:
  722. self.drag_active = True
  723. event.canvas.grab_mouse(self.ax)
  724. if not self.drag_active:
  725. return
  726. if (event.name == "button_release_event"
  727. or event.name == "button_press_event" and not self.ax.contains(event)[0]):
  728. self.drag_active = False
  729. event.canvas.release_mouse(self.ax)
  730. self._active_handle = None
  731. return
  732. # determine which handle was grabbed
  733. xdata, ydata = self._get_data_coords(event)
  734. handle_index = np.argmin(np.abs(
  735. [h.get_xdata()[0] - xdata for h in self._handles]
  736. if self.orientation == "horizontal" else
  737. [h.get_ydata()[0] - ydata for h in self._handles]))
  738. handle = self._handles[handle_index]
  739. # these checks ensure smooth behavior if the handles swap which one
  740. # has a higher value. i.e. if one is dragged over and past the other.
  741. if handle is not self._active_handle:
  742. self._active_handle = handle
  743. self._update_val_from_pos(xdata if self.orientation == "horizontal" else ydata)
  744. def _format(self, val):
  745. """Pretty-print *val*."""
  746. if self.valfmt is not None:
  747. return f"({self.valfmt % val[0]}, {self.valfmt % val[1]})"
  748. else:
  749. _, s1, s2, _ = self._fmt.format_ticks(
  750. [self.valmin, *val, self.valmax]
  751. )
  752. # fmt.get_offset is actually the multiplicative factor, if any.
  753. s1 += self._fmt.get_offset()
  754. s2 += self._fmt.get_offset()
  755. # Use f string to avoid issues with backslashes when cast to a str
  756. return f"({s1}, {s2})"
  757. def set_min(self, min):
  758. """
  759. Set the lower value of the slider to *min*.
  760. Parameters
  761. ----------
  762. min : float
  763. """
  764. self.set_val((min, self.val[1]))
  765. def set_max(self, max):
  766. """
  767. Set the lower value of the slider to *max*.
  768. Parameters
  769. ----------
  770. max : float
  771. """
  772. self.set_val((self.val[0], max))
  773. def set_val(self, val):
  774. """
  775. Set slider value to *val*.
  776. Parameters
  777. ----------
  778. val : tuple or array-like of float
  779. """
  780. val = np.sort(val)
  781. _api.check_shape((2,), val=val)
  782. # Reset value to allow _value_in_bounds() to work.
  783. self.val = (self.valmin, self.valmax)
  784. vmin, vmax = self._value_in_bounds(val)
  785. self._update_selection_poly(vmin, vmax)
  786. if self.orientation == "vertical":
  787. self._handles[0].set_ydata([vmin])
  788. self._handles[1].set_ydata([vmax])
  789. else:
  790. self._handles[0].set_xdata([vmin])
  791. self._handles[1].set_xdata([vmax])
  792. self.valtext.set_text(self._format((vmin, vmax)))
  793. if self.drawon:
  794. self.ax.get_figure(root=True).canvas.draw_idle()
  795. self.val = (vmin, vmax)
  796. if self.eventson:
  797. self._observers.process("changed", (vmin, vmax))
  798. def on_changed(self, func):
  799. """
  800. Connect *func* as callback function to changes of the slider value.
  801. Parameters
  802. ----------
  803. func : callable
  804. Function to call when slider is changed. The function
  805. must accept a 2-tuple of floats as its argument.
  806. Returns
  807. -------
  808. int
  809. Connection id (which can be used to disconnect *func*).
  810. """
  811. return self._observers.connect('changed', lambda val: func(val))
  812. def _expand_text_props(props):
  813. props = cbook.normalize_kwargs(props, mtext.Text)
  814. return cycler(**props)() if props else itertools.repeat({})
  815. class CheckButtons(AxesWidget):
  816. r"""
  817. A GUI neutral set of check buttons.
  818. For the check buttons to remain responsive you must keep a
  819. reference to this object.
  820. Connect to the CheckButtons with the `.on_clicked` method.
  821. Attributes
  822. ----------
  823. ax : `~matplotlib.axes.Axes`
  824. The parent Axes for the widget.
  825. labels : list of `~matplotlib.text.Text`
  826. The text label objects of the check buttons.
  827. """
  828. def __init__(self, ax, labels, actives=None, *, useblit=True,
  829. label_props=None, frame_props=None, check_props=None):
  830. """
  831. Add check buttons to `~.axes.Axes` instance *ax*.
  832. Parameters
  833. ----------
  834. ax : `~matplotlib.axes.Axes`
  835. The parent Axes for the widget.
  836. labels : list of str
  837. The labels of the check buttons.
  838. actives : list of bool, optional
  839. The initial check states of the buttons. The list must have the
  840. same length as *labels*. If not given, all buttons are unchecked.
  841. useblit : bool, default: True
  842. Use blitting for faster drawing if supported by the backend.
  843. See the tutorial :ref:`blitting` for details.
  844. .. versionadded:: 3.7
  845. label_props : dict of lists, optional
  846. Dictionary of `.Text` properties to be used for the labels. Each
  847. dictionary value should be a list of at least a single element. If
  848. the list is of length M, its values are cycled such that the Nth
  849. label gets the (N mod M) property.
  850. .. versionadded:: 3.7
  851. frame_props : dict, optional
  852. Dictionary of scatter `.Collection` properties to be used for the
  853. check button frame. Defaults (label font size / 2)**2 size, black
  854. edgecolor, no facecolor, and 1.0 linewidth.
  855. .. versionadded:: 3.7
  856. check_props : dict, optional
  857. Dictionary of scatter `.Collection` properties to be used for the
  858. check button check. Defaults to (label font size / 2)**2 size,
  859. black color, and 1.0 linewidth.
  860. .. versionadded:: 3.7
  861. """
  862. super().__init__(ax)
  863. _api.check_isinstance((dict, None), label_props=label_props,
  864. frame_props=frame_props, check_props=check_props)
  865. ax.set_xticks([])
  866. ax.set_yticks([])
  867. ax.set_navigate(False)
  868. if actives is None:
  869. actives = [False] * len(labels)
  870. self._useblit = useblit and self.canvas.supports_blit
  871. self._background = None
  872. ys = np.linspace(1, 0, len(labels)+2)[1:-1]
  873. label_props = _expand_text_props(label_props)
  874. self.labels = [
  875. ax.text(0.25, y, label, transform=ax.transAxes,
  876. horizontalalignment="left", verticalalignment="center",
  877. **props)
  878. for y, label, props in zip(ys, labels, label_props)]
  879. text_size = np.array([text.get_fontsize() for text in self.labels]) / 2
  880. frame_props = {
  881. 's': text_size**2,
  882. 'linewidth': 1,
  883. **cbook.normalize_kwargs(frame_props, collections.PathCollection),
  884. 'marker': 's',
  885. 'transform': ax.transAxes,
  886. }
  887. frame_props.setdefault('facecolor', frame_props.get('color', 'none'))
  888. frame_props.setdefault('edgecolor', frame_props.pop('color', 'black'))
  889. self._frames = ax.scatter([0.15] * len(ys), ys, **frame_props)
  890. check_props = {
  891. 'linewidth': 1,
  892. 's': text_size**2,
  893. **cbook.normalize_kwargs(check_props, collections.PathCollection),
  894. 'marker': 'x',
  895. 'transform': ax.transAxes,
  896. 'animated': self._useblit,
  897. }
  898. check_props.setdefault('facecolor', check_props.pop('color', 'black'))
  899. self._checks = ax.scatter([0.15] * len(ys), ys, **check_props)
  900. # The user may have passed custom colours in check_props, so we need to
  901. # create the checks (above), and modify the visibility after getting
  902. # whatever the user set.
  903. self._init_status(actives)
  904. self.connect_event('button_press_event', self._clicked)
  905. if self._useblit:
  906. self.connect_event('draw_event', self._clear)
  907. self._observers = cbook.CallbackRegistry(signals=["clicked"])
  908. def _clear(self, event):
  909. """Internal event handler to clear the buttons."""
  910. if self.ignore(event) or self.canvas.is_saving():
  911. return
  912. self._background = self.canvas.copy_from_bbox(self.ax.bbox)
  913. self.ax.draw_artist(self._checks)
  914. def _clicked(self, event):
  915. if self.ignore(event) or event.button != 1 or not self.ax.contains(event)[0]:
  916. return
  917. idxs = [ # Indices of frames and of texts that contain the event.
  918. *self._frames.contains(event)[1]["ind"],
  919. *[i for i, text in enumerate(self.labels) if text.contains(event)[0]]]
  920. if idxs:
  921. coords = self._frames.get_offset_transform().transform(
  922. self._frames.get_offsets())
  923. self.set_active( # Closest index, only looking in idxs.
  924. idxs[(((event.x, event.y) - coords[idxs]) ** 2).sum(-1).argmin()])
  925. def set_label_props(self, props):
  926. """
  927. Set properties of the `.Text` labels.
  928. .. versionadded:: 3.7
  929. Parameters
  930. ----------
  931. props : dict
  932. Dictionary of `.Text` properties to be used for the labels. Same
  933. format as label_props argument of :class:`CheckButtons`.
  934. """
  935. _api.check_isinstance(dict, props=props)
  936. props = _expand_text_props(props)
  937. for text, prop in zip(self.labels, props):
  938. text.update(prop)
  939. def set_frame_props(self, props):
  940. """
  941. Set properties of the check button frames.
  942. .. versionadded:: 3.7
  943. Parameters
  944. ----------
  945. props : dict
  946. Dictionary of `.Collection` properties to be used for the check
  947. button frames.
  948. """
  949. _api.check_isinstance(dict, props=props)
  950. if 's' in props: # Keep API consistent with constructor.
  951. props['sizes'] = np.broadcast_to(props.pop('s'), len(self.labels))
  952. self._frames.update(props)
  953. def set_check_props(self, props):
  954. """
  955. Set properties of the check button checks.
  956. .. versionadded:: 3.7
  957. Parameters
  958. ----------
  959. props : dict
  960. Dictionary of `.Collection` properties to be used for the check
  961. button check.
  962. """
  963. _api.check_isinstance(dict, props=props)
  964. if 's' in props: # Keep API consistent with constructor.
  965. props['sizes'] = np.broadcast_to(props.pop('s'), len(self.labels))
  966. actives = self.get_status()
  967. self._checks.update(props)
  968. # If new colours are supplied, then we must re-apply the status.
  969. self._init_status(actives)
  970. def set_active(self, index, state=None):
  971. """
  972. Modify the state of a check button by index.
  973. Callbacks will be triggered if :attr:`eventson` is True.
  974. Parameters
  975. ----------
  976. index : int
  977. Index of the check button to toggle.
  978. state : bool, optional
  979. If a boolean value, set the state explicitly. If no value is
  980. provided, the state is toggled.
  981. Raises
  982. ------
  983. ValueError
  984. If *index* is invalid.
  985. TypeError
  986. If *state* is not boolean.
  987. """
  988. if index not in range(len(self.labels)):
  989. raise ValueError(f'Invalid CheckButton index: {index}')
  990. _api.check_isinstance((bool, None), state=state)
  991. invisible = colors.to_rgba('none')
  992. facecolors = self._checks.get_facecolor()
  993. if state is None:
  994. state = colors.same_color(facecolors[index], invisible)
  995. facecolors[index] = self._active_check_colors[index] if state else invisible
  996. self._checks.set_facecolor(facecolors)
  997. if self.drawon:
  998. if self._useblit:
  999. if self._background is not None:
  1000. self.canvas.restore_region(self._background)
  1001. self.ax.draw_artist(self._checks)
  1002. self.canvas.blit(self.ax.bbox)
  1003. else:
  1004. self.canvas.draw()
  1005. if self.eventson:
  1006. self._observers.process('clicked', self.labels[index].get_text())
  1007. def _init_status(self, actives):
  1008. """
  1009. Initialize properties to match active status.
  1010. The user may have passed custom colours in *check_props* to the
  1011. constructor, or to `.set_check_props`, so we need to modify the
  1012. visibility after getting whatever the user set.
  1013. """
  1014. self._active_check_colors = self._checks.get_facecolor()
  1015. if len(self._active_check_colors) == 1:
  1016. self._active_check_colors = np.repeat(self._active_check_colors,
  1017. len(actives), axis=0)
  1018. self._checks.set_facecolor(
  1019. [ec if active else "none"
  1020. for ec, active in zip(self._active_check_colors, actives)])
  1021. def clear(self):
  1022. """Uncheck all checkboxes."""
  1023. self._checks.set_facecolor(['none'] * len(self._active_check_colors))
  1024. if hasattr(self, '_lines'):
  1025. for l1, l2 in self._lines:
  1026. l1.set_visible(False)
  1027. l2.set_visible(False)
  1028. if self.drawon:
  1029. self.canvas.draw()
  1030. if self.eventson:
  1031. # Call with no label, as all checkboxes are being cleared.
  1032. self._observers.process('clicked', None)
  1033. def get_status(self):
  1034. """
  1035. Return a list of the status (True/False) of all of the check buttons.
  1036. """
  1037. return [not colors.same_color(color, colors.to_rgba("none"))
  1038. for color in self._checks.get_facecolors()]
  1039. def get_checked_labels(self):
  1040. """Return a list of labels currently checked by user."""
  1041. return [l.get_text() for l, box_checked in
  1042. zip(self.labels, self.get_status())
  1043. if box_checked]
  1044. def on_clicked(self, func):
  1045. """
  1046. Connect the callback function *func* to button click events.
  1047. Parameters
  1048. ----------
  1049. func : callable
  1050. When the button is clicked, call *func* with button label.
  1051. When all buttons are cleared, call *func* with None.
  1052. The callback func must have the signature::
  1053. def func(label: str | None) -> Any
  1054. Return values may exist, but are ignored.
  1055. Returns
  1056. -------
  1057. A connection id, which can be used to disconnect the callback.
  1058. """
  1059. return self._observers.connect('clicked', lambda text: func(text))
  1060. def disconnect(self, cid):
  1061. """Remove the observer with connection id *cid*."""
  1062. self._observers.disconnect(cid)
  1063. class TextBox(AxesWidget):
  1064. """
  1065. A GUI neutral text input box.
  1066. For the text box to remain responsive you must keep a reference to it.
  1067. Call `.on_text_change` to be updated whenever the text changes.
  1068. Call `.on_submit` to be updated whenever the user hits enter or
  1069. leaves the text entry field.
  1070. Attributes
  1071. ----------
  1072. ax : `~matplotlib.axes.Axes`
  1073. The parent Axes for the widget.
  1074. label : `~matplotlib.text.Text`
  1075. color : :mpltype:`color`
  1076. The color of the text box when not hovering.
  1077. hovercolor : :mpltype:`color`
  1078. The color of the text box when hovering.
  1079. """
  1080. def __init__(self, ax, label, initial='', *,
  1081. color='.95', hovercolor='1', label_pad=.01,
  1082. textalignment="left"):
  1083. """
  1084. Parameters
  1085. ----------
  1086. ax : `~matplotlib.axes.Axes`
  1087. The `~.axes.Axes` instance the button will be placed into.
  1088. label : str
  1089. Label for this text box.
  1090. initial : str
  1091. Initial value in the text box.
  1092. color : :mpltype:`color`
  1093. The color of the box.
  1094. hovercolor : :mpltype:`color`
  1095. The color of the box when the mouse is over it.
  1096. label_pad : float
  1097. The distance between the label and the right side of the textbox.
  1098. textalignment : {'left', 'center', 'right'}
  1099. The horizontal location of the text.
  1100. """
  1101. super().__init__(ax)
  1102. self._text_position = _api.check_getitem(
  1103. {"left": 0.05, "center": 0.5, "right": 0.95},
  1104. textalignment=textalignment)
  1105. self.label = ax.text(
  1106. -label_pad, 0.5, label, transform=ax.transAxes,
  1107. verticalalignment='center', horizontalalignment='right')
  1108. # TextBox's text object should not parse mathtext at all.
  1109. self.text_disp = self.ax.text(
  1110. self._text_position, 0.5, initial, transform=self.ax.transAxes,
  1111. verticalalignment='center', horizontalalignment=textalignment,
  1112. parse_math=False)
  1113. self._observers = cbook.CallbackRegistry(signals=["change", "submit"])
  1114. ax.set(
  1115. xlim=(0, 1), ylim=(0, 1), # s.t. cursor appears from first click.
  1116. navigate=False, facecolor=color,
  1117. xticks=[], yticks=[])
  1118. self.cursor_index = 0
  1119. self.cursor = ax.vlines(0, 0, 0, visible=False, color="k", lw=1,
  1120. transform=mpl.transforms.IdentityTransform())
  1121. self.connect_event('button_press_event', self._click)
  1122. self.connect_event('button_release_event', self._release)
  1123. self.connect_event('motion_notify_event', self._motion)
  1124. self.connect_event('key_press_event', self._keypress)
  1125. self.connect_event('resize_event', self._resize)
  1126. self.color = color
  1127. self.hovercolor = hovercolor
  1128. self.capturekeystrokes = False
  1129. @property
  1130. def text(self):
  1131. return self.text_disp.get_text()
  1132. def _rendercursor(self):
  1133. # this is a hack to figure out where the cursor should go.
  1134. # we draw the text up to where the cursor should go, measure
  1135. # and save its dimensions, draw the real text, then put the cursor
  1136. # at the saved dimensions
  1137. # This causes a single extra draw if the figure has never been rendered
  1138. # yet, which should be fine as we're going to repeatedly re-render the
  1139. # figure later anyways.
  1140. fig = self.ax.get_figure(root=True)
  1141. if fig._get_renderer() is None:
  1142. fig.canvas.draw()
  1143. text = self.text_disp.get_text() # Save value before overwriting it.
  1144. widthtext = text[:self.cursor_index]
  1145. bb_text = self.text_disp.get_window_extent()
  1146. self.text_disp.set_text(widthtext or ",")
  1147. bb_widthtext = self.text_disp.get_window_extent()
  1148. if bb_text.y0 == bb_text.y1: # Restoring the height if no text.
  1149. bb_text.y0 -= bb_widthtext.height / 2
  1150. bb_text.y1 += bb_widthtext.height / 2
  1151. elif not widthtext: # Keep width to 0.
  1152. bb_text.x1 = bb_text.x0
  1153. else: # Move the cursor using width of bb_widthtext.
  1154. bb_text.x1 = bb_text.x0 + bb_widthtext.width
  1155. self.cursor.set(
  1156. segments=[[(bb_text.x1, bb_text.y0), (bb_text.x1, bb_text.y1)]],
  1157. visible=True)
  1158. self.text_disp.set_text(text)
  1159. fig.canvas.draw()
  1160. def _release(self, event):
  1161. if self.ignore(event):
  1162. return
  1163. if event.canvas.mouse_grabber != self.ax:
  1164. return
  1165. event.canvas.release_mouse(self.ax)
  1166. def _keypress(self, event):
  1167. if self.ignore(event):
  1168. return
  1169. if self.capturekeystrokes:
  1170. key = event.key
  1171. text = self.text
  1172. if len(key) == 1:
  1173. text = (text[:self.cursor_index] + key +
  1174. text[self.cursor_index:])
  1175. self.cursor_index += 1
  1176. elif key == "right":
  1177. if self.cursor_index != len(text):
  1178. self.cursor_index += 1
  1179. elif key == "left":
  1180. if self.cursor_index != 0:
  1181. self.cursor_index -= 1
  1182. elif key == "home":
  1183. self.cursor_index = 0
  1184. elif key == "end":
  1185. self.cursor_index = len(text)
  1186. elif key == "backspace":
  1187. if self.cursor_index != 0:
  1188. text = (text[:self.cursor_index - 1] +
  1189. text[self.cursor_index:])
  1190. self.cursor_index -= 1
  1191. elif key == "delete":
  1192. if self.cursor_index != len(self.text):
  1193. text = (text[:self.cursor_index] +
  1194. text[self.cursor_index + 1:])
  1195. self.text_disp.set_text(text)
  1196. self._rendercursor()
  1197. if self.eventson:
  1198. self._observers.process('change', self.text)
  1199. if key in ["enter", "return"]:
  1200. self._observers.process('submit', self.text)
  1201. def set_val(self, val):
  1202. newval = str(val)
  1203. if self.text == newval:
  1204. return
  1205. self.text_disp.set_text(newval)
  1206. self._rendercursor()
  1207. if self.eventson:
  1208. self._observers.process('change', self.text)
  1209. self._observers.process('submit', self.text)
  1210. def begin_typing(self):
  1211. self.capturekeystrokes = True
  1212. # Disable keypress shortcuts, which may otherwise cause the figure to
  1213. # be saved, closed, etc., until the user stops typing. The way to
  1214. # achieve this depends on whether toolmanager is in use.
  1215. stack = ExitStack() # Register cleanup actions when user stops typing.
  1216. self._on_stop_typing = stack.close
  1217. toolmanager = getattr(
  1218. self.ax.get_figure(root=True).canvas.manager, "toolmanager", None)
  1219. if toolmanager is not None:
  1220. # If using toolmanager, lock keypresses, and plan to release the
  1221. # lock when typing stops.
  1222. toolmanager.keypresslock(self)
  1223. stack.callback(toolmanager.keypresslock.release, self)
  1224. else:
  1225. # If not using toolmanager, disable all keypress-related rcParams.
  1226. # Avoid spurious warnings if keymaps are getting deprecated.
  1227. with _api.suppress_matplotlib_deprecation_warning():
  1228. stack.enter_context(mpl.rc_context(
  1229. {k: [] for k in mpl.rcParams if k.startswith("keymap.")}))
  1230. def stop_typing(self):
  1231. if self.capturekeystrokes:
  1232. self._on_stop_typing()
  1233. self._on_stop_typing = None
  1234. notifysubmit = True
  1235. else:
  1236. notifysubmit = False
  1237. self.capturekeystrokes = False
  1238. self.cursor.set_visible(False)
  1239. self.ax.get_figure(root=True).canvas.draw()
  1240. if notifysubmit and self.eventson:
  1241. # Because process() might throw an error in the user's code, only
  1242. # call it once we've already done our cleanup.
  1243. self._observers.process('submit', self.text)
  1244. def _click(self, event):
  1245. if self.ignore(event):
  1246. return
  1247. if not self.ax.contains(event)[0]:
  1248. self.stop_typing()
  1249. return
  1250. if not self.eventson:
  1251. return
  1252. if event.canvas.mouse_grabber != self.ax:
  1253. event.canvas.grab_mouse(self.ax)
  1254. if not self.capturekeystrokes:
  1255. self.begin_typing()
  1256. self.cursor_index = self.text_disp._char_index_at(event.x)
  1257. self._rendercursor()
  1258. def _resize(self, event):
  1259. self.stop_typing()
  1260. def _motion(self, event):
  1261. if self.ignore(event):
  1262. return
  1263. c = self.hovercolor if self.ax.contains(event)[0] else self.color
  1264. if not colors.same_color(c, self.ax.get_facecolor()):
  1265. self.ax.set_facecolor(c)
  1266. if self.drawon:
  1267. self.ax.get_figure(root=True).canvas.draw()
  1268. def on_text_change(self, func):
  1269. """
  1270. When the text changes, call this *func* with event.
  1271. A connection id is returned which can be used to disconnect.
  1272. """
  1273. return self._observers.connect('change', lambda text: func(text))
  1274. def on_submit(self, func):
  1275. """
  1276. When the user hits enter or leaves the submission box, call this
  1277. *func* with event.
  1278. A connection id is returned which can be used to disconnect.
  1279. """
  1280. return self._observers.connect('submit', lambda text: func(text))
  1281. def disconnect(self, cid):
  1282. """Remove the observer with connection id *cid*."""
  1283. self._observers.disconnect(cid)
  1284. class RadioButtons(AxesWidget):
  1285. """
  1286. A GUI neutral radio button.
  1287. For the buttons to remain responsive you must keep a reference to this
  1288. object.
  1289. Connect to the RadioButtons with the `.on_clicked` method.
  1290. Attributes
  1291. ----------
  1292. ax : `~matplotlib.axes.Axes`
  1293. The parent Axes for the widget.
  1294. activecolor : :mpltype:`color`
  1295. The color of the selected button.
  1296. labels : list of `.Text`
  1297. The button labels.
  1298. value_selected : str
  1299. The label text of the currently selected button.
  1300. index_selected : int
  1301. The index of the selected button.
  1302. """
  1303. def __init__(self, ax, labels, active=0, activecolor=None, *,
  1304. useblit=True, label_props=None, radio_props=None):
  1305. """
  1306. Add radio buttons to an `~.axes.Axes`.
  1307. Parameters
  1308. ----------
  1309. ax : `~matplotlib.axes.Axes`
  1310. The Axes to add the buttons to.
  1311. labels : list of str
  1312. The button labels.
  1313. active : int
  1314. The index of the initially selected button.
  1315. activecolor : :mpltype:`color`
  1316. The color of the selected button. The default is ``'blue'`` if not
  1317. specified here or in *radio_props*.
  1318. useblit : bool, default: True
  1319. Use blitting for faster drawing if supported by the backend.
  1320. See the tutorial :ref:`blitting` for details.
  1321. .. versionadded:: 3.7
  1322. label_props : dict of lists, optional
  1323. Dictionary of `.Text` properties to be used for the labels. Each
  1324. dictionary value should be a list of at least a single element. If
  1325. the list is of length M, its values are cycled such that the Nth
  1326. label gets the (N mod M) property.
  1327. .. versionadded:: 3.7
  1328. radio_props : dict, optional
  1329. Dictionary of scatter `.Collection` properties to be used for the
  1330. radio buttons. Defaults to (label font size / 2)**2 size, black
  1331. edgecolor, and *activecolor* facecolor (when active).
  1332. .. note::
  1333. If a facecolor is supplied in *radio_props*, it will override
  1334. *activecolor*. This may be used to provide an active color per
  1335. button.
  1336. .. versionadded:: 3.7
  1337. """
  1338. super().__init__(ax)
  1339. _api.check_isinstance((dict, None), label_props=label_props,
  1340. radio_props=radio_props)
  1341. radio_props = cbook.normalize_kwargs(radio_props,
  1342. collections.PathCollection)
  1343. if activecolor is not None:
  1344. if 'facecolor' in radio_props:
  1345. _api.warn_external(
  1346. 'Both the *activecolor* parameter and the *facecolor* '
  1347. 'key in the *radio_props* parameter has been specified. '
  1348. '*activecolor* will be ignored.')
  1349. else:
  1350. activecolor = 'blue' # Default.
  1351. self._activecolor = activecolor
  1352. self._initial_active = active
  1353. self.value_selected = labels[active]
  1354. self.index_selected = active
  1355. ax.set_xticks([])
  1356. ax.set_yticks([])
  1357. ax.set_navigate(False)
  1358. ys = np.linspace(1, 0, len(labels) + 2)[1:-1]
  1359. self._useblit = useblit and self.canvas.supports_blit
  1360. self._background = None
  1361. label_props = _expand_text_props(label_props)
  1362. self.labels = [
  1363. ax.text(0.25, y, label, transform=ax.transAxes,
  1364. horizontalalignment="left", verticalalignment="center",
  1365. **props)
  1366. for y, label, props in zip(ys, labels, label_props)]
  1367. text_size = np.array([text.get_fontsize() for text in self.labels]) / 2
  1368. radio_props = {
  1369. 's': text_size**2,
  1370. **radio_props,
  1371. 'marker': 'o',
  1372. 'transform': ax.transAxes,
  1373. 'animated': self._useblit,
  1374. }
  1375. radio_props.setdefault('edgecolor', radio_props.get('color', 'black'))
  1376. radio_props.setdefault('facecolor',
  1377. radio_props.pop('color', activecolor))
  1378. self._buttons = ax.scatter([.15] * len(ys), ys, **radio_props)
  1379. # The user may have passed custom colours in radio_props, so we need to
  1380. # create the radios, and modify the visibility after getting whatever
  1381. # the user set.
  1382. self._active_colors = self._buttons.get_facecolor()
  1383. if len(self._active_colors) == 1:
  1384. self._active_colors = np.repeat(self._active_colors, len(labels),
  1385. axis=0)
  1386. self._buttons.set_facecolor(
  1387. [activecolor if i == active else "none"
  1388. for i, activecolor in enumerate(self._active_colors)])
  1389. self.connect_event('button_press_event', self._clicked)
  1390. if self._useblit:
  1391. self.connect_event('draw_event', self._clear)
  1392. self._observers = cbook.CallbackRegistry(signals=["clicked"])
  1393. def _clear(self, event):
  1394. """Internal event handler to clear the buttons."""
  1395. if self.ignore(event) or self.canvas.is_saving():
  1396. return
  1397. self._background = self.canvas.copy_from_bbox(self.ax.bbox)
  1398. self.ax.draw_artist(self._buttons)
  1399. def _clicked(self, event):
  1400. if self.ignore(event) or event.button != 1 or not self.ax.contains(event)[0]:
  1401. return
  1402. idxs = [ # Indices of buttons and of texts that contain the event.
  1403. *self._buttons.contains(event)[1]["ind"],
  1404. *[i for i, text in enumerate(self.labels) if text.contains(event)[0]]]
  1405. if idxs:
  1406. coords = self._buttons.get_offset_transform().transform(
  1407. self._buttons.get_offsets())
  1408. self.set_active( # Closest index, only looking in idxs.
  1409. idxs[(((event.x, event.y) - coords[idxs]) ** 2).sum(-1).argmin()])
  1410. def set_label_props(self, props):
  1411. """
  1412. Set properties of the `.Text` labels.
  1413. .. versionadded:: 3.7
  1414. Parameters
  1415. ----------
  1416. props : dict
  1417. Dictionary of `.Text` properties to be used for the labels. Same
  1418. format as label_props argument of :class:`RadioButtons`.
  1419. """
  1420. _api.check_isinstance(dict, props=props)
  1421. props = _expand_text_props(props)
  1422. for text, prop in zip(self.labels, props):
  1423. text.update(prop)
  1424. def set_radio_props(self, props):
  1425. """
  1426. Set properties of the `.Text` labels.
  1427. .. versionadded:: 3.7
  1428. Parameters
  1429. ----------
  1430. props : dict
  1431. Dictionary of `.Collection` properties to be used for the radio
  1432. buttons.
  1433. """
  1434. _api.check_isinstance(dict, props=props)
  1435. if 's' in props: # Keep API consistent with constructor.
  1436. props['sizes'] = np.broadcast_to(props.pop('s'), len(self.labels))
  1437. self._buttons.update(props)
  1438. self._active_colors = self._buttons.get_facecolor()
  1439. if len(self._active_colors) == 1:
  1440. self._active_colors = np.repeat(self._active_colors,
  1441. len(self.labels), axis=0)
  1442. self._buttons.set_facecolor(
  1443. [activecolor if text.get_text() == self.value_selected else "none"
  1444. for text, activecolor in zip(self.labels, self._active_colors)])
  1445. @property
  1446. def activecolor(self):
  1447. return self._activecolor
  1448. @activecolor.setter
  1449. def activecolor(self, activecolor):
  1450. colors._check_color_like(activecolor=activecolor)
  1451. self._activecolor = activecolor
  1452. self.set_radio_props({'facecolor': activecolor})
  1453. def set_active(self, index):
  1454. """
  1455. Select button with number *index*.
  1456. Callbacks will be triggered if :attr:`eventson` is True.
  1457. Parameters
  1458. ----------
  1459. index : int
  1460. The index of the button to activate.
  1461. Raises
  1462. ------
  1463. ValueError
  1464. If the index is invalid.
  1465. """
  1466. if index not in range(len(self.labels)):
  1467. raise ValueError(f'Invalid RadioButton index: {index}')
  1468. self.value_selected = self.labels[index].get_text()
  1469. self.index_selected = index
  1470. button_facecolors = self._buttons.get_facecolor()
  1471. button_facecolors[:] = colors.to_rgba("none")
  1472. button_facecolors[index] = colors.to_rgba(self._active_colors[index])
  1473. self._buttons.set_facecolor(button_facecolors)
  1474. if self.drawon:
  1475. if self._useblit:
  1476. if self._background is not None:
  1477. self.canvas.restore_region(self._background)
  1478. self.ax.draw_artist(self._buttons)
  1479. self.canvas.blit(self.ax.bbox)
  1480. else:
  1481. self.canvas.draw()
  1482. if self.eventson:
  1483. self._observers.process('clicked', self.labels[index].get_text())
  1484. def clear(self):
  1485. """Reset the active button to the initially active one."""
  1486. self.set_active(self._initial_active)
  1487. def on_clicked(self, func):
  1488. """
  1489. Connect the callback function *func* to button click events.
  1490. Parameters
  1491. ----------
  1492. func : callable
  1493. When the button is clicked, call *func* with button label.
  1494. When all buttons are cleared, call *func* with None.
  1495. The callback func must have the signature::
  1496. def func(label: str | None) -> Any
  1497. Return values may exist, but are ignored.
  1498. Returns
  1499. -------
  1500. A connection id, which can be used to disconnect the callback.
  1501. """
  1502. return self._observers.connect('clicked', func)
  1503. def disconnect(self, cid):
  1504. """Remove the observer with connection id *cid*."""
  1505. self._observers.disconnect(cid)
  1506. class SubplotTool(Widget):
  1507. """
  1508. A tool to adjust the subplot params of a `.Figure`.
  1509. """
  1510. def __init__(self, targetfig, toolfig):
  1511. """
  1512. Parameters
  1513. ----------
  1514. targetfig : `~matplotlib.figure.Figure`
  1515. The figure instance to adjust.
  1516. toolfig : `~matplotlib.figure.Figure`
  1517. The figure instance to embed the subplot tool into.
  1518. """
  1519. self.figure = toolfig
  1520. self.targetfig = targetfig
  1521. toolfig.subplots_adjust(left=0.2, right=0.9)
  1522. toolfig.suptitle("Click on slider to adjust subplot param")
  1523. self._sliders = []
  1524. names = ["left", "bottom", "right", "top", "wspace", "hspace"]
  1525. # The last subplot, removed below, keeps space for the "Reset" button.
  1526. for name, ax in zip(names, toolfig.subplots(len(names) + 1)):
  1527. ax.set_navigate(False)
  1528. slider = Slider(ax, name, 0, 1,
  1529. valinit=getattr(targetfig.subplotpars, name))
  1530. slider.on_changed(self._on_slider_changed)
  1531. self._sliders.append(slider)
  1532. toolfig.axes[-1].remove()
  1533. (self.sliderleft, self.sliderbottom, self.sliderright, self.slidertop,
  1534. self.sliderwspace, self.sliderhspace) = self._sliders
  1535. for slider in [self.sliderleft, self.sliderbottom,
  1536. self.sliderwspace, self.sliderhspace]:
  1537. slider.closedmax = False
  1538. for slider in [self.sliderright, self.slidertop]:
  1539. slider.closedmin = False
  1540. # constraints
  1541. self.sliderleft.slidermax = self.sliderright
  1542. self.sliderright.slidermin = self.sliderleft
  1543. self.sliderbottom.slidermax = self.slidertop
  1544. self.slidertop.slidermin = self.sliderbottom
  1545. bax = toolfig.add_axes([0.8, 0.05, 0.15, 0.075])
  1546. self.buttonreset = Button(bax, 'Reset')
  1547. self.buttonreset.on_clicked(self._on_reset)
  1548. def _on_slider_changed(self, _):
  1549. self.targetfig.subplots_adjust(
  1550. **{slider.label.get_text(): slider.val
  1551. for slider in self._sliders})
  1552. if self.drawon:
  1553. self.targetfig.canvas.draw()
  1554. def _on_reset(self, event):
  1555. with ExitStack() as stack:
  1556. # Temporarily disable drawing on self and self's sliders, and
  1557. # disconnect slider events (as the subplotparams can be temporarily
  1558. # invalid, depending on the order in which they are restored).
  1559. stack.enter_context(cbook._setattr_cm(self, drawon=False))
  1560. for slider in self._sliders:
  1561. stack.enter_context(
  1562. cbook._setattr_cm(slider, drawon=False, eventson=False))
  1563. # Reset the slider to the initial position.
  1564. for slider in self._sliders:
  1565. slider.reset()
  1566. if self.drawon:
  1567. event.canvas.draw() # Redraw the subplottool canvas.
  1568. self._on_slider_changed(None) # Apply changes to the target window.
  1569. class Cursor(AxesWidget):
  1570. """
  1571. A crosshair cursor that spans the Axes and moves with mouse cursor.
  1572. For the cursor to remain responsive you must keep a reference to it.
  1573. Parameters
  1574. ----------
  1575. ax : `~matplotlib.axes.Axes`
  1576. The `~.axes.Axes` to attach the cursor to.
  1577. horizOn : bool, default: True
  1578. Whether to draw the horizontal line.
  1579. vertOn : bool, default: True
  1580. Whether to draw the vertical line.
  1581. useblit : bool, default: False
  1582. Use blitting for faster drawing if supported by the backend.
  1583. See the tutorial :ref:`blitting` for details.
  1584. Other Parameters
  1585. ----------------
  1586. **lineprops
  1587. `.Line2D` properties that control the appearance of the lines.
  1588. See also `~.Axes.axhline`.
  1589. Examples
  1590. --------
  1591. See :doc:`/gallery/widgets/cursor`.
  1592. """
  1593. def __init__(self, ax, *, horizOn=True, vertOn=True, useblit=False,
  1594. **lineprops):
  1595. super().__init__(ax)
  1596. self.connect_event('motion_notify_event', self.onmove)
  1597. self.connect_event('draw_event', self.clear)
  1598. self.visible = True
  1599. self.horizOn = horizOn
  1600. self.vertOn = vertOn
  1601. self.useblit = useblit and self.canvas.supports_blit
  1602. if self.useblit:
  1603. lineprops['animated'] = True
  1604. self.lineh = ax.axhline(ax.get_ybound()[0], visible=False, **lineprops)
  1605. self.linev = ax.axvline(ax.get_xbound()[0], visible=False, **lineprops)
  1606. self.background = None
  1607. self.needclear = False
  1608. def clear(self, event):
  1609. """Internal event handler to clear the cursor."""
  1610. if self.ignore(event) or self.canvas.is_saving():
  1611. return
  1612. if self.useblit:
  1613. self.background = self.canvas.copy_from_bbox(self.ax.bbox)
  1614. def onmove(self, event):
  1615. """Internal event handler to draw the cursor when the mouse moves."""
  1616. if self.ignore(event):
  1617. return
  1618. if not self.canvas.widgetlock.available(self):
  1619. return
  1620. if not self.ax.contains(event)[0]:
  1621. self.linev.set_visible(False)
  1622. self.lineh.set_visible(False)
  1623. if self.needclear:
  1624. self.canvas.draw()
  1625. self.needclear = False
  1626. return
  1627. self.needclear = True
  1628. xdata, ydata = self._get_data_coords(event)
  1629. self.linev.set_xdata((xdata, xdata))
  1630. self.linev.set_visible(self.visible and self.vertOn)
  1631. self.lineh.set_ydata((ydata, ydata))
  1632. self.lineh.set_visible(self.visible and self.horizOn)
  1633. if not (self.visible and (self.vertOn or self.horizOn)):
  1634. return
  1635. # Redraw.
  1636. if self.useblit:
  1637. if self.background is not None:
  1638. self.canvas.restore_region(self.background)
  1639. self.ax.draw_artist(self.linev)
  1640. self.ax.draw_artist(self.lineh)
  1641. self.canvas.blit(self.ax.bbox)
  1642. else:
  1643. self.canvas.draw_idle()
  1644. class MultiCursor(Widget):
  1645. """
  1646. Provide a vertical (default) and/or horizontal line cursor shared between
  1647. multiple Axes.
  1648. For the cursor to remain responsive you must keep a reference to it.
  1649. Parameters
  1650. ----------
  1651. canvas : object
  1652. This parameter is entirely unused and only kept for back-compatibility.
  1653. axes : list of `~matplotlib.axes.Axes`
  1654. The `~.axes.Axes` to attach the cursor to.
  1655. useblit : bool, default: True
  1656. Use blitting for faster drawing if supported by the backend.
  1657. See the tutorial :ref:`blitting`
  1658. for details.
  1659. horizOn : bool, default: False
  1660. Whether to draw the horizontal line.
  1661. vertOn : bool, default: True
  1662. Whether to draw the vertical line.
  1663. Other Parameters
  1664. ----------------
  1665. **lineprops
  1666. `.Line2D` properties that control the appearance of the lines.
  1667. See also `~.Axes.axhline`.
  1668. Examples
  1669. --------
  1670. See :doc:`/gallery/widgets/multicursor`.
  1671. """
  1672. def __init__(self, canvas, axes, *, useblit=True, horizOn=False, vertOn=True,
  1673. **lineprops):
  1674. # canvas is stored only to provide the deprecated .canvas attribute;
  1675. # once it goes away the unused argument won't need to be stored at all.
  1676. self._canvas = canvas
  1677. self.axes = axes
  1678. self.horizOn = horizOn
  1679. self.vertOn = vertOn
  1680. self._canvas_infos = {
  1681. ax.get_figure(root=True).canvas:
  1682. {"cids": [], "background": None} for ax in axes}
  1683. xmin, xmax = axes[-1].get_xlim()
  1684. ymin, ymax = axes[-1].get_ylim()
  1685. xmid = 0.5 * (xmin + xmax)
  1686. ymid = 0.5 * (ymin + ymax)
  1687. self.visible = True
  1688. self.useblit = (
  1689. useblit
  1690. and all(canvas.supports_blit for canvas in self._canvas_infos))
  1691. if self.useblit:
  1692. lineprops['animated'] = True
  1693. self.vlines = [ax.axvline(xmid, visible=False, **lineprops)
  1694. for ax in axes]
  1695. self.hlines = [ax.axhline(ymid, visible=False, **lineprops)
  1696. for ax in axes]
  1697. self.connect()
  1698. def connect(self):
  1699. """Connect events."""
  1700. for canvas, info in self._canvas_infos.items():
  1701. info["cids"] = [
  1702. canvas.mpl_connect('motion_notify_event', self.onmove),
  1703. canvas.mpl_connect('draw_event', self.clear),
  1704. ]
  1705. def disconnect(self):
  1706. """Disconnect events."""
  1707. for canvas, info in self._canvas_infos.items():
  1708. for cid in info["cids"]:
  1709. canvas.mpl_disconnect(cid)
  1710. info["cids"].clear()
  1711. def clear(self, event):
  1712. """Clear the cursor."""
  1713. if self.ignore(event):
  1714. return
  1715. if self.useblit:
  1716. for canvas, info in self._canvas_infos.items():
  1717. # someone has switched the canvas on us! This happens if
  1718. # `savefig` needs to save to a format the previous backend did
  1719. # not support (e.g. saving a figure using an Agg based backend
  1720. # saved to a vector format).
  1721. if canvas is not canvas.figure.canvas:
  1722. continue
  1723. info["background"] = canvas.copy_from_bbox(canvas.figure.bbox)
  1724. def onmove(self, event):
  1725. axs = [ax for ax in self.axes if ax.contains(event)[0]]
  1726. if self.ignore(event) or not axs or not event.canvas.widgetlock.available(self):
  1727. return
  1728. ax = cbook._topmost_artist(axs)
  1729. xdata, ydata = ((event.xdata, event.ydata) if event.inaxes is ax
  1730. else ax.transData.inverted().transform((event.x, event.y)))
  1731. for line in self.vlines:
  1732. line.set_xdata((xdata, xdata))
  1733. line.set_visible(self.visible and self.vertOn)
  1734. for line in self.hlines:
  1735. line.set_ydata((ydata, ydata))
  1736. line.set_visible(self.visible and self.horizOn)
  1737. if not (self.visible and (self.vertOn or self.horizOn)):
  1738. return
  1739. # Redraw.
  1740. if self.useblit:
  1741. for canvas, info in self._canvas_infos.items():
  1742. if info["background"]:
  1743. canvas.restore_region(info["background"])
  1744. if self.vertOn:
  1745. for ax, line in zip(self.axes, self.vlines):
  1746. ax.draw_artist(line)
  1747. if self.horizOn:
  1748. for ax, line in zip(self.axes, self.hlines):
  1749. ax.draw_artist(line)
  1750. for canvas in self._canvas_infos:
  1751. canvas.blit()
  1752. else:
  1753. for canvas in self._canvas_infos:
  1754. canvas.draw_idle()
  1755. class _SelectorWidget(AxesWidget):
  1756. def __init__(self, ax, onselect=None, useblit=False, button=None,
  1757. state_modifier_keys=None, use_data_coordinates=False):
  1758. super().__init__(ax)
  1759. self._visible = True
  1760. if onselect is None:
  1761. self.onselect = lambda *args: None
  1762. else:
  1763. self.onselect = onselect
  1764. self.useblit = useblit and self.canvas.supports_blit
  1765. self.connect_default_events()
  1766. self._state_modifier_keys = dict(move=' ', clear='escape',
  1767. square='shift', center='control',
  1768. rotate='r')
  1769. self._state_modifier_keys.update(state_modifier_keys or {})
  1770. self._use_data_coordinates = use_data_coordinates
  1771. self.background = None
  1772. if isinstance(button, Integral):
  1773. self.validButtons = [button]
  1774. else:
  1775. self.validButtons = button
  1776. # Set to True when a selection is completed, otherwise is False
  1777. self._selection_completed = False
  1778. # will save the data (position at mouseclick)
  1779. self._eventpress = None
  1780. # will save the data (pos. at mouserelease)
  1781. self._eventrelease = None
  1782. self._prev_event = None
  1783. self._state = set()
  1784. def set_active(self, active):
  1785. super().set_active(active)
  1786. if active:
  1787. self.update_background(None)
  1788. def _get_animated_artists(self):
  1789. """
  1790. Convenience method to get all animated artists of the figure containing
  1791. this widget, excluding those already present in self.artists.
  1792. The returned tuple is not sorted by 'z_order': z_order sorting is
  1793. valid only when considering all artists and not only a subset of all
  1794. artists.
  1795. """
  1796. return tuple(a for ax_ in self.ax.get_figure().get_axes()
  1797. for a in ax_.get_children()
  1798. if a.get_animated() and a not in self.artists)
  1799. def update_background(self, event):
  1800. """Force an update of the background."""
  1801. # If you add a call to `ignore` here, you'll want to check edge case:
  1802. # `release` can call a draw event even when `ignore` is True.
  1803. if not self.useblit:
  1804. return
  1805. if self.canvas.is_saving():
  1806. return # saving does not use blitting
  1807. # Make sure that widget artists don't get accidentally included in the
  1808. # background, by re-rendering the background if needed (and then
  1809. # re-re-rendering the canvas with the visible widget artists).
  1810. # We need to remove all artists which will be drawn when updating
  1811. # the selector: if we have animated artists in the figure, it is safer
  1812. # to redrawn by default, in case they have updated by the callback
  1813. # zorder needs to be respected when redrawing
  1814. artists = sorted(self.artists + self._get_animated_artists(),
  1815. key=lambda a: a.get_zorder())
  1816. needs_redraw = any(artist.get_visible() for artist in artists)
  1817. with ExitStack() as stack:
  1818. if needs_redraw:
  1819. for artist in artists:
  1820. stack.enter_context(artist._cm_set(visible=False))
  1821. self.canvas.draw()
  1822. self.background = self.canvas.copy_from_bbox(self.ax.bbox)
  1823. if needs_redraw:
  1824. for artist in artists:
  1825. self.ax.draw_artist(artist)
  1826. def connect_default_events(self):
  1827. """Connect the major canvas events to methods."""
  1828. self.connect_event('motion_notify_event', self.onmove)
  1829. self.connect_event('button_press_event', self.press)
  1830. self.connect_event('button_release_event', self.release)
  1831. self.connect_event('draw_event', self.update_background)
  1832. self.connect_event('key_press_event', self.on_key_press)
  1833. self.connect_event('key_release_event', self.on_key_release)
  1834. self.connect_event('scroll_event', self.on_scroll)
  1835. def ignore(self, event):
  1836. # docstring inherited
  1837. if not self.active or not self.ax.get_visible():
  1838. return True
  1839. # If canvas was locked
  1840. if not self.canvas.widgetlock.available(self):
  1841. return True
  1842. if not hasattr(event, 'button'):
  1843. event.button = None
  1844. # Only do rectangle selection if event was triggered
  1845. # with a desired button
  1846. if (self.validButtons is not None
  1847. and event.button not in self.validButtons):
  1848. return True
  1849. # If no button was pressed yet ignore the event if it was out of the Axes.
  1850. if self._eventpress is None:
  1851. return not self.ax.contains(event)[0]
  1852. # If a button was pressed, check if the release-button is the same.
  1853. if event.button == self._eventpress.button:
  1854. return False
  1855. # If a button was pressed, check if the release-button is the same.
  1856. return (not self.ax.contains(event)[0] or
  1857. event.button != self._eventpress.button)
  1858. def update(self):
  1859. """Draw using blit() or draw_idle(), depending on ``self.useblit``."""
  1860. if (not self.ax.get_visible() or
  1861. self.ax.get_figure(root=True)._get_renderer() is None):
  1862. return
  1863. if self.useblit:
  1864. if self.background is not None:
  1865. self.canvas.restore_region(self.background)
  1866. else:
  1867. self.update_background(None)
  1868. # We need to draw all artists, which are not included in the
  1869. # background, therefore we also draw self._get_animated_artists()
  1870. # and we make sure that we respect z_order
  1871. artists = sorted(self.artists + self._get_animated_artists(),
  1872. key=lambda a: a.get_zorder())
  1873. for artist in artists:
  1874. self.ax.draw_artist(artist)
  1875. self.canvas.blit(self.ax.bbox)
  1876. else:
  1877. self.canvas.draw_idle()
  1878. def _get_data(self, event):
  1879. """Get the xdata and ydata for event, with limits."""
  1880. if event.xdata is None:
  1881. return None, None
  1882. xdata, ydata = self._get_data_coords(event)
  1883. xdata = np.clip(xdata, *self.ax.get_xbound())
  1884. ydata = np.clip(ydata, *self.ax.get_ybound())
  1885. return xdata, ydata
  1886. def _clean_event(self, event):
  1887. """
  1888. Preprocess an event:
  1889. - Replace *event* by the previous event if *event* has no ``xdata``.
  1890. - Get ``xdata`` and ``ydata`` from this widget's Axes, and clip them to the axes
  1891. limits.
  1892. - Update the previous event.
  1893. """
  1894. if event.xdata is None:
  1895. event = self._prev_event
  1896. else:
  1897. event = copy.copy(event)
  1898. event.xdata, event.ydata = self._get_data(event)
  1899. self._prev_event = event
  1900. return event
  1901. def press(self, event):
  1902. """Button press handler and validator."""
  1903. if not self.ignore(event):
  1904. event = self._clean_event(event)
  1905. self._eventpress = event
  1906. self._prev_event = event
  1907. key = event.key or ''
  1908. key = key.replace('ctrl', 'control')
  1909. # move state is locked in on a button press
  1910. if key == self._state_modifier_keys['move']:
  1911. self._state.add('move')
  1912. self._press(event)
  1913. return True
  1914. return False
  1915. def _press(self, event):
  1916. """Button press event handler."""
  1917. def release(self, event):
  1918. """Button release event handler and validator."""
  1919. if not self.ignore(event) and self._eventpress:
  1920. event = self._clean_event(event)
  1921. self._eventrelease = event
  1922. self._release(event)
  1923. self._eventpress = None
  1924. self._eventrelease = None
  1925. self._state.discard('move')
  1926. return True
  1927. return False
  1928. def _release(self, event):
  1929. """Button release event handler."""
  1930. def onmove(self, event):
  1931. """Cursor move event handler and validator."""
  1932. if not self.ignore(event) and self._eventpress:
  1933. event = self._clean_event(event)
  1934. self._onmove(event)
  1935. return True
  1936. return False
  1937. def _onmove(self, event):
  1938. """Cursor move event handler."""
  1939. def on_scroll(self, event):
  1940. """Mouse scroll event handler and validator."""
  1941. if not self.ignore(event):
  1942. self._on_scroll(event)
  1943. def _on_scroll(self, event):
  1944. """Mouse scroll event handler."""
  1945. def on_key_press(self, event):
  1946. """Key press event handler and validator for all selection widgets."""
  1947. if self.active:
  1948. key = event.key or ''
  1949. key = key.replace('ctrl', 'control')
  1950. if key == self._state_modifier_keys['clear']:
  1951. self.clear()
  1952. return
  1953. for (state, modifier) in self._state_modifier_keys.items():
  1954. if modifier in key.split('+'):
  1955. # 'rotate' is changing _state on press and is not removed
  1956. # from _state when releasing
  1957. if state == 'rotate':
  1958. if state in self._state:
  1959. self._state.discard(state)
  1960. else:
  1961. self._state.add(state)
  1962. else:
  1963. self._state.add(state)
  1964. self._on_key_press(event)
  1965. def _on_key_press(self, event):
  1966. """Key press event handler - for widget-specific key press actions."""
  1967. def on_key_release(self, event):
  1968. """Key release event handler and validator."""
  1969. if self.active:
  1970. key = event.key or ''
  1971. for (state, modifier) in self._state_modifier_keys.items():
  1972. # 'rotate' is changing _state on press and is not removed
  1973. # from _state when releasing
  1974. if modifier in key.split('+') and state != 'rotate':
  1975. self._state.discard(state)
  1976. self._on_key_release(event)
  1977. def _on_key_release(self, event):
  1978. """Key release event handler."""
  1979. def set_visible(self, visible):
  1980. """Set the visibility of the selector artists."""
  1981. self._visible = visible
  1982. for artist in self.artists:
  1983. artist.set_visible(visible)
  1984. def get_visible(self):
  1985. """Get the visibility of the selector artists."""
  1986. return self._visible
  1987. def clear(self):
  1988. """Clear the selection and set the selector ready to make a new one."""
  1989. self._clear_without_update()
  1990. self.update()
  1991. def _clear_without_update(self):
  1992. self._selection_completed = False
  1993. self.set_visible(False)
  1994. @property
  1995. def artists(self):
  1996. """Tuple of the artists of the selector."""
  1997. handles_artists = getattr(self, '_handles_artists', ())
  1998. return (self._selection_artist,) + handles_artists
  1999. def set_props(self, **props):
  2000. """
  2001. Set the properties of the selector artist.
  2002. See the *props* argument in the selector docstring to know which properties are
  2003. supported.
  2004. """
  2005. artist = self._selection_artist
  2006. props = cbook.normalize_kwargs(props, artist)
  2007. artist.set(**props)
  2008. if self.useblit:
  2009. self.update()
  2010. def set_handle_props(self, **handle_props):
  2011. """
  2012. Set the properties of the handles selector artist. See the
  2013. `handle_props` argument in the selector docstring to know which
  2014. properties are supported.
  2015. """
  2016. if not hasattr(self, '_handles_artists'):
  2017. raise NotImplementedError("This selector doesn't have handles.")
  2018. artist = self._handles_artists[0]
  2019. handle_props = cbook.normalize_kwargs(handle_props, artist)
  2020. for handle in self._handles_artists:
  2021. handle.set(**handle_props)
  2022. if self.useblit:
  2023. self.update()
  2024. self._handle_props.update(handle_props)
  2025. def _validate_state(self, state):
  2026. supported_state = [
  2027. key for key, value in self._state_modifier_keys.items()
  2028. if key != 'clear' and value != 'not-applicable'
  2029. ]
  2030. _api.check_in_list(supported_state, state=state)
  2031. def add_state(self, state):
  2032. """
  2033. Add a state to define the widget's behavior. See the
  2034. `state_modifier_keys` parameters for details.
  2035. Parameters
  2036. ----------
  2037. state : str
  2038. Must be a supported state of the selector. See the
  2039. `state_modifier_keys` parameters for details.
  2040. Raises
  2041. ------
  2042. ValueError
  2043. When the state is not supported by the selector.
  2044. """
  2045. self._validate_state(state)
  2046. self._state.add(state)
  2047. def remove_state(self, state):
  2048. """
  2049. Remove a state to define the widget's behavior. See the
  2050. `state_modifier_keys` parameters for details.
  2051. Parameters
  2052. ----------
  2053. state : str
  2054. Must be a supported state of the selector. See the
  2055. `state_modifier_keys` parameters for details.
  2056. Raises
  2057. ------
  2058. ValueError
  2059. When the state is not supported by the selector.
  2060. """
  2061. self._validate_state(state)
  2062. self._state.remove(state)
  2063. class SpanSelector(_SelectorWidget):
  2064. """
  2065. Visually select a min/max range on a single axis and call a function with
  2066. those values.
  2067. To guarantee that the selector remains responsive, keep a reference to it.
  2068. In order to turn off the SpanSelector, set ``span_selector.active`` to
  2069. False. To turn it back on, set it to True.
  2070. Press and release events triggered at the same coordinates outside the
  2071. selection will clear the selector, except when
  2072. ``ignore_event_outside=True``.
  2073. Parameters
  2074. ----------
  2075. ax : `~matplotlib.axes.Axes`
  2076. onselect : callable with signature ``func(min: float, max: float)``
  2077. A callback function that is called after a release event and the
  2078. selection is created, changed or removed.
  2079. direction : {"horizontal", "vertical"}
  2080. The direction along which to draw the span selector.
  2081. minspan : float, default: 0
  2082. If selection is less than or equal to *minspan*, the selection is
  2083. removed (when already existing) or cancelled.
  2084. useblit : bool, default: False
  2085. If True, use the backend-dependent blitting features for faster
  2086. canvas updates. See the tutorial :ref:`blitting` for details.
  2087. props : dict, default: {'facecolor': 'red', 'alpha': 0.5}
  2088. Dictionary of `.Patch` properties.
  2089. onmove_callback : callable with signature ``func(min: float, max: float)``, optional
  2090. Called on mouse move while the span is being selected.
  2091. interactive : bool, default: False
  2092. Whether to draw a set of handles that allow interaction with the
  2093. widget after it is drawn.
  2094. button : `.MouseButton` or list of `.MouseButton`, default: all buttons
  2095. The mouse buttons which activate the span selector.
  2096. handle_props : dict, default: None
  2097. Properties of the handle lines at the edges of the span. Only used
  2098. when *interactive* is True. See `.Line2D` for valid properties.
  2099. grab_range : float, default: 10
  2100. Distance in pixels within which the interactive tool handles can be activated.
  2101. state_modifier_keys : dict, optional
  2102. Keyboard modifiers which affect the widget's behavior. Values
  2103. amend the defaults, which are:
  2104. - "clear": Clear the current shape, default: "escape".
  2105. drag_from_anywhere : bool, default: False
  2106. If `True`, the widget can be moved by clicking anywhere within its bounds.
  2107. ignore_event_outside : bool, default: False
  2108. If `True`, the event triggered outside the span selector will be ignored.
  2109. snap_values : 1D array-like, optional
  2110. Snap the selector edges to the given values.
  2111. Examples
  2112. --------
  2113. >>> import matplotlib.pyplot as plt
  2114. >>> import matplotlib.widgets as mwidgets
  2115. >>> fig, ax = plt.subplots()
  2116. >>> ax.plot([1, 2, 3], [10, 50, 100])
  2117. >>> def onselect(vmin, vmax):
  2118. ... print(vmin, vmax)
  2119. >>> span = mwidgets.SpanSelector(ax, onselect, 'horizontal',
  2120. ... props=dict(facecolor='blue', alpha=0.5))
  2121. >>> fig.show()
  2122. See also: :doc:`/gallery/widgets/span_selector`
  2123. """
  2124. def __init__(self, ax, onselect, direction, *, minspan=0, useblit=False,
  2125. props=None, onmove_callback=None, interactive=False,
  2126. button=None, handle_props=None, grab_range=10,
  2127. state_modifier_keys=None, drag_from_anywhere=False,
  2128. ignore_event_outside=False, snap_values=None):
  2129. if state_modifier_keys is None:
  2130. state_modifier_keys = dict(clear='escape',
  2131. square='not-applicable',
  2132. center='not-applicable',
  2133. rotate='not-applicable')
  2134. super().__init__(ax, onselect, useblit=useblit, button=button,
  2135. state_modifier_keys=state_modifier_keys)
  2136. if props is None:
  2137. props = dict(facecolor='red', alpha=0.5)
  2138. props['animated'] = self.useblit
  2139. self.direction = direction
  2140. self._extents_on_press = None
  2141. self.snap_values = snap_values
  2142. self.onmove_callback = onmove_callback
  2143. self.minspan = minspan
  2144. self.grab_range = grab_range
  2145. self._interactive = interactive
  2146. self._edge_handles = None
  2147. self.drag_from_anywhere = drag_from_anywhere
  2148. self.ignore_event_outside = ignore_event_outside
  2149. self.new_axes(ax, _props=props, _init=True)
  2150. # Setup handles
  2151. self._handle_props = {
  2152. 'color': props.get('facecolor', 'r'),
  2153. **cbook.normalize_kwargs(handle_props, Line2D)}
  2154. if self._interactive:
  2155. self._edge_order = ['min', 'max']
  2156. self._setup_edge_handles(self._handle_props)
  2157. self._active_handle = None
  2158. def new_axes(self, ax, *, _props=None, _init=False):
  2159. """Set SpanSelector to operate on a new Axes."""
  2160. reconnect = False
  2161. if _init or self.canvas is not ax.get_figure(root=True).canvas:
  2162. if self.canvas is not None:
  2163. self.disconnect_events()
  2164. reconnect = True
  2165. self.ax = ax
  2166. if reconnect:
  2167. self.connect_default_events()
  2168. # Reset
  2169. self._selection_completed = False
  2170. if self.direction == 'horizontal':
  2171. trans = ax.get_xaxis_transform()
  2172. w, h = 0, 1
  2173. else:
  2174. trans = ax.get_yaxis_transform()
  2175. w, h = 1, 0
  2176. rect_artist = Rectangle((0, 0), w, h, transform=trans, visible=False)
  2177. if _props is not None:
  2178. rect_artist.update(_props)
  2179. elif self._selection_artist is not None:
  2180. rect_artist.update_from(self._selection_artist)
  2181. self.ax.add_patch(rect_artist)
  2182. self._selection_artist = rect_artist
  2183. def _setup_edge_handles(self, props):
  2184. # Define initial position using the axis bounds to keep the same bounds
  2185. if self.direction == 'horizontal':
  2186. positions = self.ax.get_xbound()
  2187. else:
  2188. positions = self.ax.get_ybound()
  2189. self._edge_handles = ToolLineHandles(self.ax, positions,
  2190. direction=self.direction,
  2191. line_props=props,
  2192. useblit=self.useblit)
  2193. @property
  2194. def _handles_artists(self):
  2195. if self._edge_handles is not None:
  2196. return self._edge_handles.artists
  2197. else:
  2198. return ()
  2199. def _set_cursor(self, enabled):
  2200. """Update the canvas cursor based on direction of the selector."""
  2201. if enabled:
  2202. cursor = (backend_tools.Cursors.RESIZE_HORIZONTAL
  2203. if self.direction == 'horizontal' else
  2204. backend_tools.Cursors.RESIZE_VERTICAL)
  2205. else:
  2206. cursor = backend_tools.Cursors.POINTER
  2207. self.ax.get_figure(root=True).canvas.set_cursor(cursor)
  2208. def connect_default_events(self):
  2209. # docstring inherited
  2210. super().connect_default_events()
  2211. if getattr(self, '_interactive', False):
  2212. self.connect_event('motion_notify_event', self._hover)
  2213. def _press(self, event):
  2214. """Button press event handler."""
  2215. self._set_cursor(True)
  2216. if self._interactive and self._selection_artist.get_visible():
  2217. self._set_active_handle(event)
  2218. else:
  2219. self._active_handle = None
  2220. if self._active_handle is None or not self._interactive:
  2221. # Clear previous rectangle before drawing new rectangle.
  2222. self.update()
  2223. xdata, ydata = self._get_data_coords(event)
  2224. v = xdata if self.direction == 'horizontal' else ydata
  2225. if self._active_handle is None and not self.ignore_event_outside:
  2226. # when the press event outside the span, we initially set the
  2227. # visibility to False and extents to (v, v)
  2228. # update will be called when setting the extents
  2229. self._visible = False
  2230. self._set_extents((v, v))
  2231. # We need to set the visibility back, so the span selector will be
  2232. # drawn when necessary (span width > 0)
  2233. self._visible = True
  2234. else:
  2235. self.set_visible(True)
  2236. return False
  2237. @property
  2238. def direction(self):
  2239. """Direction of the span selector: 'vertical' or 'horizontal'."""
  2240. return self._direction
  2241. @direction.setter
  2242. def direction(self, direction):
  2243. """Set the direction of the span selector."""
  2244. _api.check_in_list(['horizontal', 'vertical'], direction=direction)
  2245. if hasattr(self, '_direction') and direction != self._direction:
  2246. # remove previous artists
  2247. self._selection_artist.remove()
  2248. if self._interactive:
  2249. self._edge_handles.remove()
  2250. self._direction = direction
  2251. self.new_axes(self.ax)
  2252. if self._interactive:
  2253. self._setup_edge_handles(self._handle_props)
  2254. else:
  2255. self._direction = direction
  2256. def _release(self, event):
  2257. """Button release event handler."""
  2258. self._set_cursor(False)
  2259. if not self._interactive:
  2260. self._selection_artist.set_visible(False)
  2261. if (self._active_handle is None and self._selection_completed and
  2262. self.ignore_event_outside):
  2263. return
  2264. vmin, vmax = self.extents
  2265. span = vmax - vmin
  2266. if span <= self.minspan:
  2267. # Remove span and set self._selection_completed = False
  2268. self.set_visible(False)
  2269. if self._selection_completed:
  2270. # Call onselect, only when the span is already existing
  2271. self.onselect(vmin, vmax)
  2272. self._selection_completed = False
  2273. else:
  2274. self.onselect(vmin, vmax)
  2275. self._selection_completed = True
  2276. self.update()
  2277. self._active_handle = None
  2278. return False
  2279. def _hover(self, event):
  2280. """Update the canvas cursor if it's over a handle."""
  2281. if self.ignore(event):
  2282. return
  2283. if self._active_handle is not None or not self._selection_completed:
  2284. # Do nothing if button is pressed and a handle is active, which may
  2285. # occur with drag_from_anywhere=True.
  2286. # Do nothing if selection is not completed, which occurs when
  2287. # a selector has been cleared
  2288. return
  2289. _, e_dist = self._edge_handles.closest(event.x, event.y)
  2290. self._set_cursor(e_dist <= self.grab_range)
  2291. def _onmove(self, event):
  2292. """Motion notify event handler."""
  2293. xdata, ydata = self._get_data_coords(event)
  2294. if self.direction == 'horizontal':
  2295. v = xdata
  2296. vpress = self._eventpress.xdata
  2297. else:
  2298. v = ydata
  2299. vpress = self._eventpress.ydata
  2300. # move existing span
  2301. # When "dragging from anywhere", `self._active_handle` is set to 'C'
  2302. # (match notation used in the RectangleSelector)
  2303. if self._active_handle == 'C' and self._extents_on_press is not None:
  2304. vmin, vmax = self._extents_on_press
  2305. dv = v - vpress
  2306. vmin += dv
  2307. vmax += dv
  2308. # resize an existing shape
  2309. elif self._active_handle and self._active_handle != 'C':
  2310. vmin, vmax = self._extents_on_press
  2311. if self._active_handle == 'min':
  2312. vmin = v
  2313. else:
  2314. vmax = v
  2315. # new shape
  2316. else:
  2317. # Don't create a new span if there is already one when
  2318. # ignore_event_outside=True
  2319. if self.ignore_event_outside and self._selection_completed:
  2320. return
  2321. vmin, vmax = vpress, v
  2322. if vmin > vmax:
  2323. vmin, vmax = vmax, vmin
  2324. self._set_extents((vmin, vmax))
  2325. if self.onmove_callback is not None:
  2326. self.onmove_callback(vmin, vmax)
  2327. return False
  2328. def _draw_shape(self, vmin, vmax):
  2329. if vmin > vmax:
  2330. vmin, vmax = vmax, vmin
  2331. if self.direction == 'horizontal':
  2332. self._selection_artist.set_x(vmin)
  2333. self._selection_artist.set_width(vmax - vmin)
  2334. else:
  2335. self._selection_artist.set_y(vmin)
  2336. self._selection_artist.set_height(vmax - vmin)
  2337. def _set_active_handle(self, event):
  2338. """Set active handle based on the location of the mouse event."""
  2339. # Note: event.xdata/ydata in data coordinates, event.x/y in pixels
  2340. e_idx, e_dist = self._edge_handles.closest(event.x, event.y)
  2341. # Prioritise center handle over other handles
  2342. # Use 'C' to match the notation used in the RectangleSelector
  2343. if 'move' in self._state:
  2344. self._active_handle = 'C'
  2345. elif e_dist > self.grab_range:
  2346. # Not close to any handles
  2347. self._active_handle = None
  2348. if self.drag_from_anywhere and self._contains(event):
  2349. # Check if we've clicked inside the region
  2350. self._active_handle = 'C'
  2351. self._extents_on_press = self.extents
  2352. else:
  2353. self._active_handle = None
  2354. return
  2355. else:
  2356. # Closest to an edge handle
  2357. self._active_handle = self._edge_order[e_idx]
  2358. # Save coordinates of rectangle at the start of handle movement.
  2359. self._extents_on_press = self.extents
  2360. def _contains(self, event):
  2361. """Return True if event is within the patch."""
  2362. return self._selection_artist.contains(event, radius=0)[0]
  2363. @staticmethod
  2364. def _snap(values, snap_values):
  2365. """Snap values to a given array values (snap_values)."""
  2366. # take into account machine precision
  2367. eps = np.min(np.abs(np.diff(snap_values))) * 1e-12
  2368. return tuple(
  2369. snap_values[np.abs(snap_values - v + np.sign(v) * eps).argmin()]
  2370. for v in values)
  2371. @property
  2372. def extents(self):
  2373. """
  2374. (float, float)
  2375. The values, in data coordinates, for the start and end points of the current
  2376. selection. If there is no selection then the start and end values will be
  2377. the same.
  2378. """
  2379. if self.direction == 'horizontal':
  2380. vmin = self._selection_artist.get_x()
  2381. vmax = vmin + self._selection_artist.get_width()
  2382. else:
  2383. vmin = self._selection_artist.get_y()
  2384. vmax = vmin + self._selection_artist.get_height()
  2385. return vmin, vmax
  2386. @extents.setter
  2387. def extents(self, extents):
  2388. self._set_extents(extents)
  2389. self._selection_completed = True
  2390. def _set_extents(self, extents):
  2391. # Update displayed shape
  2392. if self.snap_values is not None:
  2393. extents = tuple(self._snap(extents, self.snap_values))
  2394. self._draw_shape(*extents)
  2395. if self._interactive:
  2396. # Update displayed handles
  2397. self._edge_handles.set_data(self.extents)
  2398. self.set_visible(self._visible)
  2399. self.update()
  2400. class ToolLineHandles:
  2401. """
  2402. Control handles for canvas tools.
  2403. Parameters
  2404. ----------
  2405. ax : `~matplotlib.axes.Axes`
  2406. Matplotlib Axes where tool handles are displayed.
  2407. positions : 1D array
  2408. Positions of handles in data coordinates.
  2409. direction : {"horizontal", "vertical"}
  2410. Direction of handles, either 'vertical' or 'horizontal'
  2411. line_props : dict, optional
  2412. Additional line properties. See `.Line2D`.
  2413. useblit : bool, default: True
  2414. Whether to use blitting for faster drawing (if supported by the
  2415. backend). See the tutorial :ref:`blitting`
  2416. for details.
  2417. """
  2418. def __init__(self, ax, positions, direction, *, line_props=None,
  2419. useblit=True):
  2420. self.ax = ax
  2421. _api.check_in_list(['horizontal', 'vertical'], direction=direction)
  2422. self._direction = direction
  2423. line_props = {
  2424. **(line_props if line_props is not None else {}),
  2425. 'visible': False,
  2426. 'animated': useblit,
  2427. }
  2428. line_fun = ax.axvline if self.direction == 'horizontal' else ax.axhline
  2429. self._artists = [line_fun(p, **line_props) for p in positions]
  2430. @property
  2431. def artists(self):
  2432. return tuple(self._artists)
  2433. @property
  2434. def positions(self):
  2435. """Positions of the handle in data coordinates."""
  2436. method = 'get_xdata' if self.direction == 'horizontal' else 'get_ydata'
  2437. return [getattr(line, method)()[0] for line in self.artists]
  2438. @property
  2439. def direction(self):
  2440. """Direction of the handle: 'vertical' or 'horizontal'."""
  2441. return self._direction
  2442. def set_data(self, positions):
  2443. """
  2444. Set x- or y-positions of handles, depending on if the lines are
  2445. vertical or horizontal.
  2446. Parameters
  2447. ----------
  2448. positions : tuple of length 2
  2449. Set the positions of the handle in data coordinates
  2450. """
  2451. method = 'set_xdata' if self.direction == 'horizontal' else 'set_ydata'
  2452. for line, p in zip(self.artists, positions):
  2453. getattr(line, method)([p, p])
  2454. def set_visible(self, value):
  2455. """Set the visibility state of the handles artist."""
  2456. for artist in self.artists:
  2457. artist.set_visible(value)
  2458. def set_animated(self, value):
  2459. """Set the animated state of the handles artist."""
  2460. for artist in self.artists:
  2461. artist.set_animated(value)
  2462. def remove(self):
  2463. """Remove the handles artist from the figure."""
  2464. for artist in self._artists:
  2465. artist.remove()
  2466. def closest(self, x, y):
  2467. """
  2468. Return index and pixel distance to closest handle.
  2469. Parameters
  2470. ----------
  2471. x, y : float
  2472. x, y position from which the distance will be calculated to
  2473. determinate the closest handle
  2474. Returns
  2475. -------
  2476. index, distance : index of the handle and its distance from
  2477. position x, y
  2478. """
  2479. if self.direction == 'horizontal':
  2480. p_pts = np.array([
  2481. self.ax.transData.transform((p, 0))[0] for p in self.positions
  2482. ])
  2483. dist = abs(p_pts - x)
  2484. else:
  2485. p_pts = np.array([
  2486. self.ax.transData.transform((0, p))[1] for p in self.positions
  2487. ])
  2488. dist = abs(p_pts - y)
  2489. index = np.argmin(dist)
  2490. return index, dist[index]
  2491. class ToolHandles:
  2492. """
  2493. Control handles for canvas tools.
  2494. Parameters
  2495. ----------
  2496. ax : `~matplotlib.axes.Axes`
  2497. Matplotlib Axes where tool handles are displayed.
  2498. x, y : 1D arrays
  2499. Coordinates of control handles.
  2500. marker : str, default: 'o'
  2501. Shape of marker used to display handle. See `~.pyplot.plot`.
  2502. marker_props : dict, optional
  2503. Additional marker properties. See `.Line2D`.
  2504. useblit : bool, default: True
  2505. Whether to use blitting for faster drawing (if supported by the
  2506. backend). See the tutorial :ref:`blitting`
  2507. for details.
  2508. """
  2509. def __init__(self, ax, x, y, *, marker='o', marker_props=None, useblit=True):
  2510. self.ax = ax
  2511. props = {'marker': marker, 'markersize': 7, 'markerfacecolor': 'w',
  2512. 'linestyle': 'none', 'alpha': 0.5, 'visible': False,
  2513. 'label': '_nolegend_',
  2514. **cbook.normalize_kwargs(marker_props, Line2D._alias_map)}
  2515. self._markers = Line2D(x, y, animated=useblit, **props)
  2516. self.ax.add_line(self._markers)
  2517. @property
  2518. def x(self):
  2519. return self._markers.get_xdata()
  2520. @property
  2521. def y(self):
  2522. return self._markers.get_ydata()
  2523. @property
  2524. def artists(self):
  2525. return (self._markers, )
  2526. def set_data(self, pts, y=None):
  2527. """Set x and y positions of handles."""
  2528. if y is not None:
  2529. x = pts
  2530. pts = np.array([x, y])
  2531. self._markers.set_data(pts)
  2532. def set_visible(self, val):
  2533. self._markers.set_visible(val)
  2534. def set_animated(self, val):
  2535. self._markers.set_animated(val)
  2536. def closest(self, x, y):
  2537. """Return index and pixel distance to closest index."""
  2538. pts = np.column_stack([self.x, self.y])
  2539. # Transform data coordinates to pixel coordinates.
  2540. pts = self.ax.transData.transform(pts)
  2541. diff = pts - [x, y]
  2542. dist = np.hypot(*diff.T)
  2543. min_index = np.argmin(dist)
  2544. return min_index, dist[min_index]
  2545. _RECTANGLESELECTOR_PARAMETERS_DOCSTRING = \
  2546. r"""
  2547. Parameters
  2548. ----------
  2549. ax : `~matplotlib.axes.Axes`
  2550. The parent Axes for the widget.
  2551. onselect : function, optional
  2552. A callback function that is called after a release event and the
  2553. selection is created, changed or removed.
  2554. It must have the signature::
  2555. def onselect(eclick: MouseEvent, erelease: MouseEvent)
  2556. where *eclick* and *erelease* are the mouse click and release
  2557. `.MouseEvent`\s that start and complete the selection.
  2558. minspanx : float, default: 0
  2559. Selections with an x-span less than or equal to *minspanx* are removed
  2560. (when already existing) or cancelled.
  2561. minspany : float, default: 0
  2562. Selections with an y-span less than or equal to *minspanx* are removed
  2563. (when already existing) or cancelled.
  2564. useblit : bool, default: False
  2565. Whether to use blitting for faster drawing (if supported by the
  2566. backend). See the tutorial :ref:`blitting`
  2567. for details.
  2568. props : dict, optional
  2569. Properties with which the __ARTIST_NAME__ is drawn. See
  2570. `.Patch` for valid properties.
  2571. Default:
  2572. ``dict(facecolor='red', edgecolor='black', alpha=0.2, fill=True)``
  2573. spancoords : {"data", "pixels"}, default: "data"
  2574. Whether to interpret *minspanx* and *minspany* in data or in pixel
  2575. coordinates.
  2576. button : `.MouseButton`, list of `.MouseButton`, default: all buttons
  2577. Button(s) that trigger rectangle selection.
  2578. grab_range : float, default: 10
  2579. Distance in pixels within which the interactive tool handles can be
  2580. activated.
  2581. handle_props : dict, optional
  2582. Properties with which the interactive handles (marker artists) are
  2583. drawn. See the marker arguments in `.Line2D` for valid
  2584. properties. Default values are defined in ``mpl.rcParams`` except for
  2585. the default value of ``markeredgecolor`` which will be the same as the
  2586. ``edgecolor`` property in *props*.
  2587. interactive : bool, default: False
  2588. Whether to draw a set of handles that allow interaction with the
  2589. widget after it is drawn.
  2590. state_modifier_keys : dict, optional
  2591. Keyboard modifiers which affect the widget's behavior. Values
  2592. amend the defaults, which are:
  2593. - "move": Move the existing shape, default: no modifier.
  2594. - "clear": Clear the current shape, default: "escape".
  2595. - "square": Make the shape square, default: "shift".
  2596. - "center": change the shape around its center, default: "ctrl".
  2597. - "rotate": Rotate the shape around its center between -45° and 45°,
  2598. default: "r".
  2599. "square" and "center" can be combined. The square shape can be defined
  2600. in data or display coordinates as determined by the
  2601. ``use_data_coordinates`` argument specified when creating the selector.
  2602. drag_from_anywhere : bool, default: False
  2603. If `True`, the widget can be moved by clicking anywhere within
  2604. its bounds.
  2605. ignore_event_outside : bool, default: False
  2606. If `True`, the event triggered outside the span selector will be
  2607. ignored.
  2608. use_data_coordinates : bool, default: False
  2609. If `True`, the "square" shape of the selector is defined in
  2610. data coordinates instead of display coordinates.
  2611. """
  2612. @_docstring.Substitution(_RECTANGLESELECTOR_PARAMETERS_DOCSTRING.replace(
  2613. '__ARTIST_NAME__', 'rectangle'))
  2614. class RectangleSelector(_SelectorWidget):
  2615. """
  2616. Select a rectangular region of an Axes.
  2617. For the cursor to remain responsive you must keep a reference to it.
  2618. Press and release events triggered at the same coordinates outside the
  2619. selection will clear the selector, except when
  2620. ``ignore_event_outside=True``.
  2621. %s
  2622. Examples
  2623. --------
  2624. >>> import matplotlib.pyplot as plt
  2625. >>> import matplotlib.widgets as mwidgets
  2626. >>> fig, ax = plt.subplots()
  2627. >>> ax.plot([1, 2, 3], [10, 50, 100])
  2628. >>> def onselect(eclick, erelease):
  2629. ... print(eclick.xdata, eclick.ydata)
  2630. ... print(erelease.xdata, erelease.ydata)
  2631. >>> props = dict(facecolor='blue', alpha=0.5)
  2632. >>> rect = mwidgets.RectangleSelector(ax, onselect, interactive=True,
  2633. ... props=props)
  2634. >>> fig.show()
  2635. >>> rect.add_state('square')
  2636. See also: :doc:`/gallery/widgets/rectangle_selector`
  2637. """
  2638. def __init__(self, ax, onselect=None, *, minspanx=0,
  2639. minspany=0, useblit=False,
  2640. props=None, spancoords='data', button=None, grab_range=10,
  2641. handle_props=None, interactive=False,
  2642. state_modifier_keys=None, drag_from_anywhere=False,
  2643. ignore_event_outside=False, use_data_coordinates=False):
  2644. super().__init__(ax, onselect, useblit=useblit, button=button,
  2645. state_modifier_keys=state_modifier_keys,
  2646. use_data_coordinates=use_data_coordinates)
  2647. self._interactive = interactive
  2648. self.drag_from_anywhere = drag_from_anywhere
  2649. self.ignore_event_outside = ignore_event_outside
  2650. self._rotation = 0.0
  2651. self._aspect_ratio_correction = 1.0
  2652. # State to allow the option of an interactive selector that can't be
  2653. # interactively drawn. This is used in PolygonSelector as an
  2654. # interactive bounding box to allow the polygon to be easily resized
  2655. self._allow_creation = True
  2656. if props is None:
  2657. props = dict(facecolor='red', edgecolor='black',
  2658. alpha=0.2, fill=True)
  2659. props = {**props, 'animated': self.useblit}
  2660. self._visible = props.pop('visible', self._visible)
  2661. to_draw = self._init_shape(**props)
  2662. self.ax.add_patch(to_draw)
  2663. self._selection_artist = to_draw
  2664. self._set_aspect_ratio_correction()
  2665. self.minspanx = minspanx
  2666. self.minspany = minspany
  2667. _api.check_in_list(['data', 'pixels'], spancoords=spancoords)
  2668. self.spancoords = spancoords
  2669. self.grab_range = grab_range
  2670. if self._interactive:
  2671. self._handle_props = {
  2672. 'markeredgecolor': (props or {}).get('edgecolor', 'black'),
  2673. **cbook.normalize_kwargs(handle_props, Line2D)}
  2674. self._corner_order = ['SW', 'SE', 'NE', 'NW']
  2675. xc, yc = self.corners
  2676. self._corner_handles = ToolHandles(self.ax, xc, yc,
  2677. marker_props=self._handle_props,
  2678. useblit=self.useblit)
  2679. self._edge_order = ['W', 'S', 'E', 'N']
  2680. xe, ye = self.edge_centers
  2681. self._edge_handles = ToolHandles(self.ax, xe, ye, marker='s',
  2682. marker_props=self._handle_props,
  2683. useblit=self.useblit)
  2684. xc, yc = self.center
  2685. self._center_handle = ToolHandles(self.ax, [xc], [yc], marker='s',
  2686. marker_props=self._handle_props,
  2687. useblit=self.useblit)
  2688. self._active_handle = None
  2689. self._extents_on_press = None
  2690. @property
  2691. def _handles_artists(self):
  2692. return (*self._center_handle.artists, *self._corner_handles.artists,
  2693. *self._edge_handles.artists)
  2694. def _init_shape(self, **props):
  2695. return Rectangle((0, 0), 0, 1, visible=False,
  2696. rotation_point='center', **props)
  2697. def _press(self, event):
  2698. """Button press event handler."""
  2699. # make the drawn box/line visible get the click-coordinates, button, ...
  2700. if self._interactive and self._selection_artist.get_visible():
  2701. self._set_active_handle(event)
  2702. else:
  2703. self._active_handle = None
  2704. if ((self._active_handle is None or not self._interactive) and
  2705. self._allow_creation):
  2706. # Clear previous rectangle before drawing new rectangle.
  2707. self.update()
  2708. if (self._active_handle is None and not self.ignore_event_outside and
  2709. self._allow_creation):
  2710. x, y = self._get_data_coords(event)
  2711. self._visible = False
  2712. self.extents = x, x, y, y
  2713. self._visible = True
  2714. else:
  2715. self.set_visible(True)
  2716. self._extents_on_press = self.extents
  2717. self._rotation_on_press = self._rotation
  2718. self._set_aspect_ratio_correction()
  2719. return False
  2720. def _release(self, event):
  2721. """Button release event handler."""
  2722. if not self._interactive:
  2723. self._selection_artist.set_visible(False)
  2724. if (self._active_handle is None and self._selection_completed and
  2725. self.ignore_event_outside):
  2726. return
  2727. # update the eventpress and eventrelease with the resulting extents
  2728. x0, x1, y0, y1 = self.extents
  2729. self._eventpress.xdata = x0
  2730. self._eventpress.ydata = y0
  2731. xy0 = self.ax.transData.transform([x0, y0])
  2732. self._eventpress.x, self._eventpress.y = xy0
  2733. self._eventrelease.xdata = x1
  2734. self._eventrelease.ydata = y1
  2735. xy1 = self.ax.transData.transform([x1, y1])
  2736. self._eventrelease.x, self._eventrelease.y = xy1
  2737. # calculate dimensions of box or line
  2738. if self.spancoords == 'data':
  2739. spanx = abs(self._eventpress.xdata - self._eventrelease.xdata)
  2740. spany = abs(self._eventpress.ydata - self._eventrelease.ydata)
  2741. elif self.spancoords == 'pixels':
  2742. spanx = abs(self._eventpress.x - self._eventrelease.x)
  2743. spany = abs(self._eventpress.y - self._eventrelease.y)
  2744. else:
  2745. _api.check_in_list(['data', 'pixels'],
  2746. spancoords=self.spancoords)
  2747. # check if drawn distance (if it exists) is not too small in
  2748. # either x or y-direction
  2749. if spanx <= self.minspanx or spany <= self.minspany:
  2750. if self._selection_completed:
  2751. # Call onselect, only when the selection is already existing
  2752. self.onselect(self._eventpress, self._eventrelease)
  2753. self._clear_without_update()
  2754. else:
  2755. self.onselect(self._eventpress, self._eventrelease)
  2756. self._selection_completed = True
  2757. self.update()
  2758. self._active_handle = None
  2759. self._extents_on_press = None
  2760. return False
  2761. def _onmove(self, event):
  2762. """
  2763. Motion notify event handler.
  2764. This can do one of four things:
  2765. - Translate
  2766. - Rotate
  2767. - Re-size
  2768. - Continue the creation of a new shape
  2769. """
  2770. eventpress = self._eventpress
  2771. # The calculations are done for rotation at zero: we apply inverse
  2772. # transformation to events except when we rotate and move
  2773. state = self._state
  2774. rotate = 'rotate' in state and self._active_handle in self._corner_order
  2775. move = self._active_handle == 'C'
  2776. resize = self._active_handle and not move
  2777. xdata, ydata = self._get_data_coords(event)
  2778. if resize:
  2779. inv_tr = self._get_rotation_transform().inverted()
  2780. xdata, ydata = inv_tr.transform([xdata, ydata])
  2781. eventpress.xdata, eventpress.ydata = inv_tr.transform(
  2782. (eventpress.xdata, eventpress.ydata))
  2783. dx = xdata - eventpress.xdata
  2784. dy = ydata - eventpress.ydata
  2785. # refmax is used when moving the corner handle with the square state
  2786. # and is the maximum between refx and refy
  2787. refmax = None
  2788. if self._use_data_coordinates:
  2789. refx, refy = dx, dy
  2790. else:
  2791. # Get dx/dy in display coordinates
  2792. refx = event.x - eventpress.x
  2793. refy = event.y - eventpress.y
  2794. x0, x1, y0, y1 = self._extents_on_press
  2795. # rotate an existing shape
  2796. if rotate:
  2797. # calculate angle abc
  2798. a = (eventpress.xdata, eventpress.ydata)
  2799. b = self.center
  2800. c = (xdata, ydata)
  2801. angle = (np.arctan2(c[1]-b[1], c[0]-b[0]) -
  2802. np.arctan2(a[1]-b[1], a[0]-b[0]))
  2803. self.rotation = np.rad2deg(self._rotation_on_press + angle)
  2804. elif resize:
  2805. size_on_press = [x1 - x0, y1 - y0]
  2806. center = (x0 + size_on_press[0] / 2, y0 + size_on_press[1] / 2)
  2807. # Keeping the center fixed
  2808. if 'center' in state:
  2809. # hh, hw are half-height and half-width
  2810. if 'square' in state:
  2811. # when using a corner, find which reference to use
  2812. if self._active_handle in self._corner_order:
  2813. refmax = max(refx, refy, key=abs)
  2814. if self._active_handle in ['E', 'W'] or refmax == refx:
  2815. hw = xdata - center[0]
  2816. hh = hw / self._aspect_ratio_correction
  2817. else:
  2818. hh = ydata - center[1]
  2819. hw = hh * self._aspect_ratio_correction
  2820. else:
  2821. hw = size_on_press[0] / 2
  2822. hh = size_on_press[1] / 2
  2823. # cancel changes in perpendicular direction
  2824. if self._active_handle in ['E', 'W'] + self._corner_order:
  2825. hw = abs(xdata - center[0])
  2826. if self._active_handle in ['N', 'S'] + self._corner_order:
  2827. hh = abs(ydata - center[1])
  2828. x0, x1, y0, y1 = (center[0] - hw, center[0] + hw,
  2829. center[1] - hh, center[1] + hh)
  2830. else:
  2831. # change sign of relative changes to simplify calculation
  2832. # Switch variables so that x1 and/or y1 are updated on move
  2833. if 'W' in self._active_handle:
  2834. x0 = x1
  2835. if 'S' in self._active_handle:
  2836. y0 = y1
  2837. if self._active_handle in ['E', 'W'] + self._corner_order:
  2838. x1 = xdata
  2839. if self._active_handle in ['N', 'S'] + self._corner_order:
  2840. y1 = ydata
  2841. if 'square' in state:
  2842. # when using a corner, find which reference to use
  2843. if self._active_handle in self._corner_order:
  2844. refmax = max(refx, refy, key=abs)
  2845. if self._active_handle in ['E', 'W'] or refmax == refx:
  2846. sign = np.sign(ydata - y0)
  2847. y1 = y0 + sign * abs(x1 - x0) / self._aspect_ratio_correction
  2848. else:
  2849. sign = np.sign(xdata - x0)
  2850. x1 = x0 + sign * abs(y1 - y0) * self._aspect_ratio_correction
  2851. elif move:
  2852. x0, x1, y0, y1 = self._extents_on_press
  2853. dx = xdata - eventpress.xdata
  2854. dy = ydata - eventpress.ydata
  2855. x0 += dx
  2856. x1 += dx
  2857. y0 += dy
  2858. y1 += dy
  2859. else:
  2860. # Create a new shape
  2861. self._rotation = 0
  2862. # Don't create a new rectangle if there is already one when
  2863. # ignore_event_outside=True
  2864. if ((self.ignore_event_outside and self._selection_completed) or
  2865. not self._allow_creation):
  2866. return
  2867. center = [eventpress.xdata, eventpress.ydata]
  2868. dx = (xdata - center[0]) / 2
  2869. dy = (ydata - center[1]) / 2
  2870. # square shape
  2871. if 'square' in state:
  2872. refmax = max(refx, refy, key=abs)
  2873. if refmax == refx:
  2874. dy = np.sign(dy) * abs(dx) / self._aspect_ratio_correction
  2875. else:
  2876. dx = np.sign(dx) * abs(dy) * self._aspect_ratio_correction
  2877. # from center
  2878. if 'center' in state:
  2879. dx *= 2
  2880. dy *= 2
  2881. # from corner
  2882. else:
  2883. center[0] += dx
  2884. center[1] += dy
  2885. x0, x1, y0, y1 = (center[0] - dx, center[0] + dx,
  2886. center[1] - dy, center[1] + dy)
  2887. self.extents = x0, x1, y0, y1
  2888. @property
  2889. def _rect_bbox(self):
  2890. return self._selection_artist.get_bbox().bounds
  2891. def _set_aspect_ratio_correction(self):
  2892. aspect_ratio = self.ax._get_aspect_ratio()
  2893. self._selection_artist._aspect_ratio_correction = aspect_ratio
  2894. if self._use_data_coordinates:
  2895. self._aspect_ratio_correction = 1
  2896. else:
  2897. self._aspect_ratio_correction = aspect_ratio
  2898. def _get_rotation_transform(self):
  2899. aspect_ratio = self.ax._get_aspect_ratio()
  2900. return Affine2D().translate(-self.center[0], -self.center[1]) \
  2901. .scale(1, aspect_ratio) \
  2902. .rotate(self._rotation) \
  2903. .scale(1, 1 / aspect_ratio) \
  2904. .translate(*self.center)
  2905. @property
  2906. def corners(self):
  2907. """
  2908. Corners of rectangle in data coordinates from lower left,
  2909. moving clockwise.
  2910. """
  2911. x0, y0, width, height = self._rect_bbox
  2912. xc = x0, x0 + width, x0 + width, x0
  2913. yc = y0, y0, y0 + height, y0 + height
  2914. transform = self._get_rotation_transform()
  2915. coords = transform.transform(np.array([xc, yc]).T).T
  2916. return coords[0], coords[1]
  2917. @property
  2918. def edge_centers(self):
  2919. """
  2920. Midpoint of rectangle edges in data coordinates from left,
  2921. moving anti-clockwise.
  2922. """
  2923. x0, y0, width, height = self._rect_bbox
  2924. w = width / 2.
  2925. h = height / 2.
  2926. xe = x0, x0 + w, x0 + width, x0 + w
  2927. ye = y0 + h, y0, y0 + h, y0 + height
  2928. transform = self._get_rotation_transform()
  2929. coords = transform.transform(np.array([xe, ye]).T).T
  2930. return coords[0], coords[1]
  2931. @property
  2932. def center(self):
  2933. """Center of rectangle in data coordinates."""
  2934. x0, y0, width, height = self._rect_bbox
  2935. return x0 + width / 2., y0 + height / 2.
  2936. @property
  2937. def extents(self):
  2938. """
  2939. Return (xmin, xmax, ymin, ymax) in data coordinates as defined by the
  2940. bounding box before rotation.
  2941. """
  2942. x0, y0, width, height = self._rect_bbox
  2943. xmin, xmax = sorted([x0, x0 + width])
  2944. ymin, ymax = sorted([y0, y0 + height])
  2945. return xmin, xmax, ymin, ymax
  2946. @extents.setter
  2947. def extents(self, extents):
  2948. # Update displayed shape
  2949. self._draw_shape(extents)
  2950. if self._interactive:
  2951. # Update displayed handles
  2952. self._corner_handles.set_data(*self.corners)
  2953. self._edge_handles.set_data(*self.edge_centers)
  2954. x, y = self.center
  2955. self._center_handle.set_data([x], [y])
  2956. self.set_visible(self._visible)
  2957. self.update()
  2958. @property
  2959. def rotation(self):
  2960. """
  2961. Rotation in degree in interval [-45°, 45°]. The rotation is limited in
  2962. range to keep the implementation simple.
  2963. """
  2964. return np.rad2deg(self._rotation)
  2965. @rotation.setter
  2966. def rotation(self, value):
  2967. # Restrict to a limited range of rotation [-45°, 45°] to avoid changing
  2968. # order of handles
  2969. if -45 <= value and value <= 45:
  2970. self._rotation = np.deg2rad(value)
  2971. # call extents setter to draw shape and update handles positions
  2972. self.extents = self.extents
  2973. def _draw_shape(self, extents):
  2974. x0, x1, y0, y1 = extents
  2975. xmin, xmax = sorted([x0, x1])
  2976. ymin, ymax = sorted([y0, y1])
  2977. xlim = sorted(self.ax.get_xlim())
  2978. ylim = sorted(self.ax.get_ylim())
  2979. xmin = max(xlim[0], xmin)
  2980. ymin = max(ylim[0], ymin)
  2981. xmax = min(xmax, xlim[1])
  2982. ymax = min(ymax, ylim[1])
  2983. self._selection_artist.set_x(xmin)
  2984. self._selection_artist.set_y(ymin)
  2985. self._selection_artist.set_width(xmax - xmin)
  2986. self._selection_artist.set_height(ymax - ymin)
  2987. self._selection_artist.set_angle(self.rotation)
  2988. def _set_active_handle(self, event):
  2989. """Set active handle based on the location of the mouse event."""
  2990. # Note: event.xdata/ydata in data coordinates, event.x/y in pixels
  2991. c_idx, c_dist = self._corner_handles.closest(event.x, event.y)
  2992. e_idx, e_dist = self._edge_handles.closest(event.x, event.y)
  2993. m_idx, m_dist = self._center_handle.closest(event.x, event.y)
  2994. if 'move' in self._state:
  2995. self._active_handle = 'C'
  2996. # Set active handle as closest handle, if mouse click is close enough.
  2997. elif m_dist < self.grab_range * 2:
  2998. # Prioritise center handle over other handles
  2999. self._active_handle = 'C'
  3000. elif c_dist > self.grab_range and e_dist > self.grab_range:
  3001. # Not close to any handles
  3002. if self.drag_from_anywhere and self._contains(event):
  3003. # Check if we've clicked inside the region
  3004. self._active_handle = 'C'
  3005. else:
  3006. self._active_handle = None
  3007. return
  3008. elif c_dist < e_dist:
  3009. # Closest to a corner handle
  3010. self._active_handle = self._corner_order[c_idx]
  3011. else:
  3012. # Closest to an edge handle
  3013. self._active_handle = self._edge_order[e_idx]
  3014. def _contains(self, event):
  3015. """Return True if event is within the patch."""
  3016. return self._selection_artist.contains(event, radius=0)[0]
  3017. @property
  3018. def geometry(self):
  3019. """
  3020. Return an array of shape (2, 5) containing the
  3021. x (``RectangleSelector.geometry[1, :]``) and
  3022. y (``RectangleSelector.geometry[0, :]``) data coordinates of the four
  3023. corners of the rectangle starting and ending in the top left corner.
  3024. """
  3025. if hasattr(self._selection_artist, 'get_verts'):
  3026. xfm = self.ax.transData.inverted()
  3027. y, x = xfm.transform(self._selection_artist.get_verts()).T
  3028. return np.array([x, y])
  3029. else:
  3030. return np.array(self._selection_artist.get_data())
  3031. @_docstring.Substitution(_RECTANGLESELECTOR_PARAMETERS_DOCSTRING.replace(
  3032. '__ARTIST_NAME__', 'ellipse'))
  3033. class EllipseSelector(RectangleSelector):
  3034. """
  3035. Select an elliptical region of an Axes.
  3036. For the cursor to remain responsive you must keep a reference to it.
  3037. Press and release events triggered at the same coordinates outside the
  3038. selection will clear the selector, except when
  3039. ``ignore_event_outside=True``.
  3040. %s
  3041. Examples
  3042. --------
  3043. :doc:`/gallery/widgets/rectangle_selector`
  3044. """
  3045. def _init_shape(self, **props):
  3046. return Ellipse((0, 0), 0, 1, visible=False, **props)
  3047. def _draw_shape(self, extents):
  3048. x0, x1, y0, y1 = extents
  3049. xmin, xmax = sorted([x0, x1])
  3050. ymin, ymax = sorted([y0, y1])
  3051. center = [x0 + (x1 - x0) / 2., y0 + (y1 - y0) / 2.]
  3052. a = (xmax - xmin) / 2.
  3053. b = (ymax - ymin) / 2.
  3054. self._selection_artist.center = center
  3055. self._selection_artist.width = 2 * a
  3056. self._selection_artist.height = 2 * b
  3057. self._selection_artist.angle = self.rotation
  3058. @property
  3059. def _rect_bbox(self):
  3060. x, y = self._selection_artist.center
  3061. width = self._selection_artist.width
  3062. height = self._selection_artist.height
  3063. return x - width / 2., y - height / 2., width, height
  3064. class LassoSelector(_SelectorWidget):
  3065. """
  3066. Selection curve of an arbitrary shape.
  3067. For the selector to remain responsive you must keep a reference to it.
  3068. The selected path can be used in conjunction with `~.Path.contains_point`
  3069. to select data points from an image.
  3070. In contrast to `Lasso`, `LassoSelector` is written with an interface
  3071. similar to `RectangleSelector` and `SpanSelector`, and will continue to
  3072. interact with the Axes until disconnected.
  3073. Example usage::
  3074. ax = plt.subplot()
  3075. ax.plot(x, y)
  3076. def onselect(verts):
  3077. print(verts)
  3078. lasso = LassoSelector(ax, onselect)
  3079. Parameters
  3080. ----------
  3081. ax : `~matplotlib.axes.Axes`
  3082. The parent Axes for the widget.
  3083. onselect : function, optional
  3084. Whenever the lasso is released, the *onselect* function is called and
  3085. passed the vertices of the selected path.
  3086. useblit : bool, default: True
  3087. Whether to use blitting for faster drawing (if supported by the
  3088. backend). See the tutorial :ref:`blitting`
  3089. for details.
  3090. props : dict, optional
  3091. Properties with which the line is drawn, see `.Line2D`
  3092. for valid properties. Default values are defined in ``mpl.rcParams``.
  3093. button : `.MouseButton` or list of `.MouseButton`, optional
  3094. The mouse buttons used for rectangle selection. Default is ``None``,
  3095. which corresponds to all buttons.
  3096. """
  3097. def __init__(self, ax, onselect=None, *, useblit=True, props=None, button=None):
  3098. super().__init__(ax, onselect, useblit=useblit, button=button)
  3099. self.verts = None
  3100. props = {
  3101. **(props if props is not None else {}),
  3102. # Note that self.useblit may be != useblit, if the canvas doesn't
  3103. # support blitting.
  3104. 'animated': self.useblit, 'visible': False,
  3105. }
  3106. line = Line2D([], [], **props)
  3107. self.ax.add_line(line)
  3108. self._selection_artist = line
  3109. def _press(self, event):
  3110. self.verts = [self._get_data(event)]
  3111. self._selection_artist.set_visible(True)
  3112. def _release(self, event):
  3113. if self.verts is not None:
  3114. self.verts.append(self._get_data(event))
  3115. self.onselect(self.verts)
  3116. self._selection_artist.set_data([[], []])
  3117. self._selection_artist.set_visible(False)
  3118. self.verts = None
  3119. def _onmove(self, event):
  3120. if self.verts is None:
  3121. return
  3122. self.verts.append(self._get_data(event))
  3123. self._selection_artist.set_data(list(zip(*self.verts)))
  3124. self.update()
  3125. class PolygonSelector(_SelectorWidget):
  3126. """
  3127. Select a polygon region of an Axes.
  3128. Place vertices with each mouse click, and make the selection by completing
  3129. the polygon (clicking on the first vertex). Once drawn individual vertices
  3130. can be moved by clicking and dragging with the left mouse button, or
  3131. removed by clicking the right mouse button.
  3132. In addition, the following modifier keys can be used:
  3133. - Hold *ctrl* and click and drag a vertex to reposition it before the
  3134. polygon has been completed.
  3135. - Hold the *shift* key and click and drag anywhere in the Axes to move
  3136. all vertices.
  3137. - Press the *esc* key to start a new polygon.
  3138. For the selector to remain responsive you must keep a reference to it.
  3139. Parameters
  3140. ----------
  3141. ax : `~matplotlib.axes.Axes`
  3142. The parent Axes for the widget.
  3143. onselect : function, optional
  3144. When a polygon is completed or modified after completion,
  3145. the *onselect* function is called and passed a list of the vertices as
  3146. ``(xdata, ydata)`` tuples.
  3147. useblit : bool, default: False
  3148. Whether to use blitting for faster drawing (if supported by the
  3149. backend). See the tutorial :ref:`blitting`
  3150. for details.
  3151. props : dict, optional
  3152. Properties with which the line is drawn, see `.Line2D` for valid properties.
  3153. Default::
  3154. dict(color='k', linestyle='-', linewidth=2, alpha=0.5)
  3155. handle_props : dict, optional
  3156. Artist properties for the markers drawn at the vertices of the polygon.
  3157. See the marker arguments in `.Line2D` for valid
  3158. properties. Default values are defined in ``mpl.rcParams`` except for
  3159. the default value of ``markeredgecolor`` which will be the same as the
  3160. ``color`` property in *props*.
  3161. grab_range : float, default: 10
  3162. A vertex is selected (to complete the polygon or to move a vertex) if
  3163. the mouse click is within *grab_range* pixels of the vertex.
  3164. draw_bounding_box : bool, optional
  3165. If `True`, a bounding box will be drawn around the polygon selector
  3166. once it is complete. This box can be used to move and resize the
  3167. selector.
  3168. box_handle_props : dict, optional
  3169. Properties to set for the box handles. See the documentation for the
  3170. *handle_props* argument to `RectangleSelector` for more info.
  3171. box_props : dict, optional
  3172. Properties to set for the box. See the documentation for the *props*
  3173. argument to `RectangleSelector` for more info.
  3174. Examples
  3175. --------
  3176. :doc:`/gallery/widgets/polygon_selector_simple`
  3177. :doc:`/gallery/widgets/polygon_selector_demo`
  3178. Notes
  3179. -----
  3180. If only one point remains after removing points, the selector reverts to an
  3181. incomplete state and you can start drawing a new polygon from the existing
  3182. point.
  3183. """
  3184. def __init__(self, ax, onselect=None, *, useblit=False,
  3185. props=None, handle_props=None, grab_range=10,
  3186. draw_bounding_box=False, box_handle_props=None,
  3187. box_props=None):
  3188. # The state modifiers 'move', 'square', and 'center' are expected by
  3189. # _SelectorWidget but are not supported by PolygonSelector
  3190. # Note: could not use the existing 'move' state modifier in-place of
  3191. # 'move_all' because _SelectorWidget automatically discards 'move'
  3192. # from the state on button release.
  3193. state_modifier_keys = dict(clear='escape', move_vertex='control',
  3194. move_all='shift', move='not-applicable',
  3195. square='not-applicable',
  3196. center='not-applicable',
  3197. rotate='not-applicable')
  3198. super().__init__(ax, onselect, useblit=useblit,
  3199. state_modifier_keys=state_modifier_keys)
  3200. self._xys = [(0, 0)]
  3201. if props is None:
  3202. props = dict(color='k', linestyle='-', linewidth=2, alpha=0.5)
  3203. props = {**props, 'animated': self.useblit}
  3204. self._selection_artist = line = Line2D([], [], **props)
  3205. self.ax.add_line(line)
  3206. if handle_props is None:
  3207. handle_props = dict(markeredgecolor='k',
  3208. markerfacecolor=props.get('color', 'k'))
  3209. self._handle_props = handle_props
  3210. self._polygon_handles = ToolHandles(self.ax, [], [],
  3211. useblit=self.useblit,
  3212. marker_props=self._handle_props)
  3213. self._active_handle_idx = -1
  3214. self.grab_range = grab_range
  3215. self.set_visible(True)
  3216. self._draw_box = draw_bounding_box
  3217. self._box = None
  3218. if box_handle_props is None:
  3219. box_handle_props = {}
  3220. self._box_handle_props = self._handle_props.update(box_handle_props)
  3221. self._box_props = box_props
  3222. def _get_bbox(self):
  3223. return self._selection_artist.get_bbox()
  3224. def _add_box(self):
  3225. self._box = RectangleSelector(self.ax,
  3226. useblit=self.useblit,
  3227. grab_range=self.grab_range,
  3228. handle_props=self._box_handle_props,
  3229. props=self._box_props,
  3230. interactive=True)
  3231. self._box._state_modifier_keys.pop('rotate')
  3232. self._box.connect_event('motion_notify_event', self._scale_polygon)
  3233. self._update_box()
  3234. # Set state that prevents the RectangleSelector from being created
  3235. # by the user
  3236. self._box._allow_creation = False
  3237. self._box._selection_completed = True
  3238. self._draw_polygon()
  3239. def _remove_box(self):
  3240. if self._box is not None:
  3241. self._box.set_visible(False)
  3242. self._box = None
  3243. def _update_box(self):
  3244. # Update selection box extents to the extents of the polygon
  3245. if self._box is not None:
  3246. bbox = self._get_bbox()
  3247. self._box.extents = [bbox.x0, bbox.x1, bbox.y0, bbox.y1]
  3248. # Save a copy
  3249. self._old_box_extents = self._box.extents
  3250. def _scale_polygon(self, event):
  3251. """
  3252. Scale the polygon selector points when the bounding box is moved or
  3253. scaled.
  3254. This is set as a callback on the bounding box RectangleSelector.
  3255. """
  3256. if not self._selection_completed:
  3257. return
  3258. if self._old_box_extents == self._box.extents:
  3259. return
  3260. # Create transform from old box to new box
  3261. x1, y1, w1, h1 = self._box._rect_bbox
  3262. old_bbox = self._get_bbox()
  3263. t = (transforms.Affine2D()
  3264. .translate(-old_bbox.x0, -old_bbox.y0)
  3265. .scale(1 / old_bbox.width, 1 / old_bbox.height)
  3266. .scale(w1, h1)
  3267. .translate(x1, y1))
  3268. # Update polygon verts. Must be a list of tuples for consistency.
  3269. new_verts = [(x, y) for x, y in t.transform(np.array(self.verts))]
  3270. self._xys = [*new_verts, new_verts[0]]
  3271. self._draw_polygon()
  3272. self._old_box_extents = self._box.extents
  3273. @property
  3274. def _handles_artists(self):
  3275. return self._polygon_handles.artists
  3276. def _remove_vertex(self, i):
  3277. """Remove vertex with index i."""
  3278. if (len(self._xys) > 2 and
  3279. self._selection_completed and
  3280. i in (0, len(self._xys) - 1)):
  3281. # If selecting the first or final vertex, remove both first and
  3282. # last vertex as they are the same for a closed polygon
  3283. self._xys.pop(0)
  3284. self._xys.pop(-1)
  3285. # Close the polygon again by appending the new first vertex to the
  3286. # end
  3287. self._xys.append(self._xys[0])
  3288. else:
  3289. self._xys.pop(i)
  3290. if len(self._xys) <= 2:
  3291. # If only one point left, return to incomplete state to let user
  3292. # start drawing again
  3293. self._selection_completed = False
  3294. self._remove_box()
  3295. def _press(self, event):
  3296. """Button press event handler."""
  3297. # Check for selection of a tool handle.
  3298. if ((self._selection_completed or 'move_vertex' in self._state)
  3299. and len(self._xys) > 0):
  3300. h_idx, h_dist = self._polygon_handles.closest(event.x, event.y)
  3301. if h_dist < self.grab_range:
  3302. self._active_handle_idx = h_idx
  3303. # Save the vertex positions at the time of the press event (needed to
  3304. # support the 'move_all' state modifier).
  3305. self._xys_at_press = self._xys.copy()
  3306. def _release(self, event):
  3307. """Button release event handler."""
  3308. # Release active tool handle.
  3309. if self._active_handle_idx >= 0:
  3310. if event.button == 3:
  3311. self._remove_vertex(self._active_handle_idx)
  3312. self._draw_polygon()
  3313. self._active_handle_idx = -1
  3314. # Complete the polygon.
  3315. elif len(self._xys) > 3 and self._xys[-1] == self._xys[0]:
  3316. self._selection_completed = True
  3317. if self._draw_box and self._box is None:
  3318. self._add_box()
  3319. # Place new vertex.
  3320. elif (not self._selection_completed
  3321. and 'move_all' not in self._state
  3322. and 'move_vertex' not in self._state):
  3323. self._xys.insert(-1, self._get_data_coords(event))
  3324. if self._selection_completed:
  3325. self.onselect(self.verts)
  3326. def onmove(self, event):
  3327. """Cursor move event handler and validator."""
  3328. # Method overrides _SelectorWidget.onmove because the polygon selector
  3329. # needs to process the move callback even if there is no button press.
  3330. # _SelectorWidget.onmove include logic to ignore move event if
  3331. # _eventpress is None.
  3332. if self.ignore(event):
  3333. # Hide the cursor when interactive zoom/pan is active
  3334. if not self.canvas.widgetlock.available(self) and self._xys:
  3335. self._xys[-1] = (np.nan, np.nan)
  3336. self._draw_polygon()
  3337. return False
  3338. else:
  3339. event = self._clean_event(event)
  3340. self._onmove(event)
  3341. return True
  3342. def _onmove(self, event):
  3343. """Cursor move event handler."""
  3344. # Move the active vertex (ToolHandle).
  3345. if self._active_handle_idx >= 0:
  3346. idx = self._active_handle_idx
  3347. self._xys[idx] = self._get_data_coords(event)
  3348. # Also update the end of the polygon line if the first vertex is
  3349. # the active handle and the polygon is completed.
  3350. if idx == 0 and self._selection_completed:
  3351. self._xys[-1] = self._get_data_coords(event)
  3352. # Move all vertices.
  3353. elif 'move_all' in self._state and self._eventpress:
  3354. xdata, ydata = self._get_data_coords(event)
  3355. dx = xdata - self._eventpress.xdata
  3356. dy = ydata - self._eventpress.ydata
  3357. for k in range(len(self._xys)):
  3358. x_at_press, y_at_press = self._xys_at_press[k]
  3359. self._xys[k] = x_at_press + dx, y_at_press + dy
  3360. # Do nothing if completed or waiting for a move.
  3361. elif (self._selection_completed
  3362. or 'move_vertex' in self._state or 'move_all' in self._state):
  3363. return
  3364. # Position pending vertex.
  3365. else:
  3366. # Calculate distance to the start vertex.
  3367. x0, y0 = \
  3368. self._selection_artist.get_transform().transform(self._xys[0])
  3369. v0_dist = np.hypot(x0 - event.x, y0 - event.y)
  3370. # Lock on to the start vertex if near it and ready to complete.
  3371. if len(self._xys) > 3 and v0_dist < self.grab_range:
  3372. self._xys[-1] = self._xys[0]
  3373. else:
  3374. self._xys[-1] = self._get_data_coords(event)
  3375. self._draw_polygon()
  3376. def _on_key_press(self, event):
  3377. """Key press event handler."""
  3378. # Remove the pending vertex if entering the 'move_vertex' or
  3379. # 'move_all' mode
  3380. if (not self._selection_completed
  3381. and ('move_vertex' in self._state or
  3382. 'move_all' in self._state)):
  3383. self._xys.pop()
  3384. self._draw_polygon()
  3385. def _on_key_release(self, event):
  3386. """Key release event handler."""
  3387. # Add back the pending vertex if leaving the 'move_vertex' or
  3388. # 'move_all' mode (by checking the released key)
  3389. if (not self._selection_completed
  3390. and
  3391. (event.key == self._state_modifier_keys.get('move_vertex')
  3392. or event.key == self._state_modifier_keys.get('move_all'))):
  3393. self._xys.append(self._get_data_coords(event))
  3394. self._draw_polygon()
  3395. # Reset the polygon if the released key is the 'clear' key.
  3396. elif event.key == self._state_modifier_keys.get('clear'):
  3397. event = self._clean_event(event)
  3398. self._xys = [self._get_data_coords(event)]
  3399. self._selection_completed = False
  3400. self._remove_box()
  3401. self.set_visible(True)
  3402. def _draw_polygon_without_update(self):
  3403. """Redraw the polygon based on new vertex positions, no update()."""
  3404. xs, ys = zip(*self._xys) if self._xys else ([], [])
  3405. self._selection_artist.set_data(xs, ys)
  3406. self._update_box()
  3407. # Only show one tool handle at the start and end vertex of the polygon
  3408. # if the polygon is completed or the user is locked on to the start
  3409. # vertex.
  3410. if (self._selection_completed
  3411. or (len(self._xys) > 3
  3412. and self._xys[-1] == self._xys[0])):
  3413. self._polygon_handles.set_data(xs[:-1], ys[:-1])
  3414. else:
  3415. self._polygon_handles.set_data(xs, ys)
  3416. def _draw_polygon(self):
  3417. """Redraw the polygon based on the new vertex positions."""
  3418. self._draw_polygon_without_update()
  3419. self.update()
  3420. @property
  3421. def verts(self):
  3422. """The polygon vertices, as a list of ``(x, y)`` pairs."""
  3423. return self._xys[:-1]
  3424. @verts.setter
  3425. def verts(self, xys):
  3426. """
  3427. Set the polygon vertices.
  3428. This will remove any preexisting vertices, creating a complete polygon
  3429. with the new vertices.
  3430. """
  3431. self._xys = [*xys, xys[0]]
  3432. self._selection_completed = True
  3433. self.set_visible(True)
  3434. if self._draw_box and self._box is None:
  3435. self._add_box()
  3436. self._draw_polygon()
  3437. def _clear_without_update(self):
  3438. self._selection_completed = False
  3439. self._xys = [(0, 0)]
  3440. self._draw_polygon_without_update()
  3441. class Lasso(AxesWidget):
  3442. """
  3443. Selection curve of an arbitrary shape.
  3444. The selected path can be used in conjunction with
  3445. `~matplotlib.path.Path.contains_point` to select data points from an image.
  3446. Unlike `LassoSelector`, this must be initialized with a starting
  3447. point *xy*, and the `Lasso` events are destroyed upon release.
  3448. Parameters
  3449. ----------
  3450. ax : `~matplotlib.axes.Axes`
  3451. The parent Axes for the widget.
  3452. xy : (float, float)
  3453. Coordinates of the start of the lasso.
  3454. callback : callable
  3455. Whenever the lasso is released, the *callback* function is called and
  3456. passed the vertices of the selected path.
  3457. useblit : bool, default: True
  3458. Whether to use blitting for faster drawing (if supported by the
  3459. backend). See the tutorial :ref:`blitting`
  3460. for details.
  3461. props: dict, optional
  3462. Lasso line properties. See `.Line2D` for valid properties.
  3463. Default *props* are::
  3464. {'linestyle' : '-', 'color' : 'black', 'lw' : 2}
  3465. .. versionadded:: 3.9
  3466. """
  3467. def __init__(self, ax, xy, callback, *, useblit=True, props=None):
  3468. super().__init__(ax)
  3469. self.useblit = useblit and self.canvas.supports_blit
  3470. if self.useblit:
  3471. self.background = self.canvas.copy_from_bbox(self.ax.bbox)
  3472. style = {'linestyle': '-', 'color': 'black', 'lw': 2}
  3473. if props is not None:
  3474. style.update(props)
  3475. x, y = xy
  3476. self.verts = [(x, y)]
  3477. self.line = Line2D([x], [y], **style)
  3478. self.ax.add_line(self.line)
  3479. self.callback = callback
  3480. self.connect_event('button_release_event', self.onrelease)
  3481. self.connect_event('motion_notify_event', self.onmove)
  3482. def onrelease(self, event):
  3483. if self.ignore(event):
  3484. return
  3485. if self.verts is not None:
  3486. self.verts.append(self._get_data_coords(event))
  3487. if len(self.verts) > 2:
  3488. self.callback(self.verts)
  3489. self.line.remove()
  3490. self.verts = None
  3491. self.disconnect_events()
  3492. def onmove(self, event):
  3493. if (self.ignore(event)
  3494. or self.verts is None
  3495. or event.button != 1
  3496. or not self.ax.contains(event)[0]):
  3497. return
  3498. self.verts.append(self._get_data_coords(event))
  3499. self.line.set_data(list(zip(*self.verts)))
  3500. if self.useblit:
  3501. self.canvas.restore_region(self.background)
  3502. self.ax.draw_artist(self.line)
  3503. self.canvas.blit(self.ax.bbox)
  3504. else:
  3505. self.canvas.draw_idle()