Skip to content

Commit 18cde97

Browse files
committed
move to new-style format()
1 parent 78b01c8 commit 18cde97

File tree

10 files changed

+133
-121
lines changed

10 files changed

+133
-121
lines changed

matplotlib2tikz/axes.py

+62-44
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def _ticks(self, data, obj):
264264
y_tick_position_string, y_tick_position = _get_tick_position(obj, "y")
265265

266266
if x_tick_position == y_tick_position and x_tick_position is not None:
267-
self.axis_options.append("tick pos=%s" % x_tick_position)
267+
self.axis_options.append("tick pos={}".format(x_tick_position))
268268
else:
269269
self.axis_options.append(x_tick_position_string)
270270
self.axis_options.append(y_tick_position_string)
@@ -286,7 +286,7 @@ def _grid(self, obj, data):
286286
xgridcolor = xlines[0].get_color()
287287
data, col, _ = color.mpl_color2xcolor(data, xgridcolor)
288288
if col != "black":
289-
self.axis_options.append("x grid style={%s}" % col)
289+
self.axis_options.append("x grid style={{{}}}".format(col))
290290

291291
if obj.yaxis._gridOnMajor:
292292
self.axis_options.append("ymajorgrids")
@@ -298,7 +298,7 @@ def _grid(self, obj, data):
298298
ygridcolor = ylines[0].get_color()
299299
data, col, _ = color.mpl_color2xcolor(data, ygridcolor)
300300
if col != "black":
301-
self.axis_options.append("y grid style={%s}" % col)
301+
self.axis_options.append("y grid style={{{}}}".format(col))
302302

303303
return
304304

@@ -380,11 +380,13 @@ def _colorbar(self, colorbar, data):
380380
else:
381381
self.axis_options.append("colormap/" + mycolormap)
382382

383-
self.axis_options.append("point meta min=%.15g" % limits[0])
384-
self.axis_options.append("point meta max=%.15g" % limits[1])
383+
self.axis_options.append("point meta min={:.15g}".format(limits[0]))
384+
self.axis_options.append("point meta max={:.15g}".format(limits[1]))
385385

386386
if colorbar_styles:
387-
self.axis_options.append("colorbar style={%s}" % ",".join(colorbar_styles))
387+
self.axis_options.append(
388+
"colorbar style={{{}}}".format(",".join(colorbar_styles))
389+
)
388390

389391
return
390392

@@ -403,8 +405,8 @@ def _subplot(self, obj, data):
403405
self.subplot_index = geom[2] + 1
404406
if "is_in_groupplot_env" not in data or not data["is_in_groupplot_env"]:
405407
self.content.append(
406-
"\\begin{groupplot}[group style="
407-
"{group size=%.d by %.d}]\n" % (geom[1], geom[0])
408+
"\\begin{{groupplot}}[group style="
409+
"{{group size={} by {}}}]\n".format(geom[1], geom[0])
408410
)
409411
data["is_in_groupplot_env"] = True
410412
data["pgfplots libs"].add("groupplots")
@@ -414,7 +416,7 @@ def _subplot(self, obj, data):
414416

