Skip to content

Commit 33fef83

Browse files
committed
Cleanups: Broadcasting, np.hypot, np.pi, etc.
Also fixes a bug in fill_between with masked data. In the modified test figures, the area in green is supposed to correspond to the part of the hatched area where the curve is below y=2. The new behavior is the correct one. Also fixes cbook._reshape2D for scalar object inputs. Before the fix, `plt.hist(None)` would fail with `x must have 2 or fewer dimensions`, which it does have. Now it fails with a bit later with `unsupported operands type(s) for +: 'NoneType' and 'float'`, which is hopefully clearer.
1 parent c4d7ce4 commit 33fef83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+326
-525
lines changed

doc/api/axis_api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ Ticks, tick labels and Offset text
9999
Axis.axis_date
100100

101101

102-
Data and view internvals
103-
------------------------
102+
Data and view intervals
103+
-----------------------
104104

105105
.. autosummary::
106106
:toctree: _as_gen

examples/animation/unchained.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# Generate random data
2626
data = np.random.uniform(0, 1, (64, 75))
2727
X = np.linspace(-1, 1, data.shape[-1])
28-
G = 1.5 * np.exp(-4 * X * X)
28+
G = 1.5 * np.exp(-4 * X ** 2)
2929

3030
# Generate line plots
3131
lines = []

examples/api/custom_projection_example.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,15 +434,11 @@ def __init__(self, resolution):
434434
self._resolution = resolution
435435

436436
def transform_non_affine(self, xy):
437-
x = xy[:, 0:1]
438-
y = xy[:, 1:2]
439-
440-
quarter_x = 0.25 * x
441-
half_y = 0.5 * y
442-
z = np.sqrt(1.0 - quarter_x*quarter_x - half_y*half_y)
443-
longitude = 2 * np.arctan((z*x) / (2.0 * (2.0*z*z - 1.0)))
437+
x, y = xy.T
438+
z = np.sqrt(1 - (x / 4) ** 2 - (y / 2) ** 2)
439+
longitude = 2 * np.arctan((z * x) / (2 * (2 * z ** 2 - 1)))
444440
latitude = np.arcsin(y*z)
445-
return np.concatenate((longitude, latitude), 1)
441+
return np.column_stack([longitude, latitude])
446442
transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__
447443

448444
def inverted(self):

