2222
2323from collections .abc import Callable , Mapping , Sequence
2424from dataclasses import dataclass , field
25- from typing import TYPE_CHECKING , Any
25+ from typing import TYPE_CHECKING , Any , Literal
2626
2727import matplotlib as mpl
2828import matplotlib .colors as mcolors
@@ -162,6 +162,8 @@ def diagram_box(
162162 linewidth : float = 0.5 ,
163163 rounding : float = 0.05 ,
164164 textcolor : Any = None ,
165+ linestyle : Any = "-" ,
166+ hatch : str | None = None ,
165167 zorder : float = 2.0 ,
166168 ** text_kw : Any ,
167169) -> FancyBboxPatch :
@@ -178,6 +180,8 @@ def diagram_box(
178180 linewidth: Outline width (pt).
179181 rounding: Corner radius, in data units; 0 gives square corners.
180182 textcolor: Text colour; by default dark or white, whichever reads on ``facecolor``.
183+ linestyle: Outline style, e.g. ``"--"`` for a dashed frame around a group.
184+ hatch: Hatch pattern of the fill, e.g. ``"//"``.
181185 zorder: Drawing order of the box; its text draws above.
182186 **text_kw: Forwarded to :meth:`~matplotlib.axes.Axes.text`, e.g. ``fontsize`` or
183187 ``fontweight``.
@@ -190,10 +194,12 @@ def diagram_box(
190194 (x - width / 2 , y - height / 2 ),
191195 width ,
192196 height ,
193- boxstyle = f"round,pad=0,rounding_size={ rounding } " ,
197+ boxstyle = f"round,pad=0,rounding_size={ rounding } " if rounding > 0 else "square,pad=0" ,
194198 facecolor = facecolor ,
195199 edgecolor = edgecolor ,
196200 linewidth = linewidth ,
201+ linestyle = linestyle ,
202+ hatch = hatch ,
197203 zorder = zorder ,
198204 gid = "box" ,
199205 )
@@ -214,49 +220,96 @@ def diagram_arrow(
214220 color : Any = None ,
215221 head_width : float | None = None ,
216222 head_length : float | None = None ,
223+ style : Literal ["filled" , "line" ] = "filled" ,
224+ via : Sequence [tuple [float , float ]] | np .ndarray | None = None ,
225+ linestyle : Any = "-" ,
217226 label : str = "" ,
218227 label_offset : tuple [float , float ] = (0.0 , 0.0 ),
219228 zorder : float = 3.0 ,
220229 label_kw : Mapping [str , Any ] | None = None ,
221230 ** kwargs : Any ,
222231) -> FancyArrowPatch :
223- """Draw a filled arrow from ``tail`` to ``head``, its shaft ``width`` points wide.
232+ """Draw an arrow from ``tail`` to ``head``, its shaft ``width`` points wide.
224233
225- Widths are physical, so arrows keep their shape on axes of any aspect.
234+ Widths are physical, so arrows keep their shape on axes of any aspect. A ``"filled"``
235+ arrow is one solid shape, as a flux whose width carries a value; a ``"line"`` arrow is a
236+ stroked line with a filled head, the connector of a flowchart. With ``via`` the arrow
237+ runs through those points with right-angled or any other corners, and only its last
238+ segment carries the head. Either way the tip ends exactly on ``head``.
226239
227240 Args:
228241 ax: Axes to draw on.
229242 tail: Start, in data coordinates.
230243 head: Tip, in data coordinates.
231- width: Shaft width (pt).
244+ width: Shaft width (pt); the line width of a ``"line"`` arrow .
232245 color: Fill colour; the text colour by default.
233- head_width: Head width (pt); by default ``max(3, 2.4 * width)``.
234- head_length: Head length (pt); by default ``max(2.6, 1.5 * width)``.
235- label: Text at the arrow's midpoint.
246+ head_width: Head width (pt); by default ``max(3, 2.4 * width)`` for a filled arrow
247+ and ``max(2.4, 3 * width)`` for a line arrow.
248+ head_length: Head length (pt); by default ``max(2.6, 1.5 * width)`` for a filled
249+ arrow and ``max(2.4, 3 * width)`` for a line arrow.
250+ style: ``"filled"`` or ``"line"``.
251+ via: Points the arrow passes through between ``tail`` and ``head``, in data
252+ coordinates.
253+ linestyle: Line style of a ``"line"`` arrow's shaft, e.g. ``"--"``.
254+ label: Text at the midpoint of the arrow's last segment.
236255 label_offset: ``(dx, dy)`` of the label from the midpoint (pt).
237256 zorder: Drawing order; the label draws above.
238257 label_kw: Forwarded to :meth:`~matplotlib.axes.Axes.annotate` for the label,
239258 e.g. ``ha``, ``fontsize`` or ``color``.
240259 **kwargs: Forwarded to :class:`~matplotlib.patches.FancyArrowPatch`, e.g.
241- ``connectionstyle="arc3,rad=0.2"`` for a curved arrow or ``gid``.
260+ ``connectionstyle="arc3,rad=0.2"`` for a curved arrow, ``gid``, or
261+ ``joinstyle="round"`` and ``capstyle="round"`` for a softer line arrow
262+ (mitred and butt by default).
242263
243264 Returns:
244- The arrow patch.
265+ The patch of the arrow's last segment, which carries the head.
266+
267+ Raises:
268+ ValueError: If ``style`` is neither ``"filled"`` nor ``"line"``.
245269 """
246- hw = max ( 3.0 , 2.4 * width ) if head_width is None else head_width
247- hl = max ( 2.6 , 1.5 * width ) if head_length is None else head_length
270+ if style not in ( "filled" , "line" ):
271+ raise ValueError ( f"style must be 'filled' or 'line', got { style !r } " )
248272 color = mpl .rcParams ["text.color" ] if color is None else color
273+ if via is not None and len (via ):
274+ corners = [tail , * (tuple (p ) for p in via )]
275+ xs , ys = zip (* corners , strict = True )
276+ ax .plot (
277+ xs ,
278+ ys ,
279+ color = color ,
280+ linewidth = width ,
281+ linestyle = linestyle ,
282+ solid_capstyle = "butt" ,
283+ solid_joinstyle = "miter" ,
284+ zorder = zorder ,
285+ )
286+ tail = corners [- 1 ]
287+ if style == "filled" :
288+ hw = max (3.0 , 2.4 * width ) if head_width is None else head_width
289+ hl = max (2.6 , 1.5 * width ) if head_length is None else head_length
290+ arrowstyle = ArrowStyle .Simple (head_length = hl , head_width = hw , tail_width = width )
291+ line_kw : dict [str , Any ] = {"linewidth" : 0.0 }
292+ else :
293+ hl = max (2.4 , 3.0 * width ) if head_length is None else head_length
294+ hw = hl if head_width is None else head_width
295+ # Matplotlib's "-|>" head width is measured from the shaft to one side.
296+ arrowstyle = ArrowStyle ("-|>" , head_length = hl , head_width = hw / 2 )
297+ line_kw = {
298+ "linewidth" : width ,
299+ "linestyle" : linestyle ,
300+ "joinstyle" : "miter" ,
301+ "capstyle" : "butt" ,
302+ }
249303 patch = FancyArrowPatch (
250304 tail ,
251305 head ,
252- arrowstyle = ArrowStyle . Simple ( head_length = hl , head_width = hw , tail_width = width ) ,
306+ arrowstyle = arrowstyle ,
253307 mutation_scale = 1.0 ,
254308 shrinkA = 0.0 ,
255309 shrinkB = 0.0 ,
256310 color = color ,
257- linewidth = 0.0 ,
258311 zorder = zorder ,
259- ** kwargs ,
312+ ** { ** line_kw , ** kwargs } ,
260313 )
261314 ax .add_patch (patch )
262315 if label :
0 commit comments