Skip to content

Commit 1138454

Browse files
committed
DOC: Replace .format by f-strings in examples
1 parent 38f6269 commit 1138454

File tree

19 files changed

+41
-43
lines changed

19 files changed

+41
-43
lines changed

examples/event_handling/resample.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def downsample(self, xstart, xend):
4545
xdata = xdata[::ratio]
4646
ydata = ydata[::ratio]
4747

48-
print("using {} of {} visible points".format(len(ydata), np.sum(mask)))
48+
print(f"using {len(ydata)} of {np.sum(mask)} visible points")
4949

5050
return xdata, ydata
5151

examples/images_contours_and_fields/contour_corner_mask.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
for ax, corner_mask in zip(axs, corner_masks):
2828
cs = ax.contourf(x, y, z, corner_mask=corner_mask)
2929
ax.contour(cs, colors='k')
30-
ax.set_title('corner_mask = {0}'.format(corner_mask))
30+
ax.set_title(f'{corner_mask=}')
3131

3232
# Plot grid.
3333
ax.grid(c='k', ls='-', alpha=0.3)

examples/images_contours_and_fields/image_annotated_heatmap.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ def annotate_heatmap(im, data=None, valfmt="{x:.2f}",
252252
# use an integer format on the annotations and provide some colors.
253253

254254
data = np.random.randint(2, 100, size=(7, 7))
255-
y = ["Book {}".format(i) for i in range(1, 8)]
256-
x = ["Store {}".format(i) for i in list("ABCDEFG")]
255+
y = [f"Book {i}" for i in range(1, 8)]
256+
x = [f"Store {i}" for i in list("ABCDEFG")]
257257
im, _ = heatmap(data, y, x, ax=ax2, vmin=0,
258258
cmap="magma_r", cbarlabel="weekly sold copies")
259259
annotate_heatmap(im, valfmt="{x:d}", size=7, threshold=20,
@@ -265,8 +265,8 @@ def annotate_heatmap(im, data=None, valfmt="{x:.2f}",
265265
# labels from an array of classes.
266266

267267
data = np.random.randn(6, 6)
268-
y = ["Prod. {}".format(i) for i in range(10, 70, 10)]
269-
x = ["Cycle {}".format(i) for i in range(1, 7)]
268+
y = [f"Prod. {i}" for i in range(10, 70, 10)]
269+
x = [f"Cycle {i}" for i in range(1, 7)]
270270

271271
qrates = list("ABCDEFG")
272272
norm = matplotlib.colors.BoundaryNorm(np.linspace(-3.5, 3.5, 8), 7)
@@ -292,7 +292,7 @@ def annotate_heatmap(im, data=None, valfmt="{x:.2f}",
292292

293293

294294
def func(x, pos):
295-
return "{:.2f}".format(x).replace("0.", ".").replace("1.00", "")
295+
return f"{x:.2f}".replace("0.", ".").replace("1.00", "")
296296

297297
annotate_heatmap(im, valfmt=matplotlib.ticker.FuncFormatter(func), size=7)
298298

examples/lines_bars_and_markers/eventplot_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
data1 = np.random.random([6, 50])
2121

2222
# set different colors for each set of positions
23-
colors1 = ['C{}'.format(i) for i in range(6)]
23+
colors1 = [f'C{i}' for i in range(6)]
2424

2525
# set different line properties for each set of positions
2626
# note that some overlap

examples/lines_bars_and_markers/filled_step.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,16 @@ def filled_hist(ax, edges, values, bottoms=None, orientation='v',
4949
"""
5050
print(orientation)
5151
if orientation not in 'hv':
52-
raise ValueError("orientation must be in {{'h', 'v'}} "
53-
"not {o}".format(o=orientation))
52+
raise ValueError(f"orientation must be in {{'h', 'v'}} "
53+
f"not {orientation}")
5454

5555
kwargs.setdefault('step', 'post')
5656
kwargs.setdefault('alpha', 0.7)
5757
edges = np.asarray(edges)
5858
values = np.asarray(values)
5959
if len(edges) - 1 != len(values):
60-
raise ValueError('Must provide one more bin edge than value not: '
61-
'len(edges): {lb} len(values): {lv}'.format(
62-
lb=len(edges), lv=len(values)))
60+
raise ValueError(f'Must provide one more bin edge than value not: '
61+
f'{len(edges)=} {len(values)=}')
6362

6463
if bottoms is None:
6564
bottoms = 0
@@ -159,7 +158,7 @@ def stack_hist(ax, stacked_data, sty_cycle, bottoms=None,
159158
arts = {}
160159
for j, (data, label, sty) in loop_iter:
161160
if label is None:
162-
label = 'dflt set {n}'.format(n=j)
161+
label = f'dflt set {j}'
163162
label = sty.pop('label', label)
164163
vals, edges = hist_func(data)
165164
if bottoms is None:
@@ -182,7 +181,7 @@ def stack_hist(ax, stacked_data, sty_cycle, bottoms=None,
182181

183182
# set up style cycles
184183
color_cycle = cycler(facecolor=plt.rcParams['axes.prop_cycle'][:4])
185-
label_cycle = cycler(label=['set {n}'.format(n=n) for n in range(4)])
184+
label_cycle = cycler(label=[f'set {n}' for n in range(4)])
186185
hatch_cycle = cycler(hatch=['/', '*', '+', '|'])
187186

188187
# Fixing random state for reproducibility

examples/misc/table_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
# Adjust layout to make room for the table:
5252
plt.subplots_adjust(left=0.2, bottom=0.2)
5353

54-
plt.ylabel("Loss in ${0}'s".format(value_increment))
54+
plt.ylabel(f"Loss in ${value_increment}'s")
5555
plt.yticks(values * value_increment, ['%d' % val for val in values])
5656
plt.xticks([])
5757
plt.title('Loss by Disaster')

examples/pie_and_polar_charts/pie_and_donut_labels.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
def func(pct, allvals):
4646
absolute = int(np.round(pct/100.*np.sum(allvals)))
47-
return "{:.1f}%\n({:d} g)".format(pct, absolute)
47+
return f"{pct:.1f}%\n({absolute:d} g)"
4848

4949

5050
wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data),
@@ -108,7 +108,7 @@ def func(pct, allvals):
108108
y = np.sin(np.deg2rad(ang))
109109
x = np.cos(np.deg2rad(ang))
110110
horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
111-
connectionstyle = "angle,angleA=0,angleB={}".format(ang)
111+
connectionstyle = f"angle,angleA=0,angleB={ang}"
112112
kw["arrowprops"].update({"connectionstyle": connectionstyle})
113113
ax.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
114114
horizontalalignment=horizontalalignment, **kw)

examples/scales/asinh_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
fig2 = plt.figure(constrained_layout=True)
6969
axs = fig2.subplots(1, 3, sharex=True)
7070
for ax, (a0, base) in zip(axs, ((0.2, 2), (1.0, 0), (5.0, 10))):
71-
ax.set_title('linear_width={:.3g}'.format(a0))
71+
ax.set_title(f'linear_width={a0:.3g}')
7272
ax.plot(x, x, label='y=x')
7373
ax.plot(x, 10*x, label='y=10x')
7474
ax.plot(x, 100*x, label='y=100x')

examples/specialty_plots/topographic_hillshading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

5959
# Label rows and columns
6060
for ax, ve in zip(axs[0], [0.1, 1, 10]):
61-
ax.set_title('{0}'.format(ve), size=18)
61+
ax.set_title(f'{ve}', size=18)
6262
for ax, mode in zip(axs[:, 0], ['Hillshade', 'hsv', 'overlay', 'soft']):
6363
ax.set_ylabel(mode, size=18)
6464

examples/subplots_axes_and_figures/axes_margins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def f(t):
6969
) # not sticky
7070
ax.margins(x=0.1, y=0.05)
7171
ax.set_aspect('equal')
72-
ax.set_title('{} Sticky'.format(status))
72+
ax.set_title(f'{status} Sticky')
7373

7474
plt.show()
7575

0 commit comments

Comments
 (0)