examples/api/scatter_piecharts.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
Thanks to Manuel Metz for the example
99
"""
10-
import math
10+
1111
import numpy as np
1212
import matplotlib.pyplot as plt
1313

@@ -16,35 +16,33 @@
1616
r2 = r1 + 0.4 # 40%
1717

1818
# define some sizes of the scatter marker
19-
sizes = [60, 80, 120]
19+
sizes = np.array([60, 80, 120])
2020

2121
# calculate the points of the first pie marker
2222
#
2323
# these are just the origin (0,0) +
2424
# some points on a circle cos,sin
25-
x = [0] + np.cos(np.linspace(0, 2*math.pi*r1, 10)).tolist()
26-
y = [0] + np.sin(np.linspace(0, 2*math.pi*r1, 10)).tolist()
27-
25+
x = [0] + np.cos(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
26+
y = [0] + np.sin(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
2827
xy1 = list(zip(x, y))
29-
s1 = max(max(x), max(y))
28+
s1 = np.max(xy1)
3029

31-
# ...
32-
x = [0] + np.cos(np.linspace(2*math.pi*r1, 2*math.pi*r2, 10)).tolist()
33-
y = [0] + np.sin(np.linspace(2*math.pi*r1, 2*math.pi*r2, 10)).tolist()
30+
x = [0] + np.cos(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
31+
y = [0] + np.sin(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
3432
xy2 = list(zip(x, y))
35-
s2 = max(max(x), max(y))
33+
s2 = np.max(xy2)
3634

37-
x = [0] + np.cos(np.linspace(2*math.pi*r2, 2*math.pi, 10)).tolist()
38-
y = [0] + np.sin(np.linspace(2*math.pi*r2, 2*math.pi, 10)).tolist()
35+
x = [0] + np.cos(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
36+
y = [0] + np.sin(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
3937
xy3 = list(zip(x, y))
40-
s3 = max(max(x), max(y))
38+
s3 = np.max(xy3)
4139

4240
fig, ax = plt.subplots()
43-
ax.scatter(np.arange(3), np.arange(3), marker=(xy1, 0),
44-
s=[s1*s1*_ for _ in sizes], facecolor='blue')
45-
ax.scatter(np.arange(3), np.arange(3), marker=(xy2, 0),
46-
s=[s2*s2*_ for _ in sizes], facecolor='green')
47-
ax.scatter(np.arange(3), np.arange(3), marker=(xy3, 0),
48-
s=[s3*s3*_ for _ in sizes], facecolor='red')
41+
ax.scatter(range(3), range(3), marker=(xy1, 0),
42+
s=s1 ** 2 * sizes, facecolor='blue')
43+
ax.scatter(range(3), range(3), marker=(xy2, 0),
44+
s=s2 ** 2 * sizes, facecolor='green')
45+
ax.scatter(range(3), range(3), marker=(xy3, 0),
46+
s=s3 ** 2 * sizes, facecolor='red')
4947

5048
plt.show()

examples/event_handling/timers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def update_title(axes):
1818
fig, ax = plt.subplots()
1919

2020
x = np.linspace(-3, 3)
21-
ax.plot(x, x*x)
21+
ax.plot(x, x ** 2)
2222

2323
# Create a new timer object. Set the interval to 100 milliseconds
2424
# (1000 is default) and tell the timer what function should be called.

examples/event_handling/trifinder_event_demo.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111
from matplotlib.tri import Triangulation
1212
from matplotlib.patches import Polygon
1313
import numpy as np
14-
import math
1514

1615

1716
def update_polygon(tri):
1817
if tri == -1:
1918
points = [0, 0, 0]
2019
else:
21-
points = triangulation.triangles[tri]
22-
xs = triangulation.x[points]
23-
ys = triangulation.y[points]
20+
points = triang.triangles[tri]
21+
xs = triang.x[points]
22+
ys = triang.y[points]
2423
polygon.set_xy(list(zip(xs, ys)))
2524

2625

@@ -39,23 +38,22 @@ def motion_notify(event):
3938
n_radii = 5
4039
min_radius = 0.25
4140
radii = np.linspace(min_radius, 0.95, n_radii)
42-
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
41+
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
4342
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
44-
angles[:, 1::2] += math.pi / n_angles
43+
angles[:, 1::2] += np.pi / n_angles
4544
x = (radii*np.cos(angles)).flatten()
4645
y = (radii*np.sin(angles)).flatten()
47-
triangulation = Triangulation(x, y)
48-
xmid = x[triangulation.triangles].mean(axis=1)
49-
ymid = y[triangulation.triangles].mean(axis=1)
50-
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
51-
triangulation.set_mask(mask)
46+
triang = Triangulation(x, y)
47+
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
48+
y[triang.triangles].mean(axis=1))
49+
< min_radius)
5250

5351
# Use the triangulation's default TriFinder object.
54-
trifinder = triangulation.get_trifinder()
52+
trifinder = triang.get_trifinder()
5553

5654
# Setup plot and callbacks.
5755
plt.subplot(111, aspect='equal')
58-
plt.triplot(triangulation, 'bo-')
56+
plt.triplot(triang, 'bo-')
5957
polygon = Polygon([[0, 0], [0, 0]], facecolor='y') # dummy data for xs,ys
6058
update_polygon(-1)
6159
plt.gca().add_patch(polygon)

examples/images_contours_and_fields/tricontour_demo.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import matplotlib.pyplot as plt
99
import matplotlib.tri as tri
1010
import numpy as np
11-
import math
1211

1312
###############################################################################
1413
# Creating a Triangulation without specifying the triangles results in the
@@ -20,22 +19,21 @@
2019
min_radius = 0.25
2120
radii = np.linspace(min_radius, 0.95, n_radii)
2221

23-
angles = np.linspace(0, 2 * math.pi, n_angles, endpoint=False)
22+
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
2423
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
25-
angles[:, 1::2] += math.pi / n_angles
24+
angles[:, 1::2] += np.pi / n_angles
2625

2726
x = (radii * np.cos(angles)).flatten()
2827
y = (radii * np.sin(angles)).flatten()
29-
z = (np.cos(radii) * np.cos(angles * 3.0)).flatten()
28+
z = (np.cos(radii) * np.cos(3 * angles)).flatten()
3029

3130
# Create the Triangulation; no triangles so Delaunay triangulation created.
3231
triang = tri.Triangulation(x, y)
3332

3433
# Mask off unwanted triangles.
35-
xmid = x[triang.triangles].mean(axis=1)
36-
ymid = y[triang.triangles].mean(axis=1)
37-
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
38-
triang.set_mask(mask)
34+
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
35+
y[triang.triangles].mean(axis=1))
36+
< min_radius)
3937

4038
###############################################################################
4139
# pcolor plot.

examples/images_contours_and_fields/tricontour_smooth_user.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import matplotlib.pyplot as plt
1111
import matplotlib.cm as cm
1212
import numpy as np
13-
import math
1413

1514

1615
#-----------------------------------------------------------------------------
@@ -36,9 +35,9 @@ def function_z(x, y):
3635
min_radius = 0.15
3736
radii = np.linspace(min_radius, 0.95, n_radii)
3837

39-
angles = np.linspace(0, 2 * math.pi, n_angles, endpoint=False)
38+
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
4039
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
41-
angles[:, 1::2] += math.pi / n_angles
40+
angles[:, 1::2] += np.pi / n_angles
4241

4342
x = (radii * np.cos(angles)).flatten()
4443
y = (radii * np.sin(angles)).flatten()
@@ -50,10 +49,9 @@ def function_z(x, y):
5049
triang = tri.Triangulation(x, y)
5150

5251
# Mask off unwanted triangles.
53-
xmid = x[triang.triangles].mean(axis=1)
54-
ymid = y[triang.triangles].mean(axis=1)
55-
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
56-
triang.set_mask(mask)
52+
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
53+
y[triang.triangles].mean(axis=1))
54+
< min_radius)
5755

5856
#-----------------------------------------------------------------------------
5957
# Refine data

examples/images_contours_and_fields/trigradient_demo.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
66
Demonstrates computation of gradient with matplotlib.tri.CubicTriInterpolator.
77
"""
8-
from matplotlib.tri import Triangulation, UniformTriRefiner,\
9-
CubicTriInterpolator
8+
from matplotlib.tri import (
9+
Triangulation, UniformTriRefiner, CubicTriInterpolator)
1010
import matplotlib.pyplot as plt
1111
import matplotlib.cm as cm
1212
import numpy as np
13-
import math
1413

