Skip to content

Commit dfa77b9

Browse files
committed
Cleanups: Broadcasting, np.hypot, np.pi, etc.
1 parent bf5aeb5 commit dfa77b9

Some content is hidden

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

50 files changed

+193
-330
lines changed

doc/api/axis_api.rst

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

9797

98-
Data and view internvals
99-
------------------------
98+
Data and view intervals
99+
-----------------------
100100

101101
.. autosummary::
102102
: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
@@ -435,15 +435,11 @@ def __init__(self, resolution):
435435
self._resolution = resolution
436436

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

449445
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
@@ -12,7 +12,7 @@ def update_title(axes):
1212
fig, ax = plt.subplots()
1313

1414
x = np.linspace(-3, 3)
15-
ax.plot(x, x*x)
15+
ax.plot(x, x ** 2)
1616

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

examples/event_handling/trifinder_event_demo.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from matplotlib.tri import Triangulation
88
from matplotlib.patches import Polygon
99
import numpy as np
10-
import math
1110

1211

1312
def update_polygon(tri):
@@ -35,16 +34,15 @@ def motion_notify(event):
3534
n_radii = 5
3635
min_radius = 0.25
3736
radii = np.linspace(min_radius, 0.95, n_radii)
38-
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
37+
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
3938
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
40-
angles[:, 1::2] += math.pi / n_angles
39+
angles[:, 1::2] += np.pi / n_angles
4140
x = (radii*np.cos(angles)).flatten()
4241
y = (radii*np.sin(angles)).flatten()
4342
triangulation = Triangulation(x, y)
44-
xmid = x[triangulation.triangles].mean(axis=1)
45-
ymid = y[triangulation.triangles].mean(axis=1)
46-
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
47-
triangulation.set_mask(mask)
43+
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
44+
y[triang.triangles].mean(axis=1))
45+
< min_radius)
4846

4947
# Use the triangulation's default TriFinder object.
5048
trifinder = triangulation.get_trifinder()

examples/mplot3d/tricontour3d_demo.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@
2626

2727
x = (radii*np.cos(angles)).flatten()
2828
y = (radii*np.sin(angles)).flatten()
29-
z = (np.cos(radii)*np.cos(angles*3.0)).flatten()
29+
z = (np.cos(radii)*np.cos(3*angles)).flatten()
3030

3131
# Create a custom triangulation.
3232
triang = tri.Triangulation(x, y)
3333

3434
# 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)
35+
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
36+
y[triang.triangles].mean(axis=1))
37+
< min_radius)
3938

4039
fig = plt.figure()
4140
ax = fig.gca(projection='3d')

examples/mplot3d/tricontourf3d_demo.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@
2727

2828
x = (radii*np.cos(angles)).flatten()
2929
y = (radii*np.sin(angles)).flatten()
30-
z = (np.cos(radii)*np.cos(angles*3.0)).flatten()
30+
z = (np.cos(radii)*np.cos(3*angles)).flatten()
3131

3232
# Create a custom triangulation.
3333
triang = tri.Triangulation(x, y)
3434

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

4140
fig = plt.figure()
4241
ax = fig.gca(projection='3d')

examples/mplot3d/trisurf3d_demo2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
# Map radius, angle pairs to x, y, z points.
6262
x = (radii*np.cos(angles)).flatten()
6363
y = (radii*np.sin(angles)).flatten()
64-
z = (np.cos(radii)*np.cos(angles*3.0)).flatten()
64+
z = (np.cos(radii)*np.cos(3*angles)).flatten()
6565

6666
# Create the Triangulation; no triangles so Delaunay triangulation created.
6767
triang = mtri.Triangulation(x, y)

examples/pylab_examples/annotation_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
fig = plt.figure()
9292
ax = fig.add_subplot(111, projection='polar')
9393
r = np.arange(0, 1, 0.001)
94-
theta = 2*2*np.pi*r
94+
theta = 4 * np.pi * r
9595
line, = ax.plot(theta, r)
9696

9797
ind = 800

0 commit comments

Comments
 (0)