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

Skip to content

Commit 2d8d098

Browse files
authored
Merge pull request #25472 from anntzer/afo
Use get_sample_data(..., asfileobj=False) less.
2 parents 378172b + 2535c75 commit 2d8d098

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

galleries/examples/showcase/stock_prices.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,10 @@
2222
from matplotlib.cbook import get_sample_data
2323
import matplotlib.transforms as mtransforms
2424

25-
26-
def convertdate(x):
27-
return np.datetime64(x, 'D')
28-
29-
30-
fname = get_sample_data('Stocks.csv', asfileobj=False)
31-
stock_data = np.genfromtxt(fname, encoding='utf-8', delimiter=',',
32-
names=True, dtype=None, converters={0: convertdate},
33-
skip_header=1)
34-
25+
with get_sample_data('Stocks.csv') as file:
26+
stock_data = np.genfromtxt(
27+
file, delimiter=',', names=True, dtype=None,
28+
converters={0: lambda x: np.datetime64(x, 'D')}, skip_header=1)
3529

3630
fig, ax = plt.subplots(1, 1, figsize=(6, 8), layout='constrained')
3731

galleries/examples/subplots_axes_and_figures/figure_title.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
We can also add figure-level x- and y-labels using `.FigureBase.supxlabel` and
1111
`.FigureBase.supylabel`.
1212
"""
13+
1314
import matplotlib.pyplot as plt
1415
import numpy as np
1516

@@ -34,13 +35,10 @@
3435
# `.FigureBase.supylabel` methods.
3536

3637

37-
def convertdate(x):
38-
return np.datetime64(x, 'D')
39-
40-
fname = get_sample_data('Stocks.csv', asfileobj=False)
41-
stocks = np.genfromtxt(fname, encoding='utf-8', delimiter=',',
42-
names=True, dtype=None, converters={0: convertdate},
43-
skip_header=1)
38+
with get_sample_data('Stocks.csv') as file:
39+
stocks = np.genfromtxt(
40+
file, delimiter=',', names=True, dtype=None,
41+
converters={0: lambda x: np.datetime64(x, 'D')}, skip_header=1)
4442

4543
fig, axs = plt.subplots(4, 2, figsize=(9, 5), layout='constrained',
4644
sharex=True, sharey=True)
@@ -51,3 +49,5 @@ def convertdate(x):
5149
ax.set_title(column_name, fontsize='small', loc='left')
5250
fig.supxlabel('Year')
5351
fig.supylabel('Stock price relative to max')
52+
53+
plt.show()

0 commit comments

Comments
 (0)