Skip to content

Commit dec8072

Browse files
committed
ps: Use _nums_to_str in more places
I only change places that used the exact same formatting, and not cases of, e.g., `%f` or `%g`.
1 parent 7062dda commit dec8072

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def _get_papertype(w, h):
8888
return 'a0'
8989

9090

91-
def _nums_to_str(*args):
92-
return " ".join(f"{arg:1.3f}".rstrip("0").rstrip(".") for arg in args)
91+
def _nums_to_str(*args, sep=" "):
92+
return sep.join(f"{arg:1.3f}".rstrip("0").rstrip(".") for arg in args)
9393

9494

9595
def _move_path_to_path_or_stream(src, dst):
@@ -293,16 +293,16 @@ def _is_transparent(self, rgb_or_rgba):
293293

294294
def set_color(self, r, g, b, store=True):
295295
if (r, g, b) != self.color:
296-
self._pswriter.write(f"{r:1.3f} setgray\n"
296+
self._pswriter.write(f"{_nums_to_str(r)} setgray\n"
297297
if r == g == b else
298-
f"{r:1.3f} {g:1.3f} {b:1.3f} setrgbcolor\n")
298+
f"{_nums_to_str(r, g, b)} setrgbcolor\n")
299299
if store:
300300
self.color = (r, g, b)
301301

302302
def set_linewidth(self, linewidth, store=True):
303303
linewidth = float(linewidth)
304304
if linewidth != self.linewidth:
305-
self._pswriter.write("%1.3f setlinewidth\n" % linewidth)
305+
self._pswriter.write(f"{_nums_to_str(linewidth)} setlinewidth\n")
306306
if store:
307307
self.linewidth = linewidth
308308

@@ -338,8 +338,7 @@ def set_linedash(self, offset, seq, store=True):
338338
if np.array_equal(seq, oldseq) and oldo == offset:
339339
return
340340

341-
self._pswriter.write(f"[{_nums_to_str(*seq)}]"
342-
f" {_nums_to_str(offset)} setdash\n"
341+
self._pswriter.write(f"[{_nums_to_str(*seq)}] {_nums_to_str(offset)} setdash\n"
343342
if seq is not None and len(seq) else
344343
"[] 0 setdash\n")
345344
if store:
@@ -474,9 +473,9 @@ def draw_markers(
474473
ps_color = (
475474
None
476475
if self._is_transparent(rgbFace)
477-
else '%1.3f setgray' % rgbFace[0]
476+
else f'{_nums_to_str(rgbFace[0])} setgray'
478477
if rgbFace[0] == rgbFace[1] == rgbFace[2]
479-
else '%1.3f %1.3f %1.3f setrgbcolor' % rgbFace[:3])
478+
else f'{_nums_to_str(*rgbFace[:3])} setrgbcolor')
480479

481480
# construct the generic marker command:
482481

@@ -582,7 +581,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
582581
w, h, bl = self.get_text_width_height_descent(s, prop, ismath="TeX")
583582
fontsize = prop.get_size_in_points()
584583
thetext = 'psmarker%d' % self.textcnt
585-
color = '%1.3f,%1.3f,%1.3f' % gc.get_rgb()[:3]
584+
color = _nums_to_str(*gc.get_rgb()[:3], sep=',')
586585
fontcmd = {'sans-serif': r'{\sffamily %s}',
587586
'monospace': r'{\ttfamily %s}'}.get(
588587
mpl.rcParams['font.family'][0], r'{\rmfamily %s}')
@@ -784,8 +783,8 @@ def _draw_ps(self, ps, gc, rgbFace, *, fill=True, stroke=True):
784783
if hatch:
785784
hatch_name = self.create_hatch(hatch)
786785
write("gsave\n")
787-
write("%f %f %f " % gc.get_hatch_color()[:3])
788-
write("%s setpattern fill grestore\n" % hatch_name)
786+
write(_nums_to_str(*gc.get_hatch_color()[:3]))
787+
write(f" {hatch_name} setpattern fill grestore\n")
789788

790789
if stroke:
791790
write("stroke\n")

0 commit comments

Comments
 (0)