11
11
12
12
13
13
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 ):
15
16
"""Draws streamlines of a vector flow.
16
17
17
18
Parameters
@@ -31,19 +32,23 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
31
32
*color* : matplotlib color code, or 2d array
32
33
Streamline color. When given an array with the same shape as
33
34
velocities, *color* values are converted to colors using *cmap*.
34
- *cmap* : Colormap
35
+ *cmap* : :class:`~matplotlib.colors. Colormap`
35
36
Colormap used to plot streamlines and arrows. Only necessary when using
36
37
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.
37
41
*arrowsize* : float
38
42
Factor scale arrow size.
39
43
*arrowstyle* : str
40
- Arrow style specification. See `matplotlib.patches.FancyArrowPatch`.
44
+ Arrow style specification.
45
+ See :class:`~matplotlib.patches.FancyArrowPatch`.
41
46
*minlength* : float
42
47
Minimum length of streamline in axes coordinates.
43
48
44
49
Returns
45
50
-------
46
- *streamlines* : ` matplotlib.collections.LineCollection`
51
+ *streamlines* : :class:`~ matplotlib.collections.LineCollection`
47
52
Line collection with all streamlines as a series of line segments.
48
53
Currently, there is no way to differentiate between line segments
49
54
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,
62
67
line_kw = {}
63
68
arrow_kw = dict (arrowstyle = arrowstyle , mutation_scale = 10 * arrowsize )
64
69
65
- if isinstance (color , np .ndarray ):
70
+ use_multicolor_lines = isinstance (color , np .ndarray )
71
+ if use_multicolor_lines :
66
72
assert color .shape == grid .shape
67
73
line_colors = []
68
74
else :
@@ -95,12 +101,11 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
95
101
if t != None :
96
102
trajectories .append (t )
97
103
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' ])
104
109
105
110
streamlines = []
106
111
for t in trajectories :
@@ -113,7 +118,7 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
113
118
points = np .transpose ([tx , ty ]).reshape (- 1 , 1 , 2 )
114
119
streamlines .extend (np .hstack ([points [:- 1 ], points [1 :]]))
115
120
116
- ## Add arrows half way along each trajectory.
121
+ # Add arrows half way along each trajectory.
117
122
s = np .cumsum (np .sqrt (np .diff (tx )** 2 + np .diff (ty )** 2 ))
118
123
n = np .searchsorted (s , s [- 1 ] / 2. )
119
124
9E12
code>
arrow_tail = (tx [n ], ty [n ])
@@ -124,7 +129,7 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
124
129
line_kw ['linewidth' ].extend (line_widths )
125
130
arrow_kw ['linewidth' ] = line_widths [n ]
126
131
127
- if isinstance ( color , np . ndarray ) :
132
+ if use_multicolor_lines :
128
133
color_values = interpgrid (color , tgx , tgy )[:- 1 ]
129
134
line_colors .extend (color_values )
130
135
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,
133
138
axes .add_patch (p )
134
139
135
140
lc = matplotlib .collections .LineCollection (streamlines , ** line_kw )
136
- if isinstance ( color , np . ndarray ) :
141
+ if use_multicolor_lines :
137
142
lc .set_array (np .asarray (line_colors ))
138
143
lc .set_cmap (cmap )
139
144
lc .set_norm (norm )
0 commit comments