415417
def _get_label_rotation_and_horizontal_alignment(obj, data, axes_obj):
416418
tick_label_text_width = None
417-
tick_label_text_width_identifier = "%s tick label text width" % axes_obj
419+
tick_label_text_width_identifier = "{} tick label text width".format(axes_obj)
418420
if tick_label_text_width_identifier in data["extra axis options"]:
419421
tick_label_text_width = data["extra axis options [base]"][
420422
tick_label_text_width_identifier
@@ -446,59 +448,65 @@ def _get_label_rotation_and_horizontal_alignment(obj, data, axes_obj):
446448
values = []
447449

448450
if any(tick_labels_rotation) != 0:
449-
values.append("rotate=%d" % tick_labels_rotation[0])
451+
values.append("rotate={}".format(tick_labels_rotation[0]))
450452

451453
if tick_label_text_width:
452-
values.append("align=%s" % tick_labels_horizontal_alignment[0])
453-
values.append("text width=%s" % tick_label_text_width)
454+
values.append("align={}".format(tick_labels_horizontal_alignment[0]))
455+
values.append("text width={}".format(tick_label_text_width))
454456
else:
455457
print(
456-
"Horizontal alignment will be ignored as no '%s tick "
457-
"label text width' has been passed in the 'extra' "
458-
"parameter" % axes_obj
458+
(
459+
"Horizontal alignment will be ignored as no '{} tick "
460+
"label text width' has been passed in the 'extra' "
461+
"parameter"
462+
).format(axes_obj)
459463
)
460464

461465
if values:
462-
label_style = "%sticklabel style = {%s}" % (axes_obj, ",".join(values))
466+
label_style = "{}ticklabel style = {{{}}}".format(
467+
axes_obj, ",".join(values)
468+
)
463469
else:
464470
values = []
465471

466472
if tick_labels_rotation_same_value:
467-
values.append("rotate=%d" % tick_labels_rotation[0])
473+
values.append("rotate={}".format(tick_labels_rotation[0]))
468474
else:
469475
values.append(
470-
"rotate={%s,0}[\\ticknum]"
471-
% ",".join(str(x) for x in tick_labels_rotation)
476+
"rotate={{{},0}}[\\ticknum]".format(
477+
",".join(str(x) for x in tick_labels_rotation)
478+
)
472479
)
473480

474481
if tick_label_text_width:
475482
if tick_labels_horizontal_alignment_same_value:
476-
values.append("align=%s" % tick_labels_horizontal_alignment[0])
477-
values.append("text width=%s" % tick_label_text_width)
483+
values.append("align={}".format(tick_labels_horizontal_alignment[0]))
484+
values.append("text width={}".format(tick_label_text_width))
478485
else:
479486
for idx, x in enumerate(tick_labels_horizontal_alignment):
480-
label_style += "%s_tick_label_ha_%d/.initial = %s" % (
481-
axes_obj,
482-
idx,
483-
x,
487+
label_style += "{}_tick_label_ha_{}/.initial = {}".format(
488+
axes_obj, idx, x
484489
)
485490

486491
values.append(
487-
"align=\\pgfkeysvalueof{/pgfplots/"
488-
"%s_tick_label_ha_\\ticknum}" % axes_obj
492+
"align=\\pgfkeysvalueof{{/pgfplots/{}_tick_label_ha_\\ticknum}}".format(
493+
axes_obj
494+
)
489495
)
490-
values.append("text width=%s" % tick_label_text_width)
496+
values.append("text width={}".format(tick_label_text_width))
491497
else:
492498
print(
493-
"Horizontal alignment will be ignored as no '%s tick "
494-
"label text width' has been passed in the 'extra' "
495-
"parameter" % axes_obj
499+
(
500+
"Horizontal alignment will be ignored as no '{} tick "
501+
"label text width' has been passed in the 'extra' "
502+
"parameter"
503+
).format(axes_obj)
496504
)
497505

498506
label_style = (
499-
"every %s tick label/.style = {\n"
500-
"%s\n"
501-
"}" % (axes_obj, ",\n".join(values))
507+
"every {} tick label/.style = {{\n"
508+
"{}\n"
509+
"}}".format(axes_obj, ",\n".join(values))
502510
)
503511

504512
return label_style
@@ -520,7 +528,7 @@ def _get_tick_position(obj, axes_obj):
520528

521529
major_ticks_position = None
522530
if not major_ticks_bottom_show_all and not major_ticks_top_show_all:
523-
position_string = "%smajorticks=false" % axes_obj
531+
position_string = "{}majorticks=false".format(axes_obj)
524532
elif major_ticks_bottom_show_all and major_ticks_top_show_all:
525533
major_ticks_position = "both"
526534
elif major_ticks_bottom_show_all:
@@ -529,7 +537,7 @@ def _get_tick_position(obj, axes_obj):
529537
major_ticks_position = "right"
530538

531539
if major_ticks_position:
532-
position_string = "%stick pos=%s" % (axes_obj, major_ticks_position)
540+
position_string = "{}tick pos={}".format(axes_obj, major_ticks_position)
533541

534542
return position_string, major_ticks_position
535543

@@ -566,15 +574,17 @@ def _get_ticks(data, xy, ticks, ticklabels):
566574
if data["strict"] or is_label_required:
567575
if pgfplots_ticks:
568576
axis_options.append(
569-
"%stick={%s}" % (xy, ",".join(["%.15g" % el for el in pgfplots_ticks]))
577+
"{}tick={{{}}}".format(
578+
xy, ",".join(["{:.15g}".format(el) for el in pgfplots_ticks])
579+
)
570580
)
571581
else:
572582
val = "{}" if "minor" in xy else "\\empty"
573-
axis_options.append("%stick=%s" % (xy, val))
583+
axis_options.append("{}tick={}".format(xy, val))
574584

575585
if is_label_required:
576586
axis_options.append(
577-
"%sticklabels={%s}" % (xy, ",".join(pgfplots_ticklabels))
587+
"{}ticklabels={{{}}}".format(xy, ",".join(pgfplots_ticklabels))
578588
)
579589
return axis_options
580590

@@ -705,9 +715,13 @@ def _handle_linear_segmented_color_map(cmap):
705715

706716
color_changes = []
707717
for (k, x) in enumerate(X):
708-
color_changes.append("rgb(%d%s)=(%.15g,%.15g,%.15g)" % ((x, unit) + colors[k]))
718+
color_changes.append(
719+
"rgb({}{})=({:.15g},{:.15g},{:.15g})".format(*((x, unit) + colors[k]))
720+
)
709721

710-
colormap_string = "{mymap}{[1%s]\n %s\n}" % (unit, ";\n ".join(color_changes))
722+
colormap_string = "{{mymap}}{{[1{}]\n {}\n}}".format(
723+
unit, ";\n ".join(color_changes)
724+
)
711725
is_custom_colormap = True
712726
return (colormap_string, is_custom_colormap)
713727

@@ -740,18 +754,22 @@ def _handle_listed_color_map(cmap):
740754
unit = "pt"
741755
if cmap.N is None or cmap.N == len(cmap.colors):
742756
colors = [
743-
"rgb(%d%s)=(%.15g,%.15g,%.15g)" % (k, unit, rgb[0], rgb[1], rgb[2])
757+
"rgb({}{})=({:.15g},{:.15g},{:.15g})".format(
758+
k, unit, rgb[0], rgb[1], rgb[2]
759+
)
744760
for (k, rgb) in enumerate(cmap.colors)
745761
]
746762
else:
747763
reps = int(float(cmap.N) / len(cmap.colors) - 0.5) + 1
748764
repeated_cols = reps * cmap.colors
749765
colors = [
750-
"rgb(%d%s)=(%.15g,%.15g,%.15g)" % (k, unit, rgb[0], rgb[1], rgb[2])
766+
"rgb({}{})=({:.15g},{:.15g},{:.15g})".format(
767+
k, unit, rgb[0], rgb[1], rgb[2]
768+
)
751769
for (k, rgb) in enumerate(repeated_cols[: cmap.N])
752770
]
753771

754-
colormap_string = "{mymap}{[1%s]\n %s\n}" % (unit, ";\n ".join(colors))
772+
colormap_string = "{mymap}{[1{}]\n {}\n}".format(unit, ";\n ".join(colors))
755773
is_custom_colormap = True
756774
return (colormap_string, is_custom_colormap)
757775

matplotlib2tikz/color.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def mpl_color2xcolor(data, matplotlib_color):
7171
# The cases 0.0 (my_col == black) and 1.0 (my_col == rgb) are
7272
# already accounted for by checking in available_colors above.
7373
if all(my_col[:3] == alpha * rgb) and 0.0 < alpha < 1.0:
74-
xcol = name + ("!%r!black" % (alpha * 100))
74+
xcol = name + ("!{}!black".format(alpha * 100))
7575
return data, xcol, my_col
7676

7777
# Lookup failed, add it to the custom list.

matplotlib2tikz/image.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def draw_image(data, obj):
5454
# \includegraphics fails unexpectedly in some cases
5555
content.append(
5656
"\\addplot graphics [includegraphics cmd=\\pgfimage,"
57-
"xmin=%.15g, xmax=%.15g, "
58-
"ymin=%.15g, ymax=%.15g] {%s};\n" % (extent + (rel_filepath,))
57+
"xmin={:.15g}, xmax={:.15g}, "
58+
"ymin={:.15g}, ymax={:.15g}] {{{}}};\n".format(*(extent + (rel_filepath,)))
5959
)
6060

6161
return data, content

matplotlib2tikz/legend.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def draw_legend(data, obj):
1616
texts.append("{}".format(text.get_text()))
1717
children_alignment.append("{}".format(text.get_horizontalalignment()))
1818

19-
cont = "legend entries={{%s}}" % "},{".join(texts)
19+
cont = "legend entries={{{{{}}}}}".format("},{".join(texts))
2020
data["extra axis options"].add(cont)
2121

2222
# Get the location.
@@ -44,9 +44,9 @@ def draw_legend(data, obj):
4444

4545
legend_style = []
4646
if position:
47-
legend_style.append("at={(%.15g,%.15g)}" % (position[0], position[1]))
47+
legend_style.append("at={{({:.15g},{:.15g})}}".format(position[0], position[1]))
4848
if anchor:
49-
legend_style.append("anchor=%s" % anchor)
49+
legend_style.append("anchor={}".format(anchor))
5050

5151
# Get the edgecolor of the box
5252
if obj.get_frame_on():
@@ -95,7 +95,7 @@ def draw_legend(data, obj):
9595
data, numpy.squeeze(handle.get_color())
9696
)
9797
data["legend colors"].append(
98-
"\\addlegendimage{no markers, %s}\n" % legend_color
98+
"\\addlegendimage{{no markers, {}}}\n".format(legend_color)
9999
)
100100
except AttributeError:
101101
pass

matplotlib2tikz/line2d.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def draw_line2d(data, obj):
4747

4848
alpha = obj.get_alpha()
4949
if alpha is not None:
50-
addplot_options.append("opacity=%r" % alpha)
50+
addplot_options.append("opacity={}".format(alpha))
5151

5252
show_line, linestyle = _mpl_linestyle2pgfp_linestyle(obj.get_linestyle())
5353
if show_line and linestyle:
@@ -132,13 +132,12 @@ def draw_linecollection(data, obj):
132132
if style[0] is not None:
133133
assert isinstance(style, tuple)
134134
if len(style[1]) == 2:
135-
linestyle = "dash pattern=on %dpt off %dpt" % (
136-
int(style[1][0]),
137-
int(style[1][1]),
135+
linestyle = "dash pattern=on {:d}pt off {:d}pt".format(
136+
int(style[1][0]), int(style[1][1])
138137
)
139138
else:
140139
assert len(style[1]) == 4
141-
linestyle = "dash pattern=on %dpt off %dpt on %dpt off %dpt" % (
140+
linestyle = "dash pattern=on {:d}pt off {:d}pt on {:d}pt off {:d}pt".format(
142141
int(style[1][0]),
143142
int(style[1][1]),
144143
int(style[1][2]),
@@ -174,7 +173,7 @@ def _mpl_linewidth2pgfp_linewidth(data, line_width):
174173
return TIKZ_LINEWIDTHS[line_width]
175174
except KeyError:
176175
# explicit line width
177-
return "line width=%spt" % line_width
176+
return "line width={}pt".format(line_width)
178177
else:
179178
# The following is an alternative approach to line widths.
180179
# The default line width in matplotlib is 1.0pt, in PGFPlots 0.4pt
@@ -195,7 +194,7 @@ def _mpl_linewidth2pgfp_linewidth(data, line_width):
195194
out = literals[scaled_line_width]
196195
except KeyError:
197196
# explicit line width
198-
out = "line width=%rpt" % (0.4 * line_width)
197+
out = "line width={}pt".format(0.4 * line_width)
199198

200199
return out
201200

@@ -340,12 +339,12 @@ def _marker(
340339
# make sure we didn't round off to zero by accident
341340
if pgf_size == 0 and mark_size != 0:
342341
pgf_size = 1
343-
addplot_options.append("mark size=%d" % pgf_size)
342+
addplot_options.append("mark size={:d}".format(pgf_size))
344343

345344
mark_every = obj.get_markevery()
346345
if mark_every:
347346
if type(mark_every) is int:
348-
addplot_options.append("mark repeat=%d" % mark_every)
347+
addplot_options.append("mark repeat={:d}".format(mark_every))
349348
else:
350349
# python starts at index 0, pgfplots at index 1
351350
pgf_marker = [1 + m for m in mark_every]
@@ -377,7 +376,7 @@ def _marker(
377376
data, draw_xcolor, _ = mycol.mpl_color2xcolor(data, marker_edge_color)
378377
if draw_xcolor != line_xcolor:
379378
mark_options.append("draw=" + draw_xcolor)
380-
addplot_options.append("mark options={%s}" % ",".join(mark_options))
379+
addplot_options.append("mark options={{{}}}".format(",".join(mark_options)))
381380

382381
return
383382

0 commit comments

Comments
 (0)