8000 Merge pull request #849 from tonysyu/streamplot-norm-param · matplotlib/matplotlib@342c028 · GitHub
[go: up one dir, main page]

Skip to content

Commit 342c028

Browse files
committed
Merge pull request #849 from tonysyu/streamplot-norm-param
Add `norm` parameter to `streamplot`.
2 parents 3d19a08 + 7d435d4 commit 342c028

File tree

3 files changed

+24
-17
lines changed
  • lib/matplotlib
    • < 10000 div id=":R1dtddab:" class="PRIVATE_TreeView-item-content prc-TreeView-TreeViewItemContent-f0r0b">axes.py
  • 3 files changed

    +24
    -17
    lines changed

    lib/matplotlib/axes.py

    Lines changed: 3 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -6389,13 +6389,15 @@ def quiver(self, *args, **kw):
    63896389
    quiver.__doc__ = mquiver.Quiver.quiver_doc
    63906390

    63916391
    def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
    6392-
    cmap=None, arrowsize=1, arrowstyle='-|>', minlength=0.1):
    6392+
    cmap=None, norm=None, arrowsize=1, arrowstyle='-|>',
    6393+
    minlength=0.1):
    63936394
    if not self._hold: self.cla()
    63946395
    lines = mstream.streamplot(self, x, y, u, v,
    63956396
    density=density,
    63966397
    linewidth=linewidth,
    63976398
    color=color,
    63986399
    cmap=cmap,
    6400+
    norm=norm,
    63996401
    arrowsize=arrowsize,
    64006402
    arrowstyle=arrowstyle,
    64016403
    minlength=minlength)

    lib/matplotlib/pyplot.py

    Lines changed: 2 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -2658,15 +2658,15 @@ def step(x, y, *args, **kwargs):
    26582658
    # This function was autogenerated by boilerplate.py. Do not edit as
    26592659
    # changes will be lost
    26602660
    @autogen_docstring(Axes.streamplot)
    2661-
    def streamplot(x, y, u, v, density=1, linewidth=None, color=None, cmap=None, arrowsize=1, arrowstyle='-|>', minlength=0.10000000000000001, hold=None):
    2661+
    def streamplot(x, y, u, v, density=1, linewidth=None, color=None, cmap=None, norm=None, arrowsize=1, arrowstyle='-|>', minlength=0.10000000000000001, hold=None):
    26622662
    ax = gca()
    26632663
    # allow callers to override the hold state by passing hold=True|False
    26642664
    washold = ax.ishold()
    26652665

    26662666
    if hold is not None:
    26672667
    ax.hold(hold)
    26682668
    try:
    2669-
    ret = ax.streamplot(x, y, u, v, density, linewidth, color, cmap, arrowsize, arrowstyle, minlength)
    2669+
    ret = ax.streamplot(x, y, u, v, density, linewidth, color, cmap, norm, arrowsize, arrowstyle, minlength)
    26702670
    draw_if_interactive()
    26712671
    finally:
    26722672
    ax.hold(washold)

    lib/matplotlib/streamplot.py

    Lines changed: 19 additions & 14 deletions
    Original file line numberDiff line numberDiff line change
    @@ -11,7 +11,8 @@
    1111

    1212

    1313
    def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
    14-
    cmap=None, arrowsize=1, arrowstyle='-|>', minlength=0.1):
    14+
    cmap=None, norm=None, arrowsize=1, arrowstyle='-|>',
    15+
    minlength=0.1):
    1516
    """Draws streamlines of a vector flow.
    1617
    1718
    Parameters
    @@ -31,19 +32,23 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
    3132
    *color* : matplotlib color code, or 2d array
    3233
    Streamline color. When given an array with the same shape as
    3334
    velocities, *color* values are converted to colors using *cmap*.
    34-
    *cmap* : Colormap
    35+
    *cmap* : :class:`~matplotlib.colors.Colormap`
    3536
    Colormap used to plot streamlines and arrows. Only necessary when using
    3637
    an array input for *color*.
    38+
    *norm* : :class:`~matplotlib.colors.Normalize`
    39+
    Normalize object used to scale luminance data to 0, 1. If None, stretch
    40+
    (min, max) to (0, 1). Only necessary when *color* is an array.
    3741
    *arrowsize* : float
    3842
    Factor scale arrow size.
    3943
    *arrowstyle* : str
    40-
    Arrow style specification. See `matplotlib.patches.FancyArrowPatch`.
    44+
    Arrow style specification.
    45+
    See :class:`~matplotlib.patches.FancyArrowPatch`.
    4146
    *minlength* : float
    4247
    Minimum length of streamline in axes coordinates.
    4348
    4449
    Returns
    4550
    -------
    46-
    *streamlines* : `matplotlib.collections.LineCollection`
    51+
    *streamlines* : :class:`~matplotlib.collections.LineCollection`
    4752
    Line collection with all streamlines as a series of line segments.
    4853
    Currently, there is no way to differentiate between line segments
    4954
    on different streamlines (other than manually checking that segments
    @@ -62,7 +67,8 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
    6267
    line_kw = {}
    6368
    arrow_kw = dict(arrowstyle=arrowstyle, mutation_scale=10*arrowsize)
    6469

    65-
    if isinstance(color, np.ndarray):
    70+
    use_multicolor_lines = isinstance(color, np.ndarray)
    71+
    if use_multicolor_lines:
    6672
    assert color.shape == grid.shape
    6773
    line_colors = []
    6874
    else:
    @@ -95,12 +101,11 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
    95101
    if t != None:
    96102
    trajectories.append(t)
    97103

    98-
    # Load up the defaults - needed to get the color right.
    99-
    if isinstance(color, np.ndarray):
    100-
    norm = matplotlib.colors.normalize(color.min(), color.max())
    101-
    if cmap == None: cmap = matplotlib.cm.get_cmap(
    102-
    matplotlib.rcParams['image.cmap'])
    103-
    104+
    if use_multicolor_lines:
    105+
    if norm is None:
    106+
    norm = matplotlib.colors.normalize(color.min(), color.max())
    107+
    if cmap is None:
    108+
    cmap = matplotlib.cm.get_cmap(matplotlib.rcParams['image.cmap'])
    104109

    105110
    streamlines = []
    106111
    for t in trajectories:
    @@ -113,7 +118,7 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
    113118
    points = np.transpose([tx, ty]).reshape(-1, 1, 2)
    114119
    streamlines.extend(np.hstack([points[:-1], points[1:]]))
    115120

    116-
    ## Add arrows half way along each trajectory.
    121+
    # Add arrows half way along each trajectory.
    117122
    s = np.cumsum(np.sqrt(np.diff(tx)**2 + np.diff(ty)**2))
    118123
    n = np.searchsorted(s, s[-1] / 2.)
    119124
    arrow_tail = (tx[n], ty[n])
    @@ -124,7 +129,7 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
    124129
    line_kw['linewidth'].extend(line_widths)
    125130
    arrow_kw['linewidth'] = line_widths[n]
    126131

    127-
    if isinstance(color, np.ndarray):
    132+
    if use_multicolor_lines:
    128133
    color_values = interpgrid(color, tgx, tgy)[:-1]
    129134
    line_colors.extend(color_values)
    130135
    arrow_kw['color'] = cmap(norm(color_values[n]))
    @@ -133,7 +138,7 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
    133138
    axes.add_patch(p)
    134139

    135140
    lc = matplotlib.collections.LineCollection(streamlines, **line_kw)
    136-
    if isinstance(color, np.ndarray):
    141+
    if use_multicolor_lines:
    137142
    lc.set_array(np.asarray(line_colors))
    138143
    lc.set_cmap(cmap)
    139144
    lc.set_norm(norm)

    0 commit comments

    Comments
     (0)
    0