1514

1615
#-----------------------------------------------------------------------------
@@ -33,9 +32,9 @@ def dipole_potential(x, y):
3332
min_radius = 0.2
3433
radii = np.linspace(min_radius, 0.95, n_radii)
3534

36-
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
35+
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
3736
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
38-
angles[:, 1::2] += math.pi/n_angles
37+
angles[:, 1::2] += np.pi / n_angles
3938

4039
x = (radii*np.cos(angles)).flatten()
4140
y = (radii*np.sin(angles)).flatten()
@@ -46,10 +45,9 @@ def dipole_potential(x, y):
4645
triang = Triangulation(x, y)
4746

4847
# Mask off unwanted triangles.
49-
xmid = x[triang.triangles].mean(axis=1)
50-
ymid = y[triang.triangles].mean(axis=1)
51-
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
52-
triang.set_mask(mask)
48+
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
49+
y[triang.triangles].mean(axis=1))
50+
< min_radius)
5351

5452
#-----------------------------------------------------------------------------
5553
# Refine data - interpolates the electrical potential V

examples/images_contours_and_fields/tripcolor_demo.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import matplotlib.pyplot as plt
99
import matplotlib.tri as tri
1010
import numpy as np
11-
import math
1211

1312
###############################################################################
1413
# Creating a Triangulation without specifying the triangles results in the
@@ -20,22 +19,21 @@
2019
min_radius = 0.25
2120
radii = np.linspace(min_radius, 0.95, n_radii)
2221

23-
angles = np.linspace(0, 2 * math.pi, n_angles, endpoint=False)
22+
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
2423
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
25-
angles[:, 1::2] += math.pi / n_angles
24+
angles[:, 1::2] += np.pi / n_angles
2625

2726
x = (radii * np.cos(angles)).flatten()
2827
y = (radii * np.sin(angles)).flatten()
29-
z = (np.cos(radii) * np.cos(angles * 3.0)).flatten()
28+
z = (np.cos(radii) * np.cos(3 * angles)).flatten()
3029

3130
# Create the Triangulation; no triangles so Delaunay triangulation created.
3231
triang = tri.Triangulation(x, y)
3332

3433
# Mask off unwanted triangles.
35-
xmid = x[triang.triangles].mean(axis=1)
36-
ymid = y[triang.triangles].mean(axis=1)
37-
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
38-
triang.set_mask(mask)
34+
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
35+
y[triang.triangles].mean(axis=1))
36+
< min_radius)
3937

4038
###############################################################################
4139
# tripcolor plot.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy