rL i7dZddlZddlmZddlZddlZddlm Z ddl m Z ddl m Z ddlmZejeZdZd gZd Zd ZdZd Zd ZGddZy)z7 Module for creating Sankey diagrams using Matplotlib. N)SimpleNamespace)Path) PathPatch)Affine2D) _docstringzKevin L. Daviesz Yannick CopinBSDz 2011/09/16ceZdZdZ d dZd dZdZdZejfdZ e j d dZ d Zy) Sankeyab Sankey diagram. Sankey diagrams are a specific type of flow diagram, in which the width of the arrows is shown proportionally to the flow quantity. They are typically used to visualize energy or material or cost transfers between processes. `Wikipedia (6/1/2011) `_ Nc  |dkr td||kDr td| dkr td| dkr td|,ddlm} | j}|j dddgg}g|_||_||_||_||_ ||_ ||_ ||_ ||_ | |_tj tj"d| d z z zd z |_| |_tj(tj*tj* tj*tj* f|_t/| r|j0d i| yy) a Create a new Sankey instance. The optional arguments listed below are applied to all subdiagrams so that there is consistent alignment and formatting. In order to draw a complex Sankey diagram, create an instance of `Sankey` by calling it without any kwargs:: sankey = Sankey() Then add simple Sankey sub-diagrams:: sankey.add() # 1 sankey.add() # 2 #... sankey.add() # n Finally, create the full diagram:: sankey.finish() Or, instead, simply daisy-chain those calls:: Sankey().add().add... .add().finish() Other Parameters ---------------- ax : `~matplotlib.axes.Axes` Axes onto which the data should be plotted. If *ax* isn't provided, new Axes will be created. scale : float Scaling factor for the flows. *scale* sizes the width of the paths in order to maintain proper layout. The same scale is applied to all subdiagrams. The value should be chosen such that the product of the scale and the sum of the inputs is approximately 1.0 (and the product of the scale and the sum of the outputs is approximately -1.0). unit : str The physical unit associated with the flow quantities. If *unit* is None, then none of the quantities are labeled. format : str or callable A Python number formatting string or callable used to label the flows with their quantities (i.e., a number times a unit, where the unit is given). If a format string is given, the label will be ``format % quantity``. If a callable is given, it will be called with ``quantity`` as an argument. gap : float Space between paths that break in/break away to/from the top or bottom. radius : float Inner radius of the vertical paths. shoulder : float Size of the shoulders of output arrows. offset : float Text offset (from the dip or tip of the arrow). head_angle : float Angle, in degrees, of the arrow heads (and negative of the angle of the tails). margin : float Minimum space between Sankey outlines and the edge of the plot area. tolerance : float Acceptable maximum of the magnitude of the sum of flows. The magnitude of the sum of connected flows cannot be greater than *tolerance*. **kwargs Any additional keyword arguments will be passed to `add`, which will create the first subdiagram. See Also -------- Sankey.add Sankey.finish Examples -------- .. plot:: gallery/specialty_plots/sankey_basics.py rzS'gap' is negative, which is not allowed because it would cause the paths to overlapz`'radius' is greater than 'gap', which is not allowed because it would cause the paths to overlapzp'head_angle' is negative, which is not allowed because it would cause inputs to look like outputs and vice versaz3'tolerance' is negative, but it must be a magnitudeNr )xticksyticksgf@@) ValueErrormatplotlib.pyplotpyplotfigure add_subplotdiagramsaxunitformatscalegapradiusshoulderoffsetmarginnptanpipitch tolerancearrayinfextentlenadd)selfrrrrrrrr head_angler r%kwargspltfigs W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/matplotlib/sankey.py__init__zSankey.__init__*sdf 7-. . C<67 7 >IJ J q=EG G : +**,CAqB?B         VVBEEQe);%;ef?)rNr r) rLINETOCURVE4r!r& column_stacklistziptileshape)r+quadrantcwrcenter ARC_CODES ARC_VERTICESverticess r0_arcz Sankey._arcsg([[[[[[[[[[[[[[ " xx..!A"0.!A"0.!A"0.!A"0.!A"0.!A#1.!A!CD  v ''4R40??\!Q$-?,?-9!Q$-?,AB??\!Q$-?,?-9!Q$-?,AB a<WFC 6H#4););A)>(BC$DEF Fr2c  |ddgddgfS|dd\}}|dz |jz}|tk(r||z}||z||dz zg}|jtj||gftj|ftj|||zgftj||j z||zgfg|d|j z |dg} || fS||j z}|tk(rd} nd} ||dz z || ||z zz g}|tk(rd} nd} |jrU|j|j| |tk(|j||jz|| |jzz fn#|jtj||gf|jtj||| |zz gftj|ftj||z || |zz gfg|j|j| |tk(||jz||jz|| |jzz f|jtj||z || |zzgf|d|d| |j zz g} || fS)zP Add an input to a path and return its tip and label locations. rr6r r5rr>r?rr@) r$RIGHTextendrr7rrUPDOWNrrDappend) r+pathangleflowlengthxydipdepthdiplabel_locationsignr>s r0 _add_inputzSankey._add_inputs =q6Aq6> !8A;DAqqDJJ.H~V 8|Q^4 dkkAq62"kk3/"kkAq4x=9"kkAL!d(+CDFG#&a&4;;"6A!?D& &ATXX B;DD4!8|Q(1B)C%CDD= H H;;KK 8-2b[1523dkk/23dT[[6H2H2J!*!KL KKq!f 56 dkkAq4&=/@+AB"kk3/"kkAHa$-6G+HIKL DIIx).$-1DKK-?./$++o./$2D.D.F&GH  T[[1t8Q_*EFG"%a&#a&4$++3E*E!F& &r2c :|ddgddgfS|dd\}}|j|dz z |jz}|tk(r||z }||z||dz zg}|jtj ||gftj |||jzgftj |ftj |||jz |zgftj |||zgftj ||j z ||zgfg|d|jz|dg} || fS||j z }|tk(rd\} } nd\} } ||dz z || ||zzzg}|jrU|j|j| |tk(|j||jz || |jzzfn#|jtj ||gf|jtj ||| |zzgftj ||jz || |zzgftj |ftj ||jz|z || |zzgftj ||z || |zzgfg|j|j| |tk(|j|z ||jz || |jzzf|jtj ||z || |zzgf|d|d| |jzzg} || fS) z Append an output to a path and return its tip and label locations. .. note:: *flow* is negative for an output. rr6r r5r)r r )r6rrF) rr$rGrHrr7rrrIrrDrKrJ) r+rLrMrNrOrPrQ tipheighttiprTrUr>s r0 _add_outputzSankey._add_outputsB =q6Aq6> !8A;DAq1TZZ?I~V 9}a$*n5 dkkAq62"kkAq4==/@+AB"kk3/"kkAq4==/@4/G+HI"kkAq4x=9"kkAL!d(+CD FG #&a&4;;"6A!?@& &=TXX B;%)ND(%*ND(4#:~q46I3E+F'FG;;KK 8-2b[1523dkk/23dT[[6H2H2J!*!KL KKq!f 56 dkkAq4&=/@+AB"kkA ,=,-v ,=,?@"kk3/"kkA ,=,D,-v ,=,?@"kkAHa$-6G+HI KL DIIx).$-1[[4-?./$++o./$2D.D.F&GH  T[[1t8Q_*EFG"%a&#a&4$++3E*E!F& &r2cXg}|}|dddD]\}}|j||f|}|S)z A path is not simply reversible by path[::-1] since the code specifies an action to take from the **previous** point. Nr6)rK)r+rL first_action reverse_path next_codecodepositions r0_revertzSankey._revertLsI   "4R4j ND(   H 5 6I r2c  !|tjddgntj|}|jd} | d} n| dz} |d} tj|| } tj|| }|dkr t d t tj ||jkDr*tjd tj |||j|z} t d | D} t d | D}|y|dkr t dt|dkr t d|t|jk\r%t d|dt|jd|dt|j|jk\r>t dj|dt|j|j|d| k\rt d|dd| d|j|j |dt d|dd|d|j|j|d||dz}t ||jk\rt d|d|jddg| z}t#|D]R\}}||jk\rd||<||j krd||<1tjd |||jTdg| z}t#t%||D]k\}\}}|dk(r|r t&||<|dust(||<)|dk(r |1t*||<;|d!k7rt d"|d#|d$|r t(||<^|dusct&||<mtj,|r*t|| k7rt d%| d&t|d'|}|}|}|}t/|(}|Dcgc]}|j1|d}}t#t%||| D]8\}\}}}|t&k(r |r |||<||z } |t(k(s*|dus/|||<||z}:t#t3t5t%||| D]D\}\}}}|t(k(r|r||| |z dz <||z }&|t&k(s0|dus5||| |z dz <||z}Fd}t#t3t5t%||t%| |D]&\}\}}}|t*k(s|s|r d|| |z dz <%d}(d}t#t%||t5t%| |D]"\}\}}}|t*k(s|dus|rd||<!d}$t6j8|j:|d)z z | d)z gft6j<|j:|d)z z d)z | d)z gft6j>|j:|d)z z d*z | d)z gft6j>|d)z |j:z d*z | d)z gft6j<|d)z |j:z d)z | d)z gft6j<|d)z |j:z | d)z gfg}t6j<|d)z |j:z |d)z gft6j<|d)z |j:z d)z |d)z gft6j>|d)z |j:z d*z |d)z gft6j>|j:|d)z z d*z | d)z gft6j<|j:|d)z z d)z | d)z gft6j<|j:|d)z z | d)z gfg} t6j<|d)z |j:z |d)z gfg}!t6j<|j:|d)z z | d)z gfg}"tj@| d+f}#tj@| d+f}$t#t%||t5t%| |D]n\}\}}}|t&k(r(|r&|jB|"|g|\|#|ddf<|$|ddf<;|t(k(sE|dusJ|jD||g|\|#|ddf<|$|ddf<pt#t3t5t%||t5t%| |D]\}\}}}|t(k(r8|r6|jB| |g|\}%}&|%|#| |z dz ddf<|&|$| |z dz ddf<K|t&k(sU|dusZ|jD|!|g|\}%}&|%|#| |z dz ddf<|&|$| |z dz ddf<d}t#t3t5t%||t5t%| |D]\}\}}}|t*k(s|s|sN| d!dd|"d!ddkDr5| jGt6j<|"d!dd| d!ddgfd}|jB| |g|\}%}&|%|#| |z dz ddf<|&|$| |z dz ddf<d}t#t%||t5t%| |D]\}\}}}|t*k(s|dus|sN|d!dd|!d!ddkr5|jGt6j<|!d!dd|d!ddgfd}|jD||g|\|#|ddf<|$|ddf<|s |"jI| jI|s |!jI|jI||jK|!z| z|jK|"zt6jL|ddfgz}'t%|'\}(})tj|)})d,}*|{| dk7rT|Dcgc] }|*||  }}tOjQ| d-zjR}+|+|#}#|+|$}$|+|)})|jTjWdd|d.d./},n|j|j |d||dz } |Dcgc] }|*||  }}tOjQ| d-zjR}+|+|#}#|j|jX|d|#|dz }-tOjZ|-jR}.|.|#}#|.|+|$}$|.|+|)})t/|d.d./}/|jTjV|-i|/},t\j^d0rE| jId1| jId2d3}0| jId4| jId5d6}1nD| jId1| jId2d}0| jId4| jId5d}1|0$|jTj`jc}0tet7|)|(f|0|1d7| }2|jTjg|2g}3t%||||$D]\}4}}5}6|5|d8}5n|jhtk|jtlr&|jt |4z|jhz}7n2to|jr|j|4}7n tqd9|5d8k7r|5d:z }5|5|7z }5|3jG|jTjW|6d|6d|5d.d.;ttj|)dddftj|$dddf|jrdtutjt|)dddftjt|$dddf|jrdttj|)dddftj|$dddf|jrd+tutjt|)dddftjt|$dddf|jrd<f|_9|jjGtw|2|||#|,|3=|S#t$r:t dtj|dtj|ddwxYw#t$r:t dtj|d tj|ddwxYwcc}wcc}wcc}w)>u Add a simple Sankey diagram with flows at the same hierarchical level. Parameters ---------- patchlabel : str Label to be placed at the center of the diagram. Note that *label* (not *patchlabel*) can be passed as keyword argument to create an entry in the legend. flows : list of float Array of flow values. By convention, inputs are positive and outputs are negative. Flows are placed along the top of the diagram from the inside out in order of their index within *flows*. They are placed along the sides of the diagram from the top down and along the bottom from the outside in. If the sum of the inputs and outputs is nonzero, the discrepancy will appear as a cubic Bézier curve along the top and bottom edges of the trunk. orientations : list of {-1, 0, 1} List of orientations of the flows (or a single orientation to be used for all flows). Valid values are 0 (inputs from the left, outputs to the right), 1 (from and to the top) or -1 (from and to the bottom). labels : list of (str or None) List of labels for the flows (or a single label to be used for all flows). Each label may be *None* (no label), or a labeling string. If an entry is a (possibly empty) string, then the quantity for the corresponding flow will be shown below the string. However, if the *unit* of the main diagram is None, then quantities are never shown, regardless of the value of this argument. trunklength : float Length between the bases of the input and output groups (in data-space units). pathlengths : list of float List of lengths of the vertical arrows before break-in or after break-away. If a single value is given, then it will be applied to the first (inside) paths on the top and bottom, and the length of all other arrows will be justified accordingly. The *pathlengths* are not applied to the horizontal inputs and outputs. prior : int Index of the prior diagram to which this diagram should be connected. connect : (int, int) A (prior, this) tuple indexing the flow of the prior diagram and the flow of this diagram which should be connected. If this is the first diagram or *prior* is *None*, *connect* will be ignored. rotation : float Angle of rotation of the diagram in degrees. The interpretation of the *orientations* argument will be rotated accordingly (e.g., if *rotation* == 90, an *orientations* entry of 1 means to/from the left). *rotation* is ignored if this diagram is connected to an existing one (using *prior* and *connect*). Returns ------- Sankey The current `.Sankey` instance. Other Parameters ---------------- **kwargs Additional keyword arguments set `matplotlib.patches.PathPatch` properties, listed below. For example, one may want to use ``fill=False`` or ``label="A legend entry"``. %(Patch:kwdoc)s See Also -------- Sankey.finish Nr4grgV@zThe shapes of 'flows' z and 'orientations' z are incompatiblez and 'labels' zR'trunklength' is negative, which is not allowed because it would cause poor layoutzWThe sum of the flows is nonzero (%f; patchlabel=%r); is the system not at steady state?c34K|]}t|dywrN)max.0rNs r0 zSankey.add..9D3tQ<9c34K|]}t|dywrd)minrfs r0rhzSankey.add..rirjz*The index of the prior diagram is negativez2At least one of the connection indices is negativez"The index of the prior diagram is z, but there are only z other diagramszTThe connection index to the source diagram is {}, but that diagram has only {} flowsr z(The connection index to this diagram is z, but this diagram has only z flowszHThe connection cannot be made, which may occur if the magnitude of flow z of diagram z% is less than the specified tolerancez)The scaled sum of the connected flows is z%, which is not within the tolerance ()TFzwThe magnitude of flow %d (%f) is below the tolerance (%f). It will not be shown, and it cannot be used in a connection.r6zThe value of orientations[z] is z, but it must be -1, 0, or 1zThe lengths of 'flows' (z) and 'pathlengths' (z) are incompatible)rGrg @r5c|y||zS)Nr)ars r0 _get_anglezSankey.add.._get_anglesy1u r2Zr@)shavaz_internal.classic_modefc facecolorz#bfd1d4lw linewidthg?)rvrxz*format must be callable or a format string )rPrQrsrtrur )patchflowsanglestipstexttexts)wqzlK1126;<<}}U#**71:6> ))0 LH89::--.44WQZ@ +,J:$..0 ? |L::>..9ILMM VaZ  ' >#H"H"H"H;'A8>?u155??K?/8FJ)56KA 2-1N 2 % .7*d3|[+I&J9L/M 4**E8TE>5(+-.KN/3, 4;;$(([3->">!%!-.;;$(([3->">#!E!%!-.;;$(([3->">#!E!%!-.;;+"3dhh">#!E"&!./;;+"3dhh">#!E"&!./;;+"3dhh">"&!./ 0;;+"3dhh">!%!-.;;+"3dhh">#!E!%!-.;;+"3dhh">#!E!%!-.;;$(([3->">#!E"&!./;;$(([3->">#!E"&!./;;$(([3->">"&!./ 0;;+"3dhh">!%!-./;;K#,=!=!%!-./xxA((Aq6**3Cj$s<'E"F5H+I * &A&x}4CDOOE5*$(5*1QT OAqD1"U!24DD4D4DE5*$(5*1QT OAqD1  *+4HT#j$s<'E"FCH>I5J+K ? &A&x{x&5doofe&Kd&K#^%(QUQY\"0>A 1 -$8u#4&6d&6&6vu&Lt&L#^%(QUQY\"0>A 1 - ?*3HT#j$s<'E"FCH>I5J+K ? &A&x~(%bz!}Q'&*Q-*:: t{{VBZ]15E5;BZ]15E5G'HI%)N&5doofe&Kd&K#^%(QUQY\"0>A 1 - ?!*3Cj$s<'E"F5H+I * &A&x~(e"3'bz!}Q'&*Q-*:: t{{VBZ]15E5;BZ]15E5G'HI'+$4DD4D4DE5*$(5*1QT OAqD1 * JJL JJL JJL JJLf--6f9MM..&)A,/01t*x88H%  =1}CIJ%*UH5JJ!..x"}=NNd|"("9!(+77<<1 xH<MD e,33GAJ?wqz*+H?EFej1FFFZ**8b=9JJF$?BD&**[$"?@BD&**[$"?@B :..==?B$x/HB2HH % .1%2A/C A *FE5(} &dkk3/#{{S[8499DHdkk*#{{62H#DFFB;TME! LL x{(-)1h&@ A A.266(1a4.166/!Q$"78;;q>+266(1a4.166/!Q$"78;;q>+266(1a4.166/!Q$"78;;q>+266(1a4.166/!Q$"78;;q>+ ,  %uV$!%U 4 5  Q  (%(99M88L)**;=   (%(988F#$$57  |@nKGs5 A@*$AA0AB65AB;AC@*AAA-A0AAB3cb|jj|jd|jz |jd|jz|jd|jz |jd|jzg|jj dd|j S)a Adjust the Axes and return a list of information about the Sankey subdiagram(s). Returns a list of subdiagrams with the following fields: ======== ============================================================= Field Description ======== ============================================================= *patch* Sankey outline (a `~matplotlib.patches.PathPatch`). *flows* Flow values (positive for input, negative for output). *angles* List of angles of the arrows [deg/90]. For example, if the diagram has not been rotated, an input to the top side has an angle of 3 (DOWN), and an output from the top side has an angle of 1 (UP). If a flow has been skipped (because its magnitude is less than *tolerance*), then its angle will be *None*. *tips* (N, 2)-array of the (x, y) positions of the tips (or "dips") of the flow paths. If the magnitude of a flow is less the *tolerance* of this `Sankey` instance, the flow is skipped and its tip will be at the center of the diagram. *text* `.Text` instance for the diagram label. *texts* List of `.Text` instances for the flow labels. ======== ============================================================= See Also -------- Sankey.add rr r5r equaldatalim) adjustable)raxisr(r set_aspectr)r+s r0finishz Sankey.finish s>  dkk!nt{{2kk!nt{{2kk!nt{{2kk!nt{{24 5 7y9}}r2) Nr4rzz%G?g?gQ?g333333?dg?gư>)rTr rr) rzNNrzr4rNrr)__name__ __module__ __qualname____doc__r1rDrVrZrr7rarinterpdr*rrr2r0r r sg FJDG'+}~7Fr2'h4'l*. $GICIiiV $r2r )rloggingtypesrnumpyr! matplotlibrmatplotlib.pathrmatplotlib.patchesrmatplotlib.transformsrr getLoggerrr __author__ __credits__ __license__ __version__rGrIrJr rr2r0rsj! (*!w"      P P r2