Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aec15b0

Browse files
committed
No context manager
1 parent f3a5b27 commit aec15b0

23 files changed

+217
-196
lines changed

plot_types/A_basic/a_plot.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
import matplotlib.pyplot as plt
88
import numpy as np
99

10+
plt.style.use('cheatsheet_gallery')
11+
1012
# make data
1113
X = np.linspace(0, 10, 100)
1214
Y = 4 + 2 * np.sin(2 * X)
1315

1416
# plot
15-
with plt.style.context('cheatsheet_gallery'):
16-
fig, ax = plt.subplots()
17+
fig, ax = plt.subplots()
1718

18-
ax.plot(X, Y, linewidth=2.0)
19+
ax.plot(X, Y, linewidth=2.0)
1920

20-
ax.set_xlim(0, 8)
21-
ax.set_xticks(np.arange(1, 8))
22-
ax.set_ylim(0, 8)
23-
ax.set_yticks(np.arange(1, 8))
24-
plt.show()
21+
ax.set_xlim(0, 8)
22+
ax.set_xticks(np.arange(1, 8))
23+
ax.set_ylim(0, 8)
24+
ax.set_yticks(np.arange(1, 8))
25+
plt.show()

plot_types/A_basic/b_scatter.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
import matplotlib.pyplot as plt
77
import numpy as np
88

9+
plt.style.use('cheatsheet_gallery')
10+
911
# make the data
1012
np.random.seed(3)
1113
X = 4 + np.random.normal(0, 1.25, 24)
1214
Y = 4 + np.random.normal(0, 1.25, len(X))
1315

1416
# plot
15-
with plt.style.context('cheatsheet_gallery'):
16-
fig, ax = plt.subplots()
17+
fig, ax = plt.subplots()
1718

18-
ax.scatter(X, Y, 20, zorder=10,
19-
edgecolor="none", linewidth=0.25)
19+
ax.scatter(X, Y, 20, zorder=10,
20+
edgecolor="none", linewidth=0.25)
2021

21-
ax.set_xlim(0, 8)
22-
ax.set_xticks(np.arange(1, 8))
23-
ax.set_ylim(0, 8)
24-
ax.set_yticks(np.arange(1, 8))
25-
plt.show()
22+
ax.set_xlim(0, 8)
23+
ax.set_xticks(np.arange(1, 8))
24+
ax.set_ylim(0, 8)
25+
ax.set_yticks(np.arange(1, 8))
26+
plt.show()

