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

+2-2
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

+1-1
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

+4-8
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

+17-19
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

+1-1
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

+5-7
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

+4-5
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

+4-5
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

+1-1
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

+1-1
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

examples/pylab_examples/cursor_demo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def mouse_move(self, event):
7474
plt.draw()
7575

7676
t = np.arange(0.0, 1.0, 0.01)
77-
s = np.sin(2*2*np.pi*t)
77+
s = np.sin(4 * np.pi * t)
7878
fig, ax = plt.subplots()
7979

8080
#cursor = Cursor(ax)

examples/pylab_examples/date_demo_convert.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import datetime
22
import matplotlib.pyplot as plt
33
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
4-
from numpy import arange
4+
import numpy as np
55

66
date1 = datetime.datetime(2000, 3, 2)
77
date2 = datetime.datetime(2000, 3, 6)
88
delta = datetime.timedelta(hours=6)
99
dates = drange(date1, date2, delta)
1010

11-
y = arange(len(dates)*1.0)
11+
y = np.arange(len(dates))
1212

1313
fig, ax = plt.subplots()
14-
ax.plot_date(dates, y*y)
14+
ax.plot_date(dates, y ** 2)
1515

1616
# this is superfluous, since the autoscaler should get it right, but
1717
# use date2num and num2date to convert between dates and floats if
@@ -22,7 +22,7 @@
2222
# tick, not the base multiple
2323

2424
ax.xaxis.set_major_locator(DayLocator())
25-
ax.xaxis.set_minor_locator(HourLocator(arange(0, 25, 6)))
25+
ax.xaxis.set_minor_locator(HourLocator(range(0, 25, 6)))
2626
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
2727

2828
ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S')

examples/pylab_examples/equal_aspect_ratio.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77

88
t = np.arange(0.0, 1.0 + 0.01, 0.01)
9-
s = np.cos(2*2*np.pi*t)
9+
s = np.cos(4 * np.pi * t)
1010
plt.plot(t, s, '-', lw=2)
1111

1212
plt.xlabel('time (s)')
@@ -16,5 +16,4 @@
1616

1717
plt.axes().set_aspect('equal', 'datalim')
1818

19-
2019
plt.show()

examples/pylab_examples/fancybox_demo2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
styles = mpatch.BoxStyle.get_styles()
55
spacing = 1.2
66

7-
figheight = (spacing * len(styles) + .5)
7+
figheight = spacing * len(styles) + .5
88
fig1 = plt.figure(1, (4/1.5, figheight/1.5))
99
fontsize = 0.3 * 72
1010

examples/pylab_examples/multipage_pdf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
plt.rc('text', usetex=False)
3333
fig = plt.figure(figsize=(4, 5))
34-
plt.plot(x, x*x, 'ko')
34+
plt.plot(x, x ** 2, 'ko')
3535
plt.title('Page Three')
3636
pdf.savefig(fig) # or you can pass a Figure object to pdf.savefig
3737
plt.close()

examples/pylab_examples/nan_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import matplotlib.pyplot as plt
66

77
t = np.arange(0.0, 1.0 + 0.01, 0.01)
8-
s = np.cos(2 * 2 * np.pi * t)
8+
s = np.cos(4 * np.pi * t)
99
t[41:60] = np.nan
1010

1111
plt.subplot(2, 1, 1)

examples/pylab_examples/pythonic_matplotlib.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272

7373
ax2 = fig.add_subplot(212)
74-
ax2.plot(t, sin(2*2*pi*t))
74+
ax2.plot(t, sin(4 * pi * t))
7575
ax2.grid(True)
7676
ax2.set_ylim((-2, 2))
7777
l = ax2.set_xlabel('Hi mom')

examples/pylab_examples/stackplot_demo2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def bump(a):
1515
z = 10 / (.1 + np.random.random())
1616
for i in range(m):
1717
w = (i / float(m) - y) * z
18-
a[i] += x * np.exp(-w * w)
18+
a[i] += x * np.exp(-w ** 2)
1919
a = np.zeros((m, n))
2020
for i in range(n):
2121
for j in range(5):

examples/pylab_examples/table_demo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
bar_width = 0.4
2626

2727
# Initialize the vertical-offset for the stacked bar chart.
28-
y_offset = np.array([0.0] * len(columns))
28+
y_offset = np.zeros(len(columns))
2929

3030
# Plot bars and create text labels for the table
3131
cell_text = []

examples/pylab_examples/tex_unicode_demo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
plt.figure(1, figsize=(6, 4))
1515
ax = plt.axes([0.1, 0.1, 0.8, 0.7])
1616
t = np.arange(0.0, 1.0 + 0.01, 0.01)
17-
s = np.cos(2*2*np.pi*t) + 2
17+
s = np.cos(4 * np.pi * t) + 2
1818
plt.plot(t, s)
1919

2020
plt.xlabel(r'\textbf{time (s)}')

examples/pylab_examples/tricontour_demo.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import matplotlib.pyplot as plt
55
import matplotlib.tri as tri
66
import numpy as np
7-
import math
87

98
# Creating a Triangulation without specifying the triangles results in the
109
# Delaunay triangulation of the points.
@@ -15,22 +14,21 @@
1514
min_radius = 0.25
1615
radii = np.linspace(min_radius, 0.95, n_radii)
1716

18-
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
17+
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
1918
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
20-
angles[:, 1::2] += math.pi/n_angles
19+
angles[:, 1::2] += np.pi / n_angles
2120

2221
x = (radii*np.cos(angles)).flatten()
2322
y = (radii*np.sin(angles)).flatten()
24-
z = (np.cos(radii)*np.cos(angles*3.0)).flatten()
23+
z = (np.cos(radii)*np.cos(3*angles)).flatten()
2524

2625
# Create the Triangulation; no triangles so Delaunay triangulation created.
2726
triang = tri.Triangulation(x, y)
2827

2928
# Mask off unwanted triangles.
30-
xmid = x[triang.triangles].mean(axis=1)
31-
ymid = y[triang.triangles].mean(axis=1)
32-
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
33-
triang.set_mask(mask)
29+
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
30+
y[triang.triangles].mean(axis=1))
31+
< min_radius)
3432

3533
# pcolor plot.
3634
plt.figure()

examples/pylab_examples/tricontour_smooth_user.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import matplotlib.pyplot as plt
77
import matplotlib.cm as cm
88
import numpy as np
9-
import math
109

1110

1211
#-----------------------------------------------------------------------------
@@ -32,9 +31,9 @@ def function_z(x, y):
3231
min_radius = 0.15
3332
radii = np.linspace(min_radius, 0.95, n_radii)
3433

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

3938
x = (radii*np.cos(angles)).flatten()
4039
y = (radii*np.sin(angles)).flatten()
@@ -46,10 +45,9 @@ def function_z(x, y):
4645
triang = tri.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

0 commit comments

Comments
 (0)