cbook.py 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419
  1. """
  2. A collection of utility functions and classes. Originally, many
  3. (but not all) were from the Python Cookbook -- hence the name cbook.
  4. """
  5. import collections
  6. import collections.abc
  7. import contextlib
  8. import functools
  9. import gzip
  10. import itertools
  11. import math
  12. import operator
  13. import os
  14. from pathlib import Path
  15. import shlex
  16. import subprocess
  17. import sys
  18. import time
  19. import traceback
  20. import types
  21. import weakref
  22. import numpy as np
  23. try:
  24. from numpy.exceptions import VisibleDeprecationWarning # numpy >= 1.25
  25. except ImportError:
  26. from numpy import VisibleDeprecationWarning
  27. import matplotlib
  28. from matplotlib import _api, _c_internal_utils
  29. class _ExceptionInfo:
  30. """
  31. A class to carry exception information around.
  32. This is used to store and later raise exceptions. It's an alternative to
  33. directly storing Exception instances that circumvents traceback-related
  34. issues: caching tracebacks can keep user's objects in local namespaces
  35. alive indefinitely, which can lead to very surprising memory issues for
  36. users and result in incorrect tracebacks.
  37. """
  38. def __init__(self, cls, *args):
  39. self._cls = cls
  40. self._args = args
  41. @classmethod
  42. def from_exception(cls, exc):
  43. return cls(type(exc), *exc.args)
  44. def to_exception(self):
  45. return self._cls(*self._args)
  46. def _get_running_interactive_framework():
  47. """
  48. Return the interactive framework whose event loop is currently running, if
  49. any, or "headless" if no event loop can be started, or None.
  50. Returns
  51. -------
  52. Optional[str]
  53. One of the following values: "qt", "gtk3", "gtk4", "wx", "tk",
  54. "macosx", "headless", ``None``.
  55. """
  56. # Use ``sys.modules.get(name)`` rather than ``name in sys.modules`` as
  57. # entries can also have been explicitly set to None.
  58. QtWidgets = (
  59. sys.modules.get("PyQt6.QtWidgets")
  60. or sys.modules.get("PySide6.QtWidgets")
  61. or sys.modules.get("PyQt5.QtWidgets")
  62. or sys.modules.get("PySide2.QtWidgets")
  63. )
  64. if QtWidgets and QtWidgets.QApplication.instance():
  65. return "qt"
  66. Gtk = sys.modules.get("gi.repository.Gtk")
  67. if Gtk:
  68. if Gtk.MAJOR_VERSION == 4:
  69. from gi.repository import GLib
  70. if GLib.main_depth():
  71. return "gtk4"
  72. if Gtk.MAJOR_VERSION == 3 and Gtk.main_level():
  73. return "gtk3"
  74. wx = sys.modules.get("wx")
  75. if wx and wx.GetApp():
  76. return "wx"
  77. tkinter = sys.modules.get("tkinter")
  78. if tkinter:
  79. codes = {tkinter.mainloop.__code__, tkinter.Misc.mainloop.__code__}
  80. for frame in sys._current_frames().values():
  81. while frame:
  82. if frame.f_code in codes:
  83. return "tk"
  84. frame = frame.f_back
  85. # Preemptively break reference cycle between locals and the frame.
  86. del frame
  87. macosx = sys.modules.get("matplotlib.backends._macosx")
  88. if macosx and macosx.event_loop_is_running():
  89. return "macosx"
  90. if not _c_internal_utils.display_is_valid():
  91. return "headless"
  92. return None
  93. def _exception_printer(exc):
  94. if _get_running_interactive_framework() in ["headless", None]:
  95. raise exc
  96. else:
  97. traceback.print_exc()
  98. class _StrongRef:
  99. """
  100. Wrapper similar to a weakref, but keeping a strong reference to the object.
  101. """
  102. def __init__(self, obj):
  103. self._obj = obj
  104. def __call__(self):
  105. return self._obj
  106. def __eq__(self, other):
  107. return isinstance(other, _StrongRef) and self._obj == other._obj
  108. def __hash__(self):
  109. return hash(self._obj)
  110. def _weak_or_strong_ref(func, callback):
  111. """
  112. Return a `WeakMethod` wrapping *func* if possible, else a `_StrongRef`.
  113. """
  114. try:
  115. return weakref.WeakMethod(func, callback)
  116. except TypeError:
  117. return _StrongRef(func)
  118. class _UnhashDict:
  119. """
  120. A minimal dict-like class that also supports unhashable keys, storing them
  121. in a list of key-value pairs.
  122. This class only implements the interface needed for `CallbackRegistry`, and
  123. tries to minimize the overhead for the hashable case.
  124. """
  125. def __init__(self, pairs):
  126. self._dict = {}
  127. self._pairs = []
  128. for k, v in pairs:
  129. self[k] = v
  130. def __setitem__(self, key, value):
  131. try:
  132. self._dict[key] = value
  133. except TypeError:
  134. for i, (k, v) in enumerate(self._pairs):
  135. if k == key:
  136. self._pairs[i] = (key, value)
  137. break
  138. else:
  139. self._pairs.append((key, value))
  140. def __getitem__(self, key):
  141. try:
  142. return self._dict[key]
  143. except TypeError:
  144. pass
  145. for k, v in self._pairs:
  146. if k == key:
  147. return v
  148. raise KeyError(key)
  149. def pop(self, key, *args):
  150. try:
  151. if key in self._dict:
  152. return self._dict.pop(key)
  153. except TypeError:
  154. for i, (k, v) in enumerate(self._pairs):
  155. if k == key:
  156. del self._pairs[i]
  157. return v
  158. if args:
  159. return args[0]
  160. raise KeyError(key)
  161. def __iter__(self):
  162. yield from self._dict
  163. for k, v in self._pairs:
  164. yield k
  165. class CallbackRegistry:
  166. """
  167. Handle registering, processing, blocking, and disconnecting
  168. for a set of signals and callbacks:
  169. >>> def oneat(x):
  170. ... print('eat', x)
  171. >>> def ondrink(x):
  172. ... print('drink', x)
  173. >>> from matplotlib.cbook import CallbackRegistry
  174. >>> callbacks = CallbackRegistry()
  175. >>> id_eat = callbacks.connect('eat', oneat)
  176. >>> id_drink = callbacks.connect('drink', ondrink)
  177. >>> callbacks.process('drink', 123)
  178. drink 123
  179. >>> callbacks.process('eat', 456)
  180. eat 456
  181. >>> callbacks.process('be merry', 456) # nothing will be called
  182. >>> callbacks.disconnect(id_eat)
  183. >>> callbacks.process('eat', 456) # nothing will be called
  184. >>> with callbacks.blocked(signal='drink'):
  185. ... callbacks.process('drink', 123) # nothing will be called
  186. >>> callbacks.process('drink', 123)
  187. drink 123
  188. In practice, one should always disconnect all callbacks when they are
  189. no longer needed to avoid dangling references (and thus memory leaks).
  190. However, real code in Matplotlib rarely does so, and due to its design,
  191. it is rather difficult to place this kind of code. To get around this,
  192. and prevent this class of memory leaks, we instead store weak references
  193. to bound methods only, so when the destination object needs to die, the
  194. CallbackRegistry won't keep it alive.
  195. Parameters
  196. ----------
  197. exception_handler : callable, optional
  198. If not None, *exception_handler* must be a function that takes an
  199. `Exception` as single parameter. It gets called with any `Exception`
  200. raised by the callbacks during `CallbackRegistry.process`, and may
  201. either re-raise the exception or handle it in another manner.
  202. The default handler prints the exception (with `traceback.print_exc`) if
  203. an interactive event loop is running; it re-raises the exception if no
  204. interactive event loop is running.
  205. signals : list, optional
  206. If not None, *signals* is a list of signals that this registry handles:
  207. attempting to `process` or to `connect` to a signal not in the list
  208. throws a `ValueError`. The default, None, does not restrict the
  209. handled signals.
  210. """
  211. # We maintain two mappings:
  212. # callbacks: signal -> {cid -> weakref-to-callback}
  213. # _func_cid_map: {(signal, weakref-to-callback) -> cid}
  214. def __init__(self, exception_handler=_exception_printer, *, signals=None):
  215. self._signals = None if signals is None else list(signals) # Copy it.
  216. self.exception_handler = exception_handler
  217. self.callbacks = {}
  218. self._cid_gen = itertools.count()
  219. self._func_cid_map = _UnhashDict([])
  220. # A hidden variable that marks cids that need to be pickled.
  221. self._pickled_cids = set()
  222. def __getstate__(self):
  223. return {
  224. **vars(self),
  225. # In general, callbacks may not be pickled, so we just drop them,
  226. # unless directed otherwise by self._pickled_cids.
  227. "callbacks": {s: {cid: proxy() for cid, proxy in d.items()
  228. if cid in self._pickled_cids}
  229. for s, d in self.callbacks.items()},
  230. # It is simpler to reconstruct this from callbacks in __setstate__.
  231. "_func_cid_map": None,
  232. "_cid_gen": next(self._cid_gen)
  233. }
  234. def __setstate__(self, state):
  235. cid_count = state.pop('_cid_gen')
  236. vars(self).update(state)
  237. self.callbacks = {
  238. s: {cid: _weak_or_strong_ref(func, functools.partial(self._remove_proxy, s))
  239. for cid, func in d.items()}
  240. for s, d in self.callbacks.items()}
  241. self._func_cid_map = _UnhashDict(
  242. ((s, proxy), cid)
  243. for s, d in self.callbacks.items() for cid, proxy in d.items())
  244. self._cid_gen = itertools.count(cid_count)
  245. def connect(self, signal, func):
  246. """Register *func* to be called when signal *signal* is generated."""
  247. if self._signals is not None:
  248. _api.check_in_list(self._signals, signal=signal)
  249. proxy = _weak_or_strong_ref(func, functools.partial(self._remove_proxy, signal))
  250. try:
  251. return self._func_cid_map[signal, proxy]
  252. except KeyError:
  253. cid = self._func_cid_map[signal, proxy] = next(self._cid_gen)
  254. self.callbacks.setdefault(signal, {})[cid] = proxy
  255. return cid
  256. def _connect_picklable(self, signal, func):
  257. """
  258. Like `.connect`, but the callback is kept when pickling/unpickling.
  259. Currently internal-use only.
  260. """
  261. cid = self.connect(signal, func)
  262. self._pickled_cids.add(cid)
  263. return cid
  264. # Keep a reference to sys.is_finalizing, as sys may have been cleared out
  265. # at that point.
  266. def _remove_proxy(self, signal, proxy, *, _is_finalizing=sys.is_finalizing):
  267. if _is_finalizing():
  268. # Weakrefs can't be properly torn down at that point anymore.
  269. return
  270. cid = self._func_cid_map.pop((signal, proxy), None)
  271. if cid is not None:
  272. del self.callbacks[signal][cid]
  273. self._pickled_cids.discard(cid)
  274. else: # Not found
  275. return
  276. if len(self.callbacks[signal]) == 0: # Clean up empty dicts
  277. del self.callbacks[signal]
  278. def disconnect(self, cid):
  279. """
  280. Disconnect the callback registered with callback id *cid*.
  281. No error is raised if such a callback does not exist.
  282. """
  283. self._pickled_cids.discard(cid)
  284. for signal, proxy in self._func_cid_map:
  285. if self._func_cid_map[signal, proxy] == cid:
  286. break
  287. else: # Not found
  288. return
  289. assert self.callbacks[signal][cid] == proxy
  290. del self.callbacks[signal][cid]
  291. self._func_cid_map.pop((signal, proxy))
  292. if len(self.callbacks[signal]) == 0: # Clean up empty dicts
  293. del self.callbacks[signal]
  294. def process(self, s, *args, **kwargs):
  295. """
  296. Process signal *s*.
  297. All of the functions registered to receive callbacks on *s* will be
  298. called with ``*args`` and ``**kwargs``.
  299. """
  300. if self._signals is not None:
  301. _api.check_in_list(self._signals, signal=s)
  302. for ref in list(self.callbacks.get(s, {}).values()):
  303. func = ref()
  304. if func is not None:
  305. try:
  306. func(*args, **kwargs)
  307. # this does not capture KeyboardInterrupt, SystemExit,
  308. # and GeneratorExit
  309. except Exception as exc:
  310. if self.exception_handler is not None:
  311. self.exception_handler(exc)
  312. else:
  313. raise
  314. @contextlib.contextmanager
  315. def blocked(self, *, signal=None):
  316. """
  317. Block callback signals from being processed.
  318. A context manager to temporarily block/disable callback signals
  319. from being processed by the registered listeners.
  320. Parameters
  321. ----------
  322. signal : str, optional
  323. The callback signal to block. The default is to block all signals.
  324. """
  325. orig = self.callbacks
  326. try:
  327. if signal is None:
  328. # Empty out the callbacks
  329. self.callbacks = {}
  330. else:
  331. # Only remove the specific signal
  332. self.callbacks = {k: orig[k] for k in orig if k != signal}
  333. yield
  334. finally:
  335. self.callbacks = orig
  336. class silent_list(list):
  337. """
  338. A list with a short ``repr()``.
  339. This is meant to be used for a homogeneous list of artists, so that they
  340. don't cause long, meaningless output.
  341. Instead of ::
  342. [<matplotlib.lines.Line2D object at 0x7f5749fed3c8>,
  343. <matplotlib.lines.Line2D object at 0x7f5749fed4e0>,
  344. <matplotlib.lines.Line2D object at 0x7f5758016550>]
  345. one will get ::
  346. <a list of 3 Line2D objects>
  347. If ``self.type`` is None, the type name is obtained from the first item in
  348. the list (if any).
  349. """
  350. def __init__(self, type, seq=None):
  351. self.type = type
  352. if seq is not None:
  353. self.extend(seq)
  354. def __repr__(self):
  355. if self.type is not None or len(self) != 0:
  356. tp = self.type if self.type is not None else type(self[0]).__name__
  357. return f"<a list of {len(self)} {tp} objects>"
  358. else:
  359. return "<an empty list>"
  360. def _local_over_kwdict(
  361. local_var, kwargs, *keys,
  362. warning_cls=_api.MatplotlibDeprecationWarning):
  363. out = local_var
  364. for key in keys:
  365. kwarg_val = kwargs.pop(key, None)
  366. if kwarg_val is not None:
  367. if out is None:
  368. out = kwarg_val
  369. else:
  370. _api.warn_external(f'"{key}" keyword argument will be ignored',
  371. warning_cls)
  372. return out
  373. def strip_math(s):
  374. """
  375. Remove latex formatting from mathtext.
  376. Only handles fully math and fully non-math strings.
  377. """
  378. if len(s) >= 2 and s[0] == s[-1] == "$":
  379. s = s[1:-1]
  380. for tex, plain in [
  381. (r"\times", "x"), # Specifically for Formatter support.
  382. (r"\mathdefault", ""),
  383. (r"\rm", ""),
  384. (r"\cal", ""),
  385. (r"\tt", ""),
  386. (r"\it", ""),
  387. ("\\", ""),
  388. ("{", ""),
  389. ("}", ""),
  390. ]:
  391. s = s.replace(tex, plain)
  392. return s
  393. def _strip_comment(s):
  394. """Strip everything from the first unquoted #."""
  395. pos = 0
  396. while True:
  397. quote_pos = s.find('"', pos)
  398. hash_pos = s.find('#', pos)
  399. if quote_pos < 0:
  400. without_comment = s if hash_pos < 0 else s[:hash_pos]
  401. return without_comment.strip()
  402. elif 0 <= hash_pos < quote_pos:
  403. return s[:hash_pos].strip()
  404. else:
  405. closing_quote_pos = s.find('"', quote_pos + 1)
  406. if closing_quote_pos < 0:
  407. raise ValueError(
  408. f"Missing closing quote in: {s!r}. If you need a double-"
  409. 'quote inside a string, use escaping: e.g. "the \" char"')
  410. pos = closing_quote_pos + 1 # behind closing quote
  411. def is_writable_file_like(obj):
  412. """Return whether *obj* looks like a file object with a *write* method."""
  413. return callable(getattr(obj, 'write', None))
  414. def file_requires_unicode(x):
  415. """
  416. Return whether the given writable file-like object requires Unicode to be
  417. written to it.
  418. """
  419. try:
  420. x.write(b'')
  421. except TypeError:
  422. return True
  423. else:
  424. return False
  425. def to_filehandle(fname, flag='r', return_opened=False, encoding=None):
  426. """
  427. Convert a path to an open file handle or pass-through a file-like object.
  428. Consider using `open_file_cm` instead, as it allows one to properly close
  429. newly created file objects more easily.
  430. Parameters
  431. ----------
  432. fname : str or path-like or file-like
  433. If `str` or `os.PathLike`, the file is opened using the flags specified
  434. by *flag* and *encoding*. If a file-like object, it is passed through.
  435. flag : str, default: 'r'
  436. Passed as the *mode* argument to `open` when *fname* is `str` or
  437. `os.PathLike`; ignored if *fname* is file-like.
  438. return_opened : bool, default: False
  439. If True, return both the file object and a boolean indicating whether
  440. this was a new file (that the caller needs to close). If False, return
  441. only the new file.
  442. encoding : str or None, default: None
  443. Passed as the *mode* argument to `open` when *fname* is `str` or
  444. `os.PathLike`; ignored if *fname* is file-like.
  445. Returns
  446. -------
  447. fh : file-like
  448. opened : bool
  449. *opened* is only returned if *return_opened* is True.
  450. """
  451. if isinstance(fname, os.PathLike):
  452. fname = os.fspath(fname)
  453. if isinstance(fname, str):
  454. if fname.endswith('.gz'):
  455. fh = gzip.open(fname, flag)
  456. elif fname.endswith('.bz2'):
  457. # python may not be compiled with bz2 support,
  458. # bury import until we need it
  459. import bz2
  460. fh = bz2.BZ2File(fname, flag)
  461. else:
  462. fh = open(fname, flag, encoding=encoding)
  463. opened = True
  464. elif hasattr(fname, 'seek'):
  465. fh = fname
  466. opened = False
  467. else:
  468. raise ValueError('fname must be a PathLike or file handle')
  469. if return_opened:
  470. return fh, opened
  471. return fh
  472. def open_file_cm(path_or_file, mode="r", encoding=None):
  473. r"""Pass through file objects and context-manage path-likes."""
  474. fh, opened = to_filehandle(path_or_file, mode, True, encoding)
  475. return fh if opened else contextlib.nullcontext(fh)
  476. def is_scalar_or_string(val):
  477. """Return whether the given object is a scalar or string like."""
  478. return isinstance(val, str) or not np.iterable(val)
  479. def get_sample_data(fname, asfileobj=True):
  480. """
  481. Return a sample data file. *fname* is a path relative to the
  482. :file:`mpl-data/sample_data` directory. If *asfileobj* is `True`
  483. return a file object, otherwise just a file path.
  484. Sample data files are stored in the 'mpl-data/sample_data' directory within
  485. the Matplotlib package.
  486. If the filename ends in .gz, the file is implicitly ungzipped. If the
  487. filename ends with .npy or .npz, and *asfileobj* is `True`, the file is
  488. loaded with `numpy.load`.
  489. """
  490. path = _get_data_path('sample_data', fname)
  491. if asfileobj:
  492. suffix = path.suffix.lower()
  493. if suffix == '.gz':
  494. return gzip.open(path)
  495. elif suffix in ['.npy', '.npz']:
  496. return np.load(path)
  497. elif suffix in ['.csv', '.xrc', '.txt']:
  498. return path.open('r')
  499. else:
  500. return path.open('rb')
  501. else:
  502. return str(path)
  503. def _get_data_path(*args):
  504. """
  505. Return the `pathlib.Path` to a resource file provided by Matplotlib.
  506. ``*args`` specify a path relative to the base data path.
  507. """
  508. return Path(matplotlib.get_data_path(), *args)
  509. def flatten(seq, scalarp=is_scalar_or_string):
  510. """
  511. Return a generator of flattened nested containers.
  512. For example:
  513. >>> from matplotlib.cbook import flatten
  514. >>> l = (('John', ['Hunter']), (1, 23), [[([42, (5, 23)], )]])
  515. >>> print(list(flatten(l)))
  516. ['John', 'Hunter', 1, 23, 42, 5, 23]
  517. By: Composite of Holger Krekel and Luther Blissett
  518. From: https://code.activestate.com/recipes/121294-simple-generator-for-flattening-nested-containers/
  519. and Recipe 1.12 in cookbook
  520. """ # noqa: E501
  521. for item in seq:
  522. if scalarp(item) or item is None:
  523. yield item
  524. else:
  525. yield from flatten(item, scalarp)
  526. class _Stack:
  527. """
  528. Stack of elements with a movable cursor.
  529. Mimics home/back/forward in a web browser.
  530. """
  531. def __init__(self):
  532. self._pos = -1
  533. self._elements = []
  534. def clear(self):
  535. """Empty the stack."""
  536. self._pos = -1
  537. self._elements = []
  538. def __call__(self):
  539. """Return the current element, or None."""
  540. return self._elements[self._pos] if self._elements else None
  541. def __len__(self):
  542. return len(self._elements)
  543. def __getitem__(self, ind):
  544. return self._elements[ind]
  545. def forward(self):
  546. """Move the position forward and return the current element."""
  547. self._pos = min(self._pos + 1, len(self._elements) - 1)
  548. return self()
  549. def back(self):
  550. """Move the position back and return the current element."""
  551. self._pos = max(self._pos - 1, 0)
  552. return self()
  553. def push(self, o):
  554. """
  555. Push *o* to the stack after the current position, and return *o*.
  556. Discard all later elements.
  557. """
  558. self._elements[self._pos + 1:] = [o]
  559. self._pos = len(self._elements) - 1
  560. return o
  561. def home(self):
  562. """
  563. Push the first element onto the top of the stack.
  564. The first element is returned.
  565. """
  566. return self.push(self._elements[0]) if self._elements else None
  567. def safe_masked_invalid(x, copy=False):
  568. x = np.array(x, subok=True, copy=copy)
  569. if not x.dtype.isnative:
  570. # If we have already made a copy, do the byteswap in place, else make a
  571. # copy with the byte order swapped.
  572. # Swap to native order.
  573. x = x.byteswap(inplace=copy).view(x.dtype.newbyteorder('N'))
  574. try:
  575. xm = np.ma.masked_where(~(np.isfinite(x)), x, copy=False)
  576. except TypeError:
  577. return x
  578. return xm
  579. def print_cycles(objects, outstream=sys.stdout, show_progress=False):
  580. """
  581. Print loops of cyclic references in the given *objects*.
  582. It is often useful to pass in ``gc.garbage`` to find the cycles that are
  583. preventing some objects from being garbage collected.
  584. Parameters
  585. ----------
  586. objects
  587. A list of objects to find cycles in.
  588. outstream
  589. The stream for output.
  590. show_progress : bool
  591. If True, print the number of objects reached as they are found.
  592. """
  593. import gc
  594. def print_path(path):
  595. for i, step in enumerate(path):
  596. # next "wraps around"
  597. next = path[(i + 1) % len(path)]
  598. outstream.write(" %s -- " % type(step))
  599. if isinstance(step, dict):
  600. for key, val in step.items():
  601. if val is next:
  602. outstream.write(f"[{key!r}]")
  603. break
  604. if key is next:
  605. outstream.write(f"[key] = {val!r}")
  606. break
  607. elif isinstance(step, list):
  608. outstream.write("[%d]" % step.index(next))
  609. elif isinstance(step, tuple):
  610. outstream.write("( tuple )")
  611. else:
  612. outstream.write(repr(step))
  613. outstream.write(" ->\n")
  614. outstream.write("\n")
  615. def recurse(obj, start, all, current_path):
  616. if show_progress:
  617. outstream.write("%d\r" % len(all))
  618. all[id(obj)] = None
  619. referents = gc.get_referents(obj)
  620. for referent in referents:
  621. # If we've found our way back to the start, this is
  622. # a cycle, so print it out
  623. if referent is start:
  624. print_path(current_path)
  625. # Don't go back through the original list of objects, or
  626. # through temporary references to the object, since those
  627. # are just an artifact of the cycle detector itself.
  628. elif referent is objects or isinstance(referent, types.FrameType):
  629. continue
  630. # We haven't seen this object before, so recurse
  631. elif id(referent) not in all:
  632. recurse(referent, start, all, current_path + [obj])
  633. for obj in objects:
  634. outstream.write(f"Examining: {obj!r}\n")
  635. recurse(obj, obj, {}, [])
  636. class Grouper:
  637. """
  638. A disjoint-set data structure.
  639. Objects can be joined using :meth:`join`, tested for connectedness
  640. using :meth:`joined`, and all disjoint sets can be retrieved by
  641. using the object as an iterator.
  642. The objects being joined must be hashable and weak-referenceable.
  643. Examples
  644. --------
  645. >>> from matplotlib.cbook import Grouper
  646. >>> class Foo:
  647. ... def __init__(self, s):
  648. ... self.s = s
  649. ... def __repr__(self):
  650. ... return self.s
  651. ...
  652. >>> a, b, c, d, e, f = [Foo(x) for x in 'abcdef']
  653. >>> grp = Grouper()
  654. >>> grp.join(a, b)
  655. >>> grp.join(b, c)
  656. >>> grp.join(d, e)
  657. >>> list(grp)
  658. [[a, b, c], [d, e]]
  659. >>> grp.joined(a, b)
  660. True
  661. >>> grp.joined(a, c)
  662. True
  663. >>> grp.joined(a, d)
  664. False
  665. """
  666. def __init__(self, init=()):
  667. self._mapping = weakref.WeakKeyDictionary(
  668. {x: weakref.WeakSet([x]) for x in init})
  669. self._ordering = weakref.WeakKeyDictionary()
  670. for x in init:
  671. if x not in self._ordering:
  672. self._ordering[x] = len(self._ordering)
  673. self._next_order = len(self._ordering) # Plain int to simplify pickling.
  674. def __getstate__(self):
  675. return {
  676. **vars(self),
  677. # Convert weak refs to strong ones.
  678. "_mapping": {k: set(v) for k, v in self._mapping.items()},
  679. "_ordering": {**self._ordering},
  680. }
  681. def __setstate__(self, state):
  682. vars(self).update(state)
  683. # Convert strong refs to weak ones.
  684. self._mapping = weakref.WeakKeyDictionary(
  685. {k: weakref.WeakSet(v) for k, v in self._mapping.items()})
  686. self._ordering = weakref.WeakKeyDictionary(self._ordering)
  687. def __contains__(self, item):
  688. return item in self._mapping
  689. def join(self, a, *args):
  690. """
  691. Join given arguments into the same set. Accepts one or more arguments.
  692. """
  693. mapping = self._mapping
  694. try:
  695. set_a = mapping[a]
  696. except KeyError:
  697. set_a = mapping[a] = weakref.WeakSet([a])
  698. self._ordering[a] = self._next_order
  699. self._next_order += 1
  700. for arg in args:
  701. try:
  702. set_b = mapping[arg]
  703. except KeyError:
  704. set_b = mapping[arg] = weakref.WeakSet([arg])
  705. self._ordering[arg] = self._next_order
  706. self._next_order += 1
  707. if set_b is not set_a:
  708. if len(set_b) > len(set_a):
  709. set_a, set_b = set_b, set_a
  710. set_a.update(set_b)
  711. for elem in set_b:
  712. mapping[elem] = set_a
  713. def joined(self, a, b):
  714. """Return whether *a* and *b* are members of the same set."""
  715. return (self._mapping.get(a, object()) is self._mapping.get(b))
  716. def remove(self, a):
  717. """Remove *a* from the grouper, doing nothing if it is not there."""
  718. self._mapping.pop(a, {a}).remove(a)
  719. self._ordering.pop(a, None)
  720. def __iter__(self):
  721. """
  722. Iterate over each of the disjoint sets as a list.
  723. The iterator is invalid if interleaved with calls to join().
  724. """
  725. unique_groups = {id(group): group for group in self._mapping.values()}
  726. for group in unique_groups.values():
  727. yield sorted(group, key=self._ordering.__getitem__)
  728. def get_siblings(self, a):
  729. """Return all of the items joined with *a*, including itself."""
  730. siblings = self._mapping.get(a, [a])
  731. return sorted(siblings, key=self._ordering.get)
  732. class GrouperView:
  733. """Immutable view over a `.Grouper`."""
  734. def __init__(self, grouper): self._grouper = grouper
  735. def __contains__(self, item): return item in self._grouper
  736. def __iter__(self): return iter(self._grouper)
  737. def joined(self, a, b):
  738. """
  739. Return whether *a* and *b* are members of the same set.
  740. """
  741. return self._grouper.joined(a, b)
  742. def get_siblings(self, a):
  743. """
  744. Return all of the items joined with *a*, including itself.
  745. """
  746. return self._grouper.get_siblings(a)
  747. def simple_linear_interpolation(a, steps):
  748. """
  749. Resample an array with ``steps - 1`` points between original point pairs.
  750. Along each column of *a*, ``(steps - 1)`` points are introduced between
  751. each original values; the values are linearly interpolated.
  752. Parameters
  753. ----------
  754. a : array, shape (n, ...)
  755. steps : int
  756. Returns
  757. -------
  758. array
  759. shape ``((n - 1) * steps + 1, ...)``
  760. """
  761. fps = a.reshape((len(a), -1))
  762. xp = np.arange(len(a)) * steps
  763. x = np.arange((len(a) - 1) * steps + 1)
  764. return (np.column_stack([np.interp(x, xp, fp) for fp in fps.T])
  765. .reshape((len(x),) + a.shape[1:]))
  766. def delete_masked_points(*args):
  767. """
  768. Find all masked and/or non-finite points in a set of arguments,
  769. and return the arguments with only the unmasked points remaining.
  770. Arguments can be in any of 5 categories:
  771. 1) 1-D masked arrays
  772. 2) 1-D ndarrays
  773. 3) ndarrays with more than one dimension
  774. 4) other non-string iterables
  775. 5) anything else
  776. The first argument must be in one of the first four categories;
  777. any argument with a length differing from that of the first
  778. argument (and hence anything in category 5) then will be
  779. passed through unchanged.
  780. Masks are obtained from all arguments of the correct length
  781. in categories 1, 2, and 4; a point is bad if masked in a masked
  782. array or if it is a nan or inf. No attempt is made to
  783. extract a mask from categories 2, 3, and 4 if `numpy.isfinite`
  784. does not yield a Boolean array.
  785. All input arguments that are not passed unchanged are returned
  786. as ndarrays after removing the points or rows corresponding to
  787. masks in any of the arguments.
  788. A vastly simpler version of this function was originally
  789. written as a helper for Axes.scatter().
  790. """
  791. if not len(args):
  792. return ()
  793. if is_scalar_or_string(args[0]):
  794. raise ValueError("First argument must be a sequence")
  795. nrecs = len(args[0])
  796. margs = []
  797. seqlist = [False] * len(args)
  798. for i, x in enumerate(args):
  799. if not isinstance(x, str) and np.iterable(x) and len(x) == nrecs:
  800. seqlist[i] = True
  801. if isinstance(x, np.ma.MaskedArray):
  802. if x.ndim > 1:
  803. raise ValueError("Masked arrays must be 1-D")
  804. else:
  805. x = np.asarray(x)
  806. margs.append(x)
  807. masks = [] # List of masks that are True where good.
  808. for i, x in enumerate(margs):
  809. if seqlist[i]:
  810. if x.ndim > 1:
  811. continue # Don't try to get nan locations unless 1-D.
  812. if isinstance(x, np.ma.MaskedArray):
  813. masks.append(~np.ma.getmaskarray(x)) # invert the mask
  814. xd = x.data
  815. else:
  816. xd = x
  817. try:
  818. mask = np.isfinite(xd)
  819. if isinstance(mask, np.ndarray):
  820. masks.append(mask)
  821. except Exception: # Fixme: put in tuple of possible exceptions?
  822. pass
  823. if len(masks):
  824. mask = np.logical_and.reduce(masks)
  825. igood = mask.nonzero()[0]
  826. if len(igood) < nrecs:
  827. for i, x in enumerate(margs):
  828. if seqlist[i]:
  829. margs[i] = x[igood]
  830. for i, x in enumerate(margs):
  831. if seqlist[i] and isinstance(x, np.ma.MaskedArray):
  832. margs[i] = x.filled()
  833. return margs
  834. def _combine_masks(*args):
  835. """
  836. Find all masked and/or non-finite points in a set of arguments,
  837. and return the arguments as masked arrays with a common mask.
  838. Arguments can be in any of 5 categories:
  839. 1) 1-D masked arrays
  840. 2) 1-D ndarrays
  841. 3) ndarrays with more than one dimension
  842. 4) other non-string iterables
  843. 5) anything else
  844. The first argument must be in one of the first four categories;
  845. any argument with a length differing from that of the first
  846. argument (and hence anything in category 5) then will be
  847. passed through unchanged.
  848. Masks are obtained from all arguments of the correct length
  849. in categories 1, 2, and 4; a point is bad if masked in a masked
  850. array or if it is a nan or inf. No attempt is made to
  851. extract a mask from categories 2 and 4 if `numpy.isfinite`
  852. does not yield a Boolean array. Category 3 is included to
  853. support RGB or RGBA ndarrays, which are assumed to have only
  854. valid values and which are passed through unchanged.
  855. All input arguments that are not passed unchanged are returned
  856. as masked arrays if any masked points are found, otherwise as
  857. ndarrays.
  858. """
  859. if not len(args):
  860. return ()
  861. if is_scalar_or_string(args[0]):
  862. raise ValueError("First argument must be a sequence")
  863. nrecs = len(args[0])
  864. margs = [] # Output args; some may be modified.
  865. seqlist = [False] * len(args) # Flags: True if output will be masked.
  866. masks = [] # List of masks.
  867. for i, x in enumerate(args):
  868. if is_scalar_or_string(x) or len(x) != nrecs:
  869. margs.append(x) # Leave it unmodified.
  870. else:
  871. if isinstance(x, np.ma.MaskedArray) and x.ndim > 1:
  872. raise ValueError("Masked arrays must be 1-D")
  873. try:
  874. x = np.asanyarray(x)
  875. except (VisibleDeprecationWarning, ValueError):
  876. # NumPy 1.19 raises a warning about ragged arrays, but we want
  877. # to accept basically anything here.
  878. x = np.asanyarray(x, dtype=object)
  879. if x.ndim == 1:
  880. x = safe_masked_invalid(x)
  881. seqlist[i] = True
  882. if np.ma.is_masked(x):
  883. masks.append(np.ma.getmaskarray(x))
  884. margs.append(x) # Possibly modified.
  885. if len(masks):
  886. mask = np.logical_or.reduce(masks)
  887. for i, x in enumerate(margs):
  888. if seqlist[i]:
  889. margs[i] = np.ma.array(x, mask=mask)
  890. return margs
  891. def _broadcast_with_masks(*args, compress=False):
  892. """
  893. Broadcast inputs, combining all masked arrays.
  894. Parameters
  895. ----------
  896. *args : array-like
  897. The inputs to broadcast.
  898. compress : bool, default: False
  899. Whether to compress the masked arrays. If False, the masked values
  900. are replaced by NaNs.
  901. Returns
  902. -------
  903. list of array-like
  904. The broadcasted and masked inputs.
  905. """
  906. # extract the masks, if any
  907. masks = [k.mask for k in args if isinstance(k, np.ma.MaskedArray)]
  908. # broadcast to match the shape
  909. bcast = np.broadcast_arrays(*args, *masks)
  910. inputs = bcast[:len(args)]
  911. masks = bcast[len(args):]
  912. if masks:
  913. # combine the masks into one
  914. mask = np.logical_or.reduce(masks)
  915. # put mask on and compress
  916. if compress:
  917. inputs = [np.ma.array(k, mask=mask).compressed()
  918. for k in inputs]
  919. else:
  920. inputs = [np.ma.array(k, mask=mask, dtype=float).filled(np.nan).ravel()
  921. for k in inputs]
  922. else:
  923. inputs = [np.ravel(k) for k in inputs]
  924. return inputs
  925. def boxplot_stats(X, whis=1.5, bootstrap=None, labels=None, autorange=False):
  926. r"""
  927. Return a list of dictionaries of statistics used to draw a series of box
  928. and whisker plots using `~.Axes.bxp`.
  929. Parameters
  930. ----------
  931. X : array-like
  932. Data that will be represented in the boxplots. Should have 2 or
  933. fewer dimensions.
  934. whis : float or (float, float), default: 1.5
  935. The position of the whiskers.
  936. If a float, the lower whisker is at the lowest datum above
  937. ``Q1 - whis*(Q3-Q1)``, and the upper whisker at the highest datum below
  938. ``Q3 + whis*(Q3-Q1)``, where Q1 and Q3 are the first and third
  939. quartiles. The default value of ``whis = 1.5`` corresponds to Tukey's
  940. original definition of boxplots.
  941. If a pair of floats, they indicate the percentiles at which to draw the
  942. whiskers (e.g., (5, 95)). In particular, setting this to (0, 100)
  943. results in whiskers covering the whole range of the data.
  944. In the edge case where ``Q1 == Q3``, *whis* is automatically set to
  945. (0, 100) (cover the whole range of the data) if *autorange* is True.
  946. Beyond the whiskers, data are considered outliers and are plotted as
  947. individual points.
  948. bootstrap : int, optional
  949. Number of times the confidence intervals around the median
  950. should be bootstrapped (percentile method).
  951. labels : list of str, optional
  952. Labels for each dataset. Length must be compatible with
  953. dimensions of *X*.
  954. autorange : bool, optional (False)
  955. When `True` and the data are distributed such that the 25th and 75th
  956. percentiles are equal, ``whis`` is set to (0, 100) such that the
  957. whisker ends are at the minimum and maximum of the data.
  958. Returns
  959. -------
  960. list of dict
  961. A list of dictionaries containing the results for each column
  962. of data. Keys of each dictionary are the following:
  963. ======== ===================================
  964. Key Value Description
  965. ======== ===================================
  966. label tick label for the boxplot
  967. mean arithmetic mean value
  968. med 50th percentile
  969. q1 first quartile (25th percentile)
  970. q3 third quartile (75th percentile)
  971. iqr interquartile range
  972. cilo lower notch around the median
  973. cihi upper notch around the median
  974. whislo end of the lower whisker
  975. whishi end of the upper whisker
  976. fliers outliers
  977. ======== ===================================
  978. Notes
  979. -----
  980. Non-bootstrapping approach to confidence interval uses Gaussian-based
  981. asymptotic approximation:
  982. .. math::
  983. \mathrm{med} \pm 1.57 \times \frac{\mathrm{iqr}}{\sqrt{N}}
  984. General approach from:
  985. McGill, R., Tukey, J.W., and Larsen, W.A. (1978) "Variations of
  986. Boxplots", The American Statistician, 32:12-16.
  987. """
  988. def _bootstrap_median(data, N=5000):
  989. # determine 95% confidence intervals of the median
  990. M = len(data)
  991. percentiles = [2.5, 97.5]
  992. bs_index = np.random.randint(M, size=(N, M))
  993. bsData = data[bs_index]
  994. estimate = np.median(bsData, axis=1, overwrite_input=True)
  995. CI = np.percentile(estimate, percentiles)
  996. return CI
  997. def _compute_conf_interval(data, med, iqr, bootstrap):
  998. if bootstrap is not None:
  999. # Do a bootstrap estimate of notch locations.
  1000. # get conf. intervals around median
  1001. CI = _bootstrap_median(data, N=bootstrap)
  1002. notch_min = CI[0]
  1003. notch_max = CI[1]
  1004. else:
  1005. N = len(data)
  1006. notch_min = med - 1.57 * iqr / np.sqrt(N)
  1007. notch_max = med + 1.57 * iqr / np.sqrt(N)
  1008. return notch_min, notch_max
  1009. # output is a list of dicts
  1010. bxpstats = []
  1011. # convert X to a list of lists
  1012. X = _reshape_2D(X, "X")
  1013. ncols = len(X)
  1014. if labels is None:
  1015. labels = itertools.repeat(None)
  1016. elif len(labels) != ncols:
  1017. raise ValueError("Dimensions of labels and X must be compatible")
  1018. input_whis = whis
  1019. for ii, (x, label) in enumerate(zip(X, labels)):
  1020. # empty dict
  1021. stats = {}
  1022. if label is not None:
  1023. stats['label'] = label
  1024. # restore whis to the input values in case it got changed in the loop
  1025. whis = input_whis
  1026. # note tricksiness, append up here and then mutate below
  1027. bxpstats.append(stats)
  1028. # if empty, bail
  1029. if len(x) == 0:
  1030. stats['fliers'] = np.array([])
  1031. stats['mean'] = np.nan
  1032. stats['med'] = np.nan
  1033. stats['q1'] = np.nan
  1034. stats['q3'] = np.nan
  1035. stats['iqr'] = np.nan
  1036. stats['cilo'] = np.nan
  1037. stats['cihi'] = np.nan
  1038. stats['whislo'] = np.nan
  1039. stats['whishi'] = np.nan
  1040. continue
  1041. # up-convert to an array, just to be safe
  1042. x = np.ma.asarray(x)
  1043. x = x.data[~x.mask].ravel()
  1044. # arithmetic mean
  1045. stats['mean'] = np.mean(x)
  1046. # medians and quartiles
  1047. q1, med, q3 = np.percentile(x, [25, 50, 75])
  1048. # interquartile range
  1049. stats['iqr'] = q3 - q1
  1050. if stats['iqr'] == 0 and autorange:
  1051. whis = (0, 100)
  1052. # conf. interval around median
  1053. stats['cilo'], stats['cihi'] = _compute_conf_interval(
  1054. x, med, stats['iqr'], bootstrap
  1055. )
  1056. # lowest/highest non-outliers
  1057. if np.iterable(whis) and not isinstance(whis, str):
  1058. loval, hival = np.percentile(x, whis)
  1059. elif np.isreal(whis):
  1060. loval = q1 - whis * stats['iqr']
  1061. hival = q3 + whis * stats['iqr']
  1062. else:
  1063. raise ValueError('whis must be a float or list of percentiles')
  1064. # get high extreme
  1065. wiskhi = x[x <= hival]
  1066. if len(wiskhi) == 0 or np.max(wiskhi) < q3:
  1067. stats['whishi'] = q3
  1068. else:
  1069. stats['whishi'] = np.max(wiskhi)
  1070. # get low extreme
  1071. wisklo = x[x >= loval]
  1072. if len(wisklo) == 0 or np.min(wisklo) > q1:
  1073. stats['whislo'] = q1
  1074. else:
  1075. stats['whislo'] = np.min(wisklo)
  1076. # compute a single array of outliers
  1077. stats['fliers'] = np.concatenate([
  1078. x[x < stats['whislo']],
  1079. x[x > stats['whishi']],
  1080. ])
  1081. # add in the remaining stats
  1082. stats['q1'], stats['med'], stats['q3'] = q1, med, q3
  1083. return bxpstats
  1084. #: Maps short codes for line style to their full name used by backends.
  1085. ls_mapper = {'-': 'solid', '--': 'dashed', '-.': 'dashdot', ':': 'dotted'}
  1086. #: Maps full names for line styles used by backends to their short codes.
  1087. ls_mapper_r = {v: k for k, v in ls_mapper.items()}
  1088. def contiguous_regions(mask):
  1089. """
  1090. Return a list of (ind0, ind1) such that ``mask[ind0:ind1].all()`` is
  1091. True and we cover all such regions.
  1092. """
  1093. mask = np.asarray(mask, dtype=bool)
  1094. if not mask.size:
  1095. return []
  1096. # Find the indices of region changes, and correct offset
  1097. idx, = np.nonzero(mask[:-1] != mask[1:])
  1098. idx += 1
  1099. # List operations are faster for moderately sized arrays
  1100. idx = idx.tolist()
  1101. # Add first and/or last index if needed
  1102. if mask[0]:
  1103. idx = [0] + idx
  1104. if mask[-1]:
  1105. idx.append(len(mask))
  1106. return list(zip(idx[::2], idx[1::2]))
  1107. def is_math_text(s):
  1108. """
  1109. Return whether the string *s* contains math expressions.
  1110. This is done by checking whether *s* contains an even number of
  1111. non-escaped dollar signs.
  1112. """
  1113. s = str(s)
  1114. dollar_count = s.count(r'$') - s.count(r'\$')
  1115. even_dollars = (dollar_count > 0 and dollar_count % 2 == 0)
  1116. return even_dollars
  1117. def _to_unmasked_float_array(x):
  1118. """
  1119. Convert a sequence to a float array; if input was a masked array, masked
  1120. values are converted to nans.
  1121. """
  1122. if hasattr(x, 'mask'):
  1123. return np.ma.asarray(x, float).filled(np.nan)
  1124. else:
  1125. return np.asarray(x, float)
  1126. def _check_1d(x):
  1127. """Convert scalars to 1D arrays; pass-through arrays as is."""
  1128. # Unpack in case of e.g. Pandas or xarray object
  1129. x = _unpack_to_numpy(x)
  1130. # plot requires `shape` and `ndim`. If passed an
  1131. # object that doesn't provide them, then force to numpy array.
  1132. # Note this will strip unit information.
  1133. if (not hasattr(x, 'shape') or
  1134. not hasattr(x, 'ndim') or
  1135. len(x.shape) < 1):
  1136. return np.atleast_1d(x)
  1137. else:
  1138. return x
  1139. def _reshape_2D(X, name):
  1140. """
  1141. Use Fortran ordering to convert ndarrays and lists of iterables to lists of
  1142. 1D arrays.
  1143. Lists of iterables are converted by applying `numpy.asanyarray` to each of
  1144. their elements. 1D ndarrays are returned in a singleton list containing
  1145. them. 2D ndarrays are converted to the list of their *columns*.
  1146. *name* is used to generate the error message for invalid inputs.
  1147. """
  1148. # Unpack in case of e.g. Pandas or xarray object
  1149. X = _unpack_to_numpy(X)
  1150. # Iterate over columns for ndarrays.
  1151. if isinstance(X, np.ndarray):
  1152. X = X.transpose()
  1153. if len(X) == 0:
  1154. return [[]]
  1155. elif X.ndim == 1 and np.ndim(X[0]) == 0:
  1156. # 1D array of scalars: directly return it.
  1157. return [X]
  1158. elif X.ndim in [1, 2]:
  1159. # 2D array, or 1D array of iterables: flatten them first.
  1160. return [np.reshape(x, -1) for x in X]
  1161. else:
  1162. raise ValueError(f'{name} must have 2 or fewer dimensions')
  1163. # Iterate over list of iterables.
  1164. if len(X) == 0:
  1165. return [[]]
  1166. result = []
  1167. is_1d = True
  1168. for xi in X:
  1169. # check if this is iterable, except for strings which we
  1170. # treat as singletons.
  1171. if not isinstance(xi, str):
  1172. try:
  1173. iter(xi)
  1174. except TypeError:
  1175. pass
  1176. else:
  1177. is_1d = False
  1178. xi = np.asanyarray(xi)
  1179. nd = np.ndim(xi)
  1180. if nd > 1:
  1181. raise ValueError(f'{name} must have 2 or fewer dimensions')
  1182. result.append(xi.reshape(-1))
  1183. if is_1d:
  1184. # 1D array of scalars: directly return it.
  1185. return [np.reshape(result, -1)]
  1186. else:
  1187. # 2D array, or 1D array of iterables: use flattened version.
  1188. return result
  1189. def violin_stats(X, method, points=100, quantiles=None):
  1190. """
  1191. Return a list of dictionaries of data which can be used to draw a series
  1192. of violin plots.
  1193. See the ``Returns`` section below to view the required keys of the
  1194. dictionary.
  1195. Users can skip this function and pass a user-defined set of dictionaries
  1196. with the same keys to `~.axes.Axes.violinplot` instead of using Matplotlib
  1197. to do the calculations. See the *Returns* section below for the keys
  1198. that must be present in the dictionaries.
  1199. Parameters
  1200. ----------
  1201. X : array-like
  1202. Sample data that will be used to produce the gaussian kernel density
  1203. estimates. Must have 2 or fewer dimensions.
  1204. method : callable
  1205. The method used to calculate the kernel density estimate for each
  1206. column of data. When called via ``method(v, coords)``, it should
  1207. return a vector of the values of the KDE evaluated at the values
  1208. specified in coords.
  1209. points : int, default: 100
  1210. Defines the number of points to evaluate each of the gaussian kernel
  1211. density estimates at.
  1212. quantiles : array-like, default: None
  1213. Defines (if not None) a list of floats in interval [0, 1] for each
  1214. column of data, which represents the quantiles that will be rendered
  1215. for that column of data. Must have 2 or fewer dimensions. 1D array will
  1216. be treated as a singleton list containing them.
  1217. Returns
  1218. -------
  1219. list of dict
  1220. A list of dictionaries containing the results for each column of data.
  1221. The dictionaries contain at least the following:
  1222. - coords: A list of scalars containing the coordinates this particular
  1223. kernel density estimate was evaluated at.
  1224. - vals: A list of scalars containing the values of the kernel density
  1225. estimate at each of the coordinates given in *coords*.
  1226. - mean: The mean value for this column of data.
  1227. - median: The median value for this column of data.
  1228. - min: The minimum value for this column of data.
  1229. - max: The maximum value for this column of data.
  1230. - quantiles: The quantile values for this column of data.
  1231. """
  1232. # List of dictionaries describing each of the violins.
  1233. vpstats = []
  1234. # Want X to be a list of data sequences
  1235. X = _reshape_2D(X, "X")
  1236. # Want quantiles to be as the same shape as data sequences
  1237. if quantiles is not None and len(quantiles) != 0:
  1238. quantiles = _reshape_2D(quantiles, "quantiles")
  1239. # Else, mock quantiles if it's none or empty
  1240. else:
  1241. quantiles = [[]] * len(X)
  1242. # quantiles should have the same size as dataset
  1243. if len(X) != len(quantiles):
  1244. raise ValueError("List of violinplot statistics and quantiles values"
  1245. " must have the same length")
  1246. # Zip x and quantiles
  1247. for (x, q) in zip(X, quantiles):
  1248. # Dictionary of results for this distribution
  1249. stats = {}
  1250. # Calculate basic stats for the distribution
  1251. min_val = np.min(x)
  1252. max_val = np.max(x)
  1253. quantile_val = np.percentile(x, 100 * q)
  1254. # Evaluate the kernel density estimate
  1255. coords = np.linspace(min_val, max_val, points)
  1256. stats['vals'] = method(x, coords)
  1257. stats['coords'] = coords
  1258. # Store additional statistics for this distribution
  1259. stats['mean'] = np.mean(x)
  1260. stats['median'] = np.median(x)
  1261. stats['min'] = min_val
  1262. stats['max'] = max_val
  1263. stats['quantiles'] = np.atleast_1d(quantile_val)
  1264. # Append to output
  1265. vpstats.append(stats)
  1266. return vpstats
  1267. def pts_to_prestep(x, *args):
  1268. """
  1269. Convert continuous line to pre-steps.
  1270. Given a set of ``N`` points, convert to ``2N - 1`` points, which when
  1271. connected linearly give a step function which changes values at the
  1272. beginning of the intervals.
  1273. Parameters
  1274. ----------
  1275. x : array
  1276. The x location of the steps. May be empty.
  1277. y1, ..., yp : array
  1278. y arrays to be turned into steps; all must be the same length as ``x``.
  1279. Returns
  1280. -------
  1281. array
  1282. The x and y values converted to steps in the same order as the input;
  1283. can be unpacked as ``x_out, y1_out, ..., yp_out``. If the input is
  1284. length ``N``, each of these arrays will be length ``2N + 1``. For
  1285. ``N=0``, the length will be 0.
  1286. Examples
  1287. --------
  1288. >>> x_s, y1_s, y2_s = pts_to_prestep(x, y1, y2)
  1289. """
  1290. steps = np.zeros((1 + len(args), max(2 * len(x) - 1, 0)))
  1291. # In all `pts_to_*step` functions, only assign once using *x* and *args*,
  1292. # as converting to an array may be expensive.
  1293. steps[0, 0::2] = x
  1294. steps[0, 1::2] = steps[0, 0:-2:2]
  1295. steps[1:, 0::2] = args
  1296. steps[1:, 1::2] = steps[1:, 2::2]
  1297. return steps
  1298. def pts_to_poststep(x, *args):
  1299. """
  1300. Convert continuous line to post-steps.
  1301. Given a set of ``N`` points convert to ``2N + 1`` points, which when
  1302. connected linearly give a step function which changes values at the end of
  1303. the intervals.
  1304. Parameters
  1305. ----------
  1306. x : array
  1307. The x location of the steps. May be empty.
  1308. y1, ..., yp : array
  1309. y arrays to be turned into steps; all must be the same length as ``x``.
  1310. Returns
  1311. -------
  1312. array
  1313. The x and y values converted to steps in the same order as the input;
  1314. can be unpacked as ``x_out, y1_out, ..., yp_out``. If the input is
  1315. length ``N``, each of these arrays will be length ``2N + 1``. For
  1316. ``N=0``, the length will be 0.
  1317. Examples
  1318. --------
  1319. >>> x_s, y1_s, y2_s = pts_to_poststep(x, y1, y2)
  1320. """
  1321. steps = np.zeros((1 + len(args), max(2 * len(x) - 1, 0)))
  1322. steps[0, 0::2] = x
  1323. steps[0, 1::2] = steps[0, 2::2]
  1324. steps[1:, 0::2] = args
  1325. steps[1:, 1::2] = steps[1:, 0:-2:2]
  1326. return steps
  1327. def pts_to_midstep(x, *args):
  1328. """
  1329. Convert continuous line to mid-steps.
  1330. Given a set of ``N`` points convert to ``2N`` points which when connected
  1331. linearly give a step function which changes values at the middle of the
  1332. intervals.
  1333. Parameters
  1334. ----------
  1335. x : array
  1336. The x location of the steps. May be empty.
  1337. y1, ..., yp : array
  1338. y arrays to be turned into steps; all must be the same length as
  1339. ``x``.
  1340. Returns
  1341. -------
  1342. array
  1343. The x and y values converted to steps in the same order as the input;
  1344. can be unpacked as ``x_out, y1_out, ..., yp_out``. If the input is
  1345. length ``N``, each of these arrays will be length ``2N``.
  1346. Examples
  1347. --------
  1348. >>> x_s, y1_s, y2_s = pts_to_midstep(x, y1, y2)
  1349. """
  1350. steps = np.zeros((1 + len(args), 2 * len(x)))
  1351. x = np.asanyarray(x)
  1352. steps[0, 1:-1:2] = steps[0, 2::2] = (x[:-1] + x[1:]) / 2
  1353. steps[0, :1] = x[:1] # Also works for zero-sized input.
  1354. steps[0, -1:] = x[-1:]
  1355. steps[1:, 0::2] = args
  1356. steps[1:, 1::2] = steps[1:, 0::2]
  1357. return steps
  1358. STEP_LOOKUP_MAP = {'default': lambda x, y: (x, y),
  1359. 'steps': pts_to_prestep,
  1360. 'steps-pre': pts_to_prestep,
  1361. 'steps-post': pts_to_poststep,
  1362. 'steps-mid': pts_to_midstep}
  1363. def index_of(y):
  1364. """
  1365. A helper function to create reasonable x values for the given *y*.
  1366. This is used for plotting (x, y) if x values are not explicitly given.
  1367. First try ``y.index`` (assuming *y* is a `pandas.Series`), if that
  1368. fails, use ``range(len(y))``.
  1369. This will be extended in the future to deal with more types of
  1370. labeled data.
  1371. Parameters
  1372. ----------
  1373. y : float or array-like
  1374. Returns
  1375. -------
  1376. x, y : ndarray
  1377. The x and y values to plot.
  1378. """
  1379. try:
  1380. return y.index.to_numpy(), y.to_numpy()
  1381. except AttributeError:
  1382. pass
  1383. try:
  1384. y = _check_1d(y)
  1385. except (VisibleDeprecationWarning, ValueError):
  1386. # NumPy 1.19 will warn on ragged input, and we can't actually use it.
  1387. pass
  1388. else:
  1389. return np.arange(y.shape[0], dtype=float), y
  1390. raise ValueError('Input could not be cast to an at-least-1D NumPy array')
  1391. def safe_first_element(obj):
  1392. """
  1393. Return the first element in *obj*.
  1394. This is a type-independent way of obtaining the first element,
  1395. supporting both index access and the iterator protocol.
  1396. """
  1397. if isinstance(obj, collections.abc.Iterator):
  1398. # needed to accept `array.flat` as input.
  1399. # np.flatiter reports as an instance of collections.Iterator but can still be
  1400. # indexed via []. This has the side effect of re-setting the iterator, but
  1401. # that is acceptable.
  1402. try:
  1403. return obj[0]
  1404. except TypeError:
  1405. pass
  1406. raise RuntimeError("matplotlib does not support generators as input")
  1407. return next(iter(obj))
  1408. def _safe_first_finite(obj):
  1409. """
  1410. Return the first finite element in *obj* if one is available and skip_nonfinite is
  1411. True. Otherwise, return the first element.
  1412. This is a method for internal use.
  1413. This is a type-independent way of obtaining the first finite element, supporting
  1414. both index access and the iterator protocol.
  1415. """
  1416. def safe_isfinite(val):
  1417. if val is None:
  1418. return False
  1419. try:
  1420. return math.isfinite(val)
  1421. except (TypeError, ValueError):
  1422. # if the outer object is 2d, then val is a 1d array, and
  1423. # - math.isfinite(numpy.zeros(3)) raises TypeError
  1424. # - math.isfinite(torch.zeros(3)) raises ValueError
  1425. pass
  1426. try:
  1427. return np.isfinite(val) if np.isscalar(val) else True
  1428. except TypeError:
  1429. # This is something that NumPy cannot make heads or tails of,
  1430. # assume "finite"
  1431. return True
  1432. if isinstance(obj, np.flatiter):
  1433. # TODO do the finite filtering on this
  1434. return obj[0]
  1435. elif isinstance(obj, collections.abc.Iterator):
  1436. raise RuntimeError("matplotlib does not support generators as input")
  1437. else:
  1438. for val in obj:
  1439. if safe_isfinite(val):
  1440. return val
  1441. return safe_first_element(obj)
  1442. def sanitize_sequence(data):
  1443. """
  1444. Convert dictview objects to list. Other inputs are returned unchanged.
  1445. """
  1446. return (list(data) if isinstance(data, collections.abc.MappingView)
  1447. else data)
  1448. def normalize_kwargs(kw, alias_mapping=None):
  1449. """
  1450. Helper function to normalize kwarg inputs.
  1451. Parameters
  1452. ----------
  1453. kw : dict or None
  1454. A dict of keyword arguments. None is explicitly supported and treated
  1455. as an empty dict, to support functions with an optional parameter of
  1456. the form ``props=None``.
  1457. alias_mapping : dict or Artist subclass or Artist instance, optional
  1458. A mapping between a canonical name to a list of aliases, in order of
  1459. precedence from lowest to highest.
  1460. If the canonical value is not in the list it is assumed to have the
  1461. highest priority.
  1462. If an Artist subclass or instance is passed, use its properties alias
  1463. mapping.
  1464. Raises
  1465. ------
  1466. TypeError
  1467. To match what Python raises if invalid arguments/keyword arguments are
  1468. passed to a callable.
  1469. """
  1470. from matplotlib.artist import Artist
  1471. if kw is None:
  1472. return {}
  1473. # deal with default value of alias_mapping
  1474. if alias_mapping is None:
  1475. alias_mapping = {}
  1476. elif (isinstance(alias_mapping, type) and issubclass(alias_mapping, Artist)
  1477. or isinstance(alias_mapping, Artist)):
  1478. alias_mapping = getattr(alias_mapping, "_alias_map", {})
  1479. to_canonical = {alias: canonical
  1480. for canonical, alias_list in alias_mapping.items()
  1481. for alias in alias_list}
  1482. canonical_to_seen = {}
  1483. ret = {} # output dictionary
  1484. for k, v in kw.items():
  1485. canonical = to_canonical.get(k, k)
  1486. if canonical in canonical_to_seen:
  1487. raise TypeError(f"Got both {canonical_to_seen[canonical]!r} and "
  1488. f"{k!r}, which are aliases of one another")
  1489. canonical_to_seen[canonical] = k
  1490. ret[canonical] = v
  1491. return ret
  1492. @contextlib.contextmanager
  1493. def _lock_path(path):
  1494. """
  1495. Context manager for locking a path.
  1496. Usage::
  1497. with _lock_path(path):
  1498. ...
  1499. Another thread or process that attempts to lock the same path will wait
  1500. until this context manager is exited.
  1501. The lock is implemented by creating a temporary file in the parent
  1502. directory, so that directory must exist and be writable.
  1503. """
  1504. path = Path(path)
  1505. lock_path = path.with_name(path.name + ".matplotlib-lock")
  1506. retries = 50
  1507. sleeptime = 0.1
  1508. for _ in range(retries):
  1509. try:
  1510. with lock_path.open("xb"):
  1511. break
  1512. except FileExistsError:
  1513. time.sleep(sleeptime)
  1514. else:
  1515. raise TimeoutError("""\
  1516. Lock error: Matplotlib failed to acquire the following lock file:
  1517. {}
  1518. This maybe due to another process holding this lock file. If you are sure no
  1519. other Matplotlib process is running, remove this file and try again.""".format(
  1520. lock_path))
  1521. try:
  1522. yield
  1523. finally:
  1524. lock_path.unlink()
  1525. def _topmost_artist(
  1526. artists,
  1527. _cached_max=functools.partial(max, key=operator.attrgetter("zorder"))):
  1528. """
  1529. Get the topmost artist of a list.
  1530. In case of a tie, return the *last* of the tied artists, as it will be
  1531. drawn on top of the others. `max` returns the first maximum in case of
  1532. ties, so we need to iterate over the list in reverse order.
  1533. """
  1534. return _cached_max(reversed(artists))
  1535. def _str_equal(obj, s):
  1536. """
  1537. Return whether *obj* is a string equal to string *s*.
  1538. This helper solely exists to handle the case where *obj* is a numpy array,
  1539. because in such cases, a naive ``obj == s`` would yield an array, which
  1540. cannot be used in a boolean context.
  1541. """
  1542. return isinstance(obj, str) and obj == s
  1543. def _str_lower_equal(obj, s):
  1544. """
  1545. Return whether *obj* is a string equal, when lowercased, to string *s*.
  1546. This helper solely exists to handle the case where *obj* is a numpy array,
  1547. because in such cases, a naive ``obj == s`` would yield an array, which
  1548. cannot be used in a boolean context.
  1549. """
  1550. return isinstance(obj, str) and obj.lower() == s
  1551. def _array_perimeter(arr):
  1552. """
  1553. Get the elements on the perimeter of *arr*.
  1554. Parameters
  1555. ----------
  1556. arr : ndarray, shape (M, N)
  1557. The input array.
  1558. Returns
  1559. -------
  1560. ndarray, shape (2*(M - 1) + 2*(N - 1),)
  1561. The elements on the perimeter of the array::
  1562. [arr[0, 0], ..., arr[0, -1], ..., arr[-1, -1], ..., arr[-1, 0], ...]
  1563. Examples
  1564. --------
  1565. >>> i, j = np.ogrid[:3, :4]
  1566. >>> a = i*10 + j
  1567. >>> a
  1568. array([[ 0, 1, 2, 3],
  1569. [10, 11, 12, 13],
  1570. [20, 21, 22, 23]])
  1571. >>> _array_perimeter(a)
  1572. array([ 0, 1, 2, 3, 13, 23, 22, 21, 20, 10])
  1573. """
  1574. # note we use Python's half-open ranges to avoid repeating
  1575. # the corners
  1576. forward = np.s_[0:-1] # [0 ... -1)
  1577. backward = np.s_[-1:0:-1] # [-1 ... 0)
  1578. return np.concatenate((
  1579. arr[0, forward],
  1580. arr[forward, -1],
  1581. arr[-1, backward],
  1582. arr[backward, 0],
  1583. ))
  1584. def _unfold(arr, axis, size, step):
  1585. """
  1586. Append an extra dimension containing sliding windows along *axis*.
  1587. All windows are of size *size* and begin with every *step* elements.
  1588. Parameters
  1589. ----------
  1590. arr : ndarray, shape (N_1, ..., N_k)
  1591. The input array
  1592. axis : int
  1593. Axis along which the windows are extracted
  1594. size : int
  1595. Size of the windows
  1596. step : int
  1597. Stride between first elements of subsequent windows.
  1598. Returns
  1599. -------
  1600. ndarray, shape (N_1, ..., 1 + (N_axis-size)/step, ..., N_k, size)
  1601. Examples
  1602. --------
  1603. >>> i, j = np.ogrid[:3, :7]
  1604. >>> a = i*10 + j
  1605. >>> a
  1606. array([[ 0, 1, 2, 3, 4, 5, 6],
  1607. [10, 11, 12, 13, 14, 15, 16],
  1608. [20, 21, 22, 23, 24, 25, 26]])
  1609. >>> _unfold(a, axis=1, size=3, step=2)
  1610. array([[[ 0, 1, 2],
  1611. [ 2, 3, 4],
  1612. [ 4, 5, 6]],
  1613. [[10, 11, 12],
  1614. [12, 13, 14],
  1615. [14, 15, 16]],
  1616. [[20, 21, 22],
  1617. [22, 23, 24],
  1618. [24, 25, 26]]])
  1619. """
  1620. new_shape = [*arr.shape, size]
  1621. new_strides = [*arr.strides, arr.strides[axis]]
  1622. new_shape[axis] = (new_shape[axis] - size) // step + 1
  1623. new_strides[axis] = new_strides[axis] * step
  1624. return np.lib.stride_tricks.as_strided(arr,
  1625. shape=new_shape,
  1626. strides=new_strides,
  1627. writeable=False)
  1628. def _array_patch_perimeters(x, rstride, cstride):
  1629. """
  1630. Extract perimeters of patches from *arr*.
  1631. Extracted patches are of size (*rstride* + 1) x (*cstride* + 1) and
  1632. share perimeters with their neighbors. The ordering of the vertices matches
  1633. that returned by ``_array_perimeter``.
  1634. Parameters
  1635. ----------
  1636. x : ndarray, shape (N, M)
  1637. Input array
  1638. rstride : int
  1639. Vertical (row) stride between corresponding elements of each patch
  1640. cstride : int
  1641. Horizontal (column) stride between corresponding elements of each patch
  1642. Returns
  1643. -------
  1644. ndarray, shape (N/rstride * M/cstride, 2 * (rstride + cstride))
  1645. """
  1646. assert rstride > 0 and cstride > 0
  1647. assert (x.shape[0] - 1) % rstride == 0
  1648. assert (x.shape[1] - 1) % cstride == 0
  1649. # We build up each perimeter from four half-open intervals. Here is an
  1650. # illustrated explanation for rstride == cstride == 3
  1651. #
  1652. # T T T R
  1653. # L R
  1654. # L R
  1655. # L B B B
  1656. #
  1657. # where T means that this element will be in the top array, R for right,
  1658. # B for bottom and L for left. Each of the arrays below has a shape of:
  1659. #
  1660. # (number of perimeters that can be extracted vertically,
  1661. # number of perimeters that can be extracted horizontally,
  1662. # cstride for top and bottom and rstride for left and right)
  1663. #
  1664. # Note that _unfold doesn't incur any memory copies, so the only costly
  1665. # operation here is the np.concatenate.
  1666. top = _unfold(x[:-1:rstride, :-1], 1, cstride, cstride)
  1667. bottom = _unfold(x[rstride::rstride, 1:], 1, cstride, cstride)[..., ::-1]
  1668. right = _unfold(x[:-1, cstride::cstride], 0, rstride, rstride)
  1669. left = _unfold(x[1:, :-1:cstride], 0, rstride, rstride)[..., ::-1]
  1670. return (np.concatenate((top, right, bottom, left), axis=2)
  1671. .reshape(-1, 2 * (rstride + cstride)))
  1672. @contextlib.contextmanager
  1673. def _setattr_cm(obj, **kwargs):
  1674. """
  1675. Temporarily set some attributes; restore original state at context exit.
  1676. """
  1677. sentinel = object()
  1678. origs = {}
  1679. for attr in kwargs:
  1680. orig = getattr(obj, attr, sentinel)
  1681. if attr in obj.__dict__ or orig is sentinel:
  1682. # if we are pulling from the instance dict or the object
  1683. # does not have this attribute we can trust the above
  1684. origs[attr] = orig
  1685. else:
  1686. # if the attribute is not in the instance dict it must be
  1687. # from the class level
  1688. cls_orig = getattr(type(obj), attr)
  1689. # if we are dealing with a property (but not a general descriptor)
  1690. # we want to set the original value back.
  1691. if isinstance(cls_orig, property):
  1692. origs[attr] = orig
  1693. # otherwise this is _something_ we are going to shadow at
  1694. # the instance dict level from higher up in the MRO. We
  1695. # are going to assume we can delattr(obj, attr) to clean
  1696. # up after ourselves. It is possible that this code will
  1697. # fail if used with a non-property custom descriptor which
  1698. # implements __set__ (and __delete__ does not act like a
  1699. # stack). However, this is an internal tool and we do not
  1700. # currently have any custom descriptors.
  1701. else:
  1702. origs[attr] = sentinel
  1703. try:
  1704. for attr, val in kwargs.items():
  1705. setattr(obj, attr, val)
  1706. yield
  1707. finally:
  1708. for attr, orig in origs.items():
  1709. if orig is sentinel:
  1710. delattr(obj, attr)
  1711. else:
  1712. setattr(obj, attr, orig)
  1713. class _OrderedSet(collections.abc.MutableSet):
  1714. def __init__(self):
  1715. self._od = collections.OrderedDict()
  1716. def __contains__(self, key):
  1717. return key in self._od
  1718. def __iter__(self):
  1719. return iter(self._od)
  1720. def __len__(self):
  1721. return len(self._od)
  1722. def add(self, key):
  1723. self._od.pop(key, None)
  1724. self._od[key] = None
  1725. def discard(self, key):
  1726. self._od.pop(key, None)
  1727. # Agg's buffers are unmultiplied RGBA8888, which neither PyQt<=5.1 nor cairo
  1728. # support; however, both do support premultiplied ARGB32.
  1729. def _premultiplied_argb32_to_unmultiplied_rgba8888(buf):
  1730. """
  1731. Convert a premultiplied ARGB32 buffer to an unmultiplied RGBA8888 buffer.
  1732. """
  1733. rgba = np.take( # .take() ensures C-contiguity of the result.
  1734. buf,
  1735. [2, 1, 0, 3] if sys.byteorder == "little" else [1, 2, 3, 0], axis=2)
  1736. rgb = rgba[..., :-1]
  1737. alpha = rgba[..., -1]
  1738. # Un-premultiply alpha. The formula is the same as in cairo-png.c.
  1739. mask = alpha != 0
  1740. for channel in np.rollaxis(rgb, -1):
  1741. channel[mask] = (
  1742. (channel[mask].astype(int) * 255 + alpha[mask] // 2)
  1743. // alpha[mask])
  1744. return rgba
  1745. def _unmultiplied_rgba8888_to_premultiplied_argb32(rgba8888):
  1746. """
  1747. Convert an unmultiplied RGBA8888 buffer to a premultiplied ARGB32 buffer.
  1748. """
  1749. if sys.byteorder == "little":
  1750. argb32 = np.take(rgba8888, [2, 1, 0, 3], axis=2)
  1751. rgb24 = argb32[..., :-1]
  1752. alpha8 = argb32[..., -1:]
  1753. else:
  1754. argb32 = np.take(rgba8888, [3, 0, 1, 2], axis=2)
  1755. alpha8 = argb32[..., :1]
  1756. rgb24 = argb32[..., 1:]
  1757. # Only bother premultiplying when the alpha channel is not fully opaque,
  1758. # as the cost is not negligible. The unsafe cast is needed to do the
  1759. # multiplication in-place in an integer buffer.
  1760. if alpha8.min() != 0xff:
  1761. np.multiply(rgb24, alpha8 / 0xff, out=rgb24, casting="unsafe")
  1762. return argb32
  1763. def _get_nonzero_slices(buf):
  1764. """
  1765. Return the bounds of the nonzero region of a 2D array as a pair of slices.
  1766. ``buf[_get_nonzero_slices(buf)]`` is the smallest sub-rectangle in *buf*
  1767. that encloses all non-zero entries in *buf*. If *buf* is fully zero, then
  1768. ``(slice(0, 0), slice(0, 0))`` is returned.
  1769. """
  1770. x_nz, = buf.any(axis=0).nonzero()
  1771. y_nz, = buf.any(axis=1).nonzero()
  1772. if len(x_nz) and len(y_nz):
  1773. l, r = x_nz[[0, -1]]
  1774. b, t = y_nz[[0, -1]]
  1775. return slice(b, t + 1), slice(l, r + 1)
  1776. else:
  1777. return slice(0, 0), slice(0, 0)
  1778. def _pformat_subprocess(command):
  1779. """Pretty-format a subprocess command for printing/logging purposes."""
  1780. return (command if isinstance(command, str)
  1781. else " ".join(shlex.quote(os.fspath(arg)) for arg in command))
  1782. def _check_and_log_subprocess(command, logger, **kwargs):
  1783. """
  1784. Run *command*, returning its stdout output if it succeeds.
  1785. If it fails (exits with nonzero return code), raise an exception whose text
  1786. includes the failed command and captured stdout and stderr output.
  1787. Regardless of the return code, the command is logged at DEBUG level on
  1788. *logger*. In case of success, the output is likewise logged.
  1789. """
  1790. logger.debug('%s', _pformat_subprocess(command))
  1791. proc = subprocess.run(command, capture_output=True, **kwargs)
  1792. if proc.returncode:
  1793. stdout = proc.stdout
  1794. if isinstance(stdout, bytes):
  1795. stdout = stdout.decode()
  1796. stderr = proc.stderr
  1797. if isinstance(stderr, bytes):
  1798. stderr = stderr.decode()
  1799. raise RuntimeError(
  1800. f"The command\n"
  1801. f" {_pformat_subprocess(command)}\n"
  1802. f"failed and generated the following output:\n"
  1803. f"{stdout}\n"
  1804. f"and the following error:\n"
  1805. f"{stderr}")
  1806. if proc.stdout:
  1807. logger.debug("stdout:\n%s", proc.stdout)
  1808. if proc.stderr:
  1809. logger.debug("stderr:\n%s", proc.stderr)
  1810. return proc.stdout
  1811. def _setup_new_guiapp():
  1812. """
  1813. Perform OS-dependent setup when Matplotlib creates a new GUI application.
  1814. """
  1815. # Windows: If not explicit app user model id has been set yet (so we're not
  1816. # already embedded), then set it to "matplotlib", so that taskbar icons are
  1817. # correct.
  1818. try:
  1819. _c_internal_utils.Win32_GetCurrentProcessExplicitAppUserModelID()
  1820. except OSError:
  1821. _c_internal_utils.Win32_SetCurrentProcessExplicitAppUserModelID(
  1822. "matplotlib")
  1823. def _format_approx(number, precision):
  1824. """
  1825. Format the number with at most the number of decimals given as precision.
  1826. Remove trailing zeros and possibly the decimal point.
  1827. """
  1828. return f'{number:.{precision}f}'.rstrip('0').rstrip('.') or '0'
  1829. def _g_sig_digits(value, delta):
  1830. """
  1831. Return the number of significant digits to %g-format *value*, assuming that
  1832. it is known with an error of *delta*.
  1833. """
  1834. # For inf or nan, the precision doesn't matter.
  1835. if not math.isfinite(value):
  1836. return 0
  1837. if delta == 0:
  1838. if value == 0:
  1839. # if both value and delta are 0, np.spacing below returns 5e-324
  1840. # which results in rather silly results
  1841. return 3
  1842. # delta = 0 may occur when trying to format values over a tiny range;
  1843. # in that case, replace it by the distance to the closest float.
  1844. delta = abs(np.spacing(value))
  1845. # If e.g. value = 45.67 and delta = 0.02, then we want to round to 2 digits
  1846. # after the decimal point (floor(log10(0.02)) = -2); 45.67 contributes 2
  1847. # digits before the decimal point (floor(log10(45.67)) + 1 = 2): the total
  1848. # is 4 significant digits. A value of 0 contributes 1 "digit" before the
  1849. # decimal point.
  1850. return max(
  1851. 0,
  1852. (math.floor(math.log10(abs(value))) + 1 if value else 1)
  1853. - math.floor(math.log10(delta)))
  1854. def _unikey_or_keysym_to_mplkey(unikey, keysym):
  1855. """
  1856. Convert a Unicode key or X keysym to a Matplotlib key name.
  1857. The Unicode key is checked first; this avoids having to list most printable
  1858. keysyms such as ``EuroSign``.
  1859. """
  1860. # For non-printable characters, gtk3 passes "\0" whereas tk passes an "".
  1861. if unikey and unikey.isprintable():
  1862. return unikey
  1863. key = keysym.lower()
  1864. if key.startswith("kp_"): # keypad_x (including kp_enter).
  1865. key = key[3:]
  1866. if key.startswith("page_"): # page_{up,down}
  1867. key = key.replace("page_", "page")
  1868. if key.endswith(("_l", "_r")): # alt_l, ctrl_l, shift_l.
  1869. key = key[:-2]
  1870. if sys.platform == "darwin" and key == "meta":
  1871. # meta should be reported as command on mac
  1872. key = "cmd"
  1873. key = {
  1874. "return": "enter",
  1875. "prior": "pageup", # Used by tk.
  1876. "next": "pagedown", # Used by tk.
  1877. }.get(key, key)
  1878. return key
  1879. @functools.cache
  1880. def _make_class_factory(mixin_class, fmt, attr_name=None):
  1881. """
  1882. Return a function that creates picklable classes inheriting from a mixin.
  1883. After ::
  1884. factory = _make_class_factory(FooMixin, fmt, attr_name)
  1885. FooAxes = factory(Axes)
  1886. ``Foo`` is a class that inherits from ``FooMixin`` and ``Axes`` and **is
  1887. picklable** (picklability is what differentiates this from a plain call to
  1888. `type`). Its ``__name__`` is set to ``fmt.format(Axes.__name__)`` and the
  1889. base class is stored in the ``attr_name`` attribute, if not None.
  1890. Moreover, the return value of ``factory`` is memoized: calls with the same
  1891. ``Axes`` class always return the same subclass.
  1892. """
  1893. @functools.cache
  1894. def class_factory(axes_class):
  1895. # if we have already wrapped this class, declare victory!
  1896. if issubclass(axes_class, mixin_class):
  1897. return axes_class
  1898. # The parameter is named "axes_class" for backcompat but is really just
  1899. # a base class; no axes semantics are used.
  1900. base_class = axes_class
  1901. class subcls(mixin_class, base_class):
  1902. # Better approximation than __module__ = "matplotlib.cbook".
  1903. __module__ = mixin_class.__module__
  1904. def __reduce__(self):
  1905. return (_picklable_class_constructor,
  1906. (mixin_class, fmt, attr_name, base_class),
  1907. self.__getstate__())
  1908. subcls.__name__ = subcls.__qualname__ = fmt.format(base_class.__name__)
  1909. if attr_name is not None:
  1910. setattr(subcls, attr_name, base_class)
  1911. return subcls
  1912. class_factory.__module__ = mixin_class.__module__
  1913. return class_factory
  1914. def _picklable_class_constructor(mixin_class, fmt, attr_name, base_class):
  1915. """Internal helper for _make_class_factory."""
  1916. factory = _make_class_factory(mixin_class, fmt, attr_name)
  1917. cls = factory(base_class)
  1918. return cls.__new__(cls)
  1919. def _is_torch_array(x):
  1920. """Return whether *x* is a PyTorch Tensor."""
  1921. try:
  1922. # We're intentionally not attempting to import torch. If somebody
  1923. # has created a torch array, torch should already be in sys.modules.
  1924. tp = sys.modules.get("torch").Tensor
  1925. except AttributeError:
  1926. return False # Module not imported or a nonstandard module with no Tensor attr.
  1927. return (isinstance(tp, type) # Just in case it's a very nonstandard module.
  1928. and isinstance(x, tp))
  1929. def _is_jax_array(x):
  1930. """Return whether *x* is a JAX Array."""
  1931. try:
  1932. # We're intentionally not attempting to import jax. If somebody
  1933. # has created a jax array, jax should already be in sys.modules.
  1934. tp = sys.modules.get("jax").Array
  1935. except AttributeError:
  1936. return False # Module not imported or a nonstandard module with no Array attr.
  1937. return (isinstance(tp, type) # Just in case it's a very nonstandard module.
  1938. and isinstance(x, tp))
  1939. def _is_pandas_dataframe(x):
  1940. """Check if *x* is a Pandas DataFrame."""
  1941. try:
  1942. # We're intentionally not attempting to import Pandas. If somebody
  1943. # has created a Pandas DataFrame, Pandas should already be in sys.modules.
  1944. tp = sys.modules.get("pandas").DataFrame
  1945. except AttributeError:
  1946. return False # Module not imported or a nonstandard module with no Array attr.
  1947. return (isinstance(tp, type) # Just in case it's a very nonstandard module.
  1948. and isinstance(x, tp))
  1949. def _is_tensorflow_array(x):
  1950. """Return whether *x* is a TensorFlow Tensor or Variable."""
  1951. try:
  1952. # We're intentionally not attempting to import TensorFlow. If somebody
  1953. # has created a TensorFlow array, TensorFlow should already be in
  1954. # sys.modules we use `is_tensor` to not depend on the class structure
  1955. # of TensorFlow arrays, as `tf.Variables` are not instances of
  1956. # `tf.Tensor` (they both convert the same way).
  1957. is_tensor = sys.modules.get("tensorflow").is_tensor
  1958. except AttributeError:
  1959. return False
  1960. try:
  1961. return is_tensor(x)
  1962. except Exception:
  1963. return False # Just in case it's a very nonstandard module.
  1964. def _unpack_to_numpy(x):
  1965. """Internal helper to extract data from e.g. pandas and xarray objects."""
  1966. if isinstance(x, np.ndarray):
  1967. # If numpy, return directly
  1968. return x
  1969. if hasattr(x, 'to_numpy'):
  1970. # Assume that any to_numpy() method actually returns a numpy array
  1971. return x.to_numpy()
  1972. if hasattr(x, 'values'):
  1973. xtmp = x.values
  1974. # For example a dict has a 'values' attribute, but it is not a property
  1975. # so in this case we do not want to return a function
  1976. if isinstance(xtmp, np.ndarray):
  1977. return xtmp
  1978. if _is_torch_array(x) or _is_jax_array(x) or _is_tensorflow_array(x):
  1979. # using np.asarray() instead of explicitly __array__(), as the latter is
  1980. # only _one_ of many methods, and it's the last resort, see also
  1981. # https://numpy.org/devdocs/user/basics.interoperability.html#using-arbitrary-objects-in-numpy
  1982. # therefore, let arrays do better if they can
  1983. xtmp = np.asarray(x)
  1984. # In case np.asarray method does not return a numpy array in future
  1985. if isinstance(xtmp, np.ndarray):
  1986. return xtmp
  1987. return x
  1988. def _auto_format_str(fmt, value):
  1989. """
  1990. Apply *value* to the format string *fmt*.
  1991. This works both with unnamed %-style formatting and
  1992. unnamed {}-style formatting. %-style formatting has priority.
  1993. If *fmt* is %-style formattable that will be used. Otherwise,
  1994. {}-formatting is applied. Strings without formatting placeholders
  1995. are passed through as is.
  1996. Examples
  1997. --------
  1998. >>> _auto_format_str('%.2f m', 0.2)
  1999. '0.20 m'
  2000. >>> _auto_format_str('{} m', 0.2)
  2001. '0.2 m'
  2002. >>> _auto_format_str('const', 0.2)
  2003. 'const'
  2004. >>> _auto_format_str('%d or {}', 0.2)
  2005. '0 or {}'
  2006. """
  2007. try:
  2008. return fmt % (value,)
  2009. except (TypeError, ValueError):
  2010. return fmt.format(value)