plot_types/A_basic/c_bar.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
"""
66
import matplotlib.pyplot as plt
77
import numpy as np
8+
plt.style.use('cheatsheet_gallery')
89

910
# make data:
1011
np.random.seed(3)
1112
X = 0.5 + np.arange(8)
1213
Y = np.random.uniform(2, 7, len(X))
1314

1415
# plot
15-
with plt.style.context('cheatsheet_gallery'):
16-
fig, ax = plt.subplots()
16+
fig, ax = plt.subplots()
1717

18-
ax.bar(X, Y, bottom=0, width=1, edgecolor="white", linewidth=0.7)
18+
ax.bar(X, Y, bottom=0, width=1, edgecolor="white", linewidth=0.7)
1919

20-
ax.set_xlim(0, 8)
21-
ax.set_xticks(np.arange(1, 8))
22-
ax.set_ylim(0, 8)
23-
ax.set_yticks(np.arange(1, 8))
24-
plt.show()
20+
ax.set_xlim(0, 8)
21+
ax.set_xticks(np.arange(1, 8))
22+
ax.set_ylim(0, 8)
23+
ax.set_yticks(np.arange(1, 8))
24+
plt.show()

plot_types/A_basic/d_stem.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
import matplotlib.pyplot as plt
77
import numpy as np
88

9+
plt.style.use('cheatsheet_gallery')
10+
911
# make data
1012
np.random.seed(3)
1113
X = 0.5 + np.arange(8)
1214
Y = np.random.uniform(2, 7, len(X))
1315

1416
# plot
15-
with plt.style.context('cheatsheet_gallery'):
16-
fig, ax = plt.subplots()
17+
fig, ax = plt.subplots()
1718

18-
ax.stem(X, Y, bottom=0, linefmt="-", markerfmt="d")
19+
ax.stem(X, Y, bottom=0, linefmt="-", markerfmt="d")
1920

20-
ax.set_xlim(0, 8)
21-
ax.set_xticks(np.arange(1, 8))
22-
ax.set_ylim(0, 8)
23-
ax.set_yticks(np.arange(1, 8))
24-
plt.show()
21+
ax.set_xlim(0, 8)
22+
ax.set_xticks(np.arange(1, 8))
23+
ax.set_ylim(0, 8)
24+
ax.set_yticks(np.arange(1, 8))
25+
plt.show()

plot_types/A_basic/e_step.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
import matplotlib.pyplot as plt
77
import numpy as np
88

9+
plt.style.use('cheatsheet_gallery')
10+
911
# make data
1012
np.random.seed(3)
1113
X = 0.5 + np.arange(8)
1214
Y = np.random.uniform(2, 7, len(X))
1315

1416
# plot
15-
with plt.style.context('cheatsheet_gallery'):
16-
fig, ax = plt.subplots()
17+
fig, ax = plt.subplots()
1718

18-
ax.step(X, Y, linewidth=2.5)
19+
ax.step(X, Y, linewidth=2.5)
1920

20-
ax.set_xlim(0, 8)
21-
ax.set_xticks(np.arange(1, 8))
22-
ax.set_ylim(0, 8)
23-
ax.set_yticks(np.arange(1, 8))
24-
plt.show()
21+
ax.set_xlim(0, 8)
22+
ax.set_xticks(np.arange(1, 8))
23+
ax.set_ylim(0, 8)
24+
ax.set_yticks(np.arange(1, 8))
25+
plt.show()

plot_types/A_basic/f_pie.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@
77
import matplotlib.pyplot as plt
88
import numpy as np
99

10-
# plot
11-
with plt.style.context('cheatsheet_gallery'):
12-
fig, ax = plt.subplots()
10+
plt.style.use('cheatsheet_gallery')
11+
1312

14-
# make data
15-
X = [1, 2, 3, 4]
16-
colors = np.zeros((len(X), 4))
17-
colors[:] = mpl.colors.to_rgba("C0")
18-
colors[:, 3] = np.linspace(0.25, 0.75, len(X))
13+
# make data
14+
X = [1, 2, 3, 4]
15+
colors = np.zeros((len(X), 4))
16+
colors[:] = mpl.colors.to_rgba("C0")
17+
colors[:, 3] = np.linspace(0.25, 0.75, len(X))
1918

20-
ax.pie(X, colors=["white"]*len(X), radius=3, center=(4, 4),
21-
wedgeprops={"linewidth": 1, "edgecolor": "white"}, frame=True)
22-
ax.pie(X, colors=colors, radius=3, center=(4, 4),
23-
wedgeprops={"linewidth": 1, "edgecolor": "white"}, frame=True)
19+
# plot
20+
fig, ax = plt.subplots()
21+
ax.pie(X, colors=["white"]*len(X), radius=3, center=(4, 4),
22+
wedgeprops={"linewidth": 1, "edgecolor": "white"}, frame=True)
23+
ax.pie(X, colors=colors, radius=3, center=(4, 4),
24+
wedgeprops={"linewidth": 1, "edgecolor": "white"}, frame=True)
2425

25-
ax.set_xlim(0, 8)
26-
ax.set_xticks(np.arange(1, 8))
27-
ax.set_ylim(0, 8)
28-
ax.set_yticks(np.arange(1, 8))
26+
ax.set_xlim(0, 8)
27+
ax.set_xticks(np.arange(1, 8))
28+
ax.set_ylim(0, 8)
29+
ax.set_yticks(np.arange(1, 8))
2930

30-
plt.show()
31+
plt.show()

plot_types/A_basic/g_fill_between.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@
66
import matplotlib.pyplot as plt
77
import numpy as np
88

9+
plt.style.use('cheatsheet_gallery')
10+
911
# make data
1012
np.random.seed(1)
1113
X = np.linspace(0, 8, 16)
1214
Y1 = 3 + 4*X/8 + np.random.uniform(0.0, 0.5, len(X))
1315
Y2 = 1 + 2*X/8 + np.random.uniform(0.0, 0.5, len(X))
1416

1517
# plot
16-
with plt.style.context('cheatsheet_gallery'):
17-
fig, ax = plt.subplots()
18+
fig, ax = plt.subplots()
1819

19-
ax.fill_between(X, Y1, Y2, alpha=.5, linewidth=0)
20-
ax.plot(X, (Y1+Y2)/2, linewidth=2.5)
20+
ax.fill_between(X, Y1, Y2, alpha=.5, linewidth=0)
21+
ax.plot(X, (Y1+Y2)/2, linewidth=2.5)
2122

22-
ax.set_xlim(0, 8)
23-
ax.set_xticks(np.arange(1, 8))
24-
ax.set_ylim(0, 8)
25-
ax.set_yticks(np.arange(1, 8))
23+
ax.set_xlim(0, 8)
24+
ax.set_xticks(np.arange(1, 8))
25+
ax.set_ylim(0, 8)
26+
ax.set_yticks(np.arange(1, 8))
2627

27-
plt.show()
28+
plt.show()

plot_types/B_arrays/a_imshow.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77
import matplotlib.pyplot as plt
88
import numpy as np
99

10+
plt.style.use('cheatsheet_gallery')
11+
1012
# make data
1113
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
1214
Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2)
1315
Z = Z - Z.min()
1416
Z = Z[::16, ::16]
1517

1618
# plot
17-
with plt.style.context('cheatsheet_gallery'):
18-
fig, ax = plt.subplots()
19+
fig, ax = plt.subplots()
1920

20-
ax.imshow(Z, extent=[0, 8, 0, 8], interpolation="nearest",
21-
cmap=plt.get_cmap('Blues'), vmin=0, vmax=1.6)
21+
ax.imshow(Z, extent=[0, 8, 0, 8], interpolation="nearest",
22+
cmap=plt.get_cmap('Blues'), vmin=0, vmax=1.6)
2223

23-
ax.set_xticks([])
24-
ax.set_yticks([])
25-
plt.show()
24+
ax.set_xticks([])
25+
ax.set_yticks([])
26+
plt.show()

plot_types/B_arrays/b_pcolormesh.py

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

13+
plt.style.use('cheatsheet_gallery')
14+
1315
# make full-res data
1416
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
1517
Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2)
@@ -24,9 +26,8 @@
2426
Z = Z[::8, :][:, xint]
2527

2628
# plot
27-
with plt.style.context('cheatsheet_gallery'):
28-
fig, ax = plt.subplots()
29+
fig, ax = plt.subplots()
2930

30-
ax.pcolormesh(X, Y, Z, vmin=0, vmax=1.5, shading='nearest')
31+
ax.pcolormesh(X, Y, Z, vmin=0, vmax=1.5, shading='nearest')
3132

32-
plt.show()
33+
plt.show()

plot_types/B_arrays/c_contourf.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
import matplotlib.pyplot as plt
77
import numpy as np
88

9+
plt.style.use('cheatsheet_gallery')
10+
911
# make data
1012
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
1113
Z = (1 - X/2. + X**5 + Y**3)*np.exp(-X**2-Y**2)
1214
Z = Z - Z.min()
1315
levs = np.linspace(np.min(Z), np.max(Z), 7)
1416

1517
# plot
16-
with plt.style.context('cheatsheet_gallery'):
17-
fig, ax = plt.subplots()
18+
fig, ax = plt.subplots()
1819

19-
plt.contourf(X, Y, Z, levels=levs)
20-
plt.contour(X, Y, Z, levels=levs, colors="white", linewidths=0.5)
20+
plt.contourf(X, Y, Z, levels=levs)
21+
plt.contour(X, Y, Z, levels=levs, colors="white", linewidths=0.5)
2122

22-
plt.show()
23+
plt.show()

plot_types/B_arrays/d_quiver.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
import matplotlib.pyplot as plt
77
import numpy as np
88

9+
plt.style.use('cheatsheet_gallery')
10+
911
# make data
1012
T = np.linspace(0, 2*np.pi, 8)
1113
X, Y = 4 + 1 * np.cos(T), 4 + 1 * np.sin(T)
1214
U, V = 1.5 * np.cos(T), 1.5 * np.sin(T)
1315

1416
# plot
15-
with plt.style.context('cheatsheet_gallery'):
16-
fig, ax = plt.subplots()
17+
fig, ax = plt.subplots()
1718

18-
plt.quiver(X, Y, U, V, color="C0", angles='xy',
19-
scale_units='xy', scale=0.5, width=.05)
19+
plt.quiver(X, Y, U, V, color="C0", angles='xy',
20+
scale_units='xy', scale=0.5, width=.05)
2021

21-
ax.set_xlim(0, 8)
22-
ax.set_xticks(np.arange(1, 8))
23-
ax.set_ylim(0, 8)
24-
ax.set_yticks(np.arange(1, 8))
22+
ax.set_xlim(0, 8)
23+
ax.set_xticks(np.arange(1, 8))
24+
ax.set_ylim(0, 8)
25+
ax.set_yticks(np.arange(1, 8))
2526

26-
plt.show()
27+
plt.show()

plot_types/B_arrays/e_streamplot.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import matplotlib.pyplot as plt
77
import numpy as np
88

9+
plt.style.use('cheatsheet_gallery')
10+
911
# make a stream function:
1012
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
1113
Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2)
@@ -15,11 +17,10 @@
1517
U = -np.diff(Z[:, 1:], axis=0)
1618

1719
# plot:
18-
with plt.style.context('cheatsheet_gallery'):
19-
fig, ax = plt.subplots()
20-
# contour stream function
21-
ax.contour(X, Y, Z, colors='C1', alpha=0.5, zorder=1, linewidths=3)
22-
# plot stream plot
23-
ax.streamplot(X[1:, 1:], Y[1:, 1:], U, V, zorder=2)
20+
fig, ax = plt.subplots()
21+
# contour stream function
22+
ax.contour(X, Y, Z, colors='C1', alpha=0.5, zorder=1, linewidths=3)
23+
# plot stream plot
24+
ax.streamplot(X[1:, 1:], Y[1:, 1:], U, V, zorder=2)
2425

25-
plt.show()
26+
plt.show()

plot_types/C_stats/a_hist.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
import matplotlib.pyplot as plt
77
import numpy as np
88

9+
plt.style.use('cheatsheet_gallery')
10+
911
# make data
1012
np.random.seed(1)
1113
X = 4 + np.random.normal(0, 1.5, 200)
1214

1315
# plot:
14-
with plt.style.context('cheatsheet_gallery'):
15-
fig, ax = plt.subplots()
16+
fig, ax = plt.subplots()
1617

17-
ax.hist(X, bins=8, linewidth=0.5, edgecolor="white",)
18+
ax.hist(X, bins=8, linewidth=0.5, edgecolor="white",)
1819

19-
ax.set_xlim(0, 8)
20-
ax.set_xticks(np.arange(1, 8))
21-
ax.set_ylim(0, 80)
22-
ax.set_yticks(np.arange(1, 80, 10))
20+
ax.set_xlim(0, 8)
21+
ax.set_xticks(np.arange(1, 8))
22+
ax.set_ylim(0, 80)
23+
ax.set_yticks(np.arange(1, 80, 10))
2324

24-
plt.show()
25+
plt.show()

0 commit comments

Comments
 (0)