Skip to content

Commit db866fb

Browse files
committed
make draw_line2d less complex
1 parent de539b2 commit db866fb

File tree

1 file changed

+120
-90
lines changed

1 file changed

+120
-90
lines changed

matplotlib2tikz/line2d.py

+120-90
Original file line numberDiff line numberDiff line change
@@ -59,47 +59,16 @@ def draw_line2d(data, obj):
5959
data, obj.get_marker(), marker_face_color
6060
)
6161
if marker:
62-
addplot_options.append("mark=" + marker)
63-
64-
mark_size = obj.get_markersize()
65-
if mark_size:
66-
# setting half size because pgfplots counts the radius/half-width
67-
pgf_size = int(0.5 * mark_size)
68-
# make sure we didn't round off to zero by accident
69-
if pgf_size == 0 and mark_size != 0:
70-
pgf_size = 1
71-
addplot_options.append("mark size=%d" % pgf_size)
72-
73-
mark_every = obj.get_markevery()
74-
if mark_every:
75-
addplot_options.append("mark repeat=%d" % mark_every)
76-
77-
mark_options = ["solid"]
78-
if extra_mark_options:
79-
mark_options.append(extra_mark_options)
80-
if marker_face_color is None or (
81-
isinstance(marker_face_color, six.string_types)
82-
and marker_face_color == "none"
83-
):
84-
mark_options.append("fill opacity=0")
85-
else:
86-
data, face_xcolor, _ = mycol.mpl_color2xcolor(data, marker_face_color)
87-
if face_xcolor != line_xcolor:
88-
mark_options.append("fill=" + face_xcolor)
89-
90-
face_and_edge_have_equal_color = marker_edge_color == marker_face_color
91-
# Sometimes, the colors are given as arrays. Collapse them into a
92-
# single boolean.
93-
try:
94-
face_and_edge_have_equal_color = all(face_and_edge_have_equal_color)
95-
except TypeError:
96-
pass
97-
98-
if not face_and_edge_have_equal_color:
99-
data, draw_xcolor, _ = mycol.mpl_color2xcolor(data, marker_edge_color)
100-
if draw_xcolor != line_xcolor:
101-
mark_options.append("draw=" + draw_xcolor)
102-
addplot_options.append("mark options={%s}" % ",".join(mark_options))
62+
_marker(
63+
obj,
64+
data,
65+
marker,
66+
addplot_options,
67+
extra_mark_options,
68+
marker_face_color,
69+
marker_edge_color,
70+
line_xcolor,
71+
)
10372

10473
if marker and not show_line:
10574
addplot_options.append("only marks")
@@ -115,55 +84,7 @@ def draw_line2d(data, obj):
11584
options = ", ".join(addplot_options)
11685
content.append("[" + options + "]\n")
11786

118-
content.append("table {%\n")
119-
120-
# nschloe, Oct 2, 2015:
121-
# The transform call yields warnings and it is unclear why. Perhaps
122-
# the input data is not suitable? Anyhow, this should not happen.
123-
# Comment out for now.
124-
# xdata, ydata = _transform_to_data_coordinates(obj, *obj.get_data())
125-
xdata, ydata = obj.get_data()
126-
127-
# matplotlib allows plotting of data containing `astropy.units`, but they will
128-
# break the formatted string here. Try to strip the units from the data.
129-
try:
130-
xdata = xdata.value
131-
except AttributeError:
132-
pass
133-
try:
134-
ydata = ydata.value
135-
except AttributeError:
136-
pass
137-
138-
try:
139-
has_mask = ydata.mask.any()
140-
except AttributeError:
141-
has_mask = 0
142-
143-
plot_table = []
144-
if has_mask:
145-
# matplotlib jumps at masked images, while PGFPlots by default
146-
# interpolates. Hence, if we have a masked plot, make sure that
147-
# PGFPlots jumps as well.
148-
data["extra axis options"].add("unbounded coords=jump")
149-
for (x, y, is_masked) in zip(xdata, ydata, ydata.mask):
150-
if is_masked:
151-
plot_table.append("%.15g\tnan\n" % x)
152-
else:
153-
plot_table.append("%.15g\t%.15g\n" % (x, y))
154-
else:
155-
for (x, y) in zip(xdata, ydata):
156-
plot_table.append("%.15g\t%.15g\n" % (x, y))
157-
if data["externalize tables"]:
158-
filename, rel_filepath = files.new_filename(data, "table", ".tsv")
159-
with open(filename, "w") as f:
160-
# No encoding handling required: plot_table is only ASCII
161-
f.write("".join(plot_table))
162-
content.append(rel_filepath)
163-
else:
164-
content.extend(plot_table)
165-
166-
content.append("};\n")
87+
_table(obj, content, data)
16788

16889
return data, content
16990

@@ -398,3 +319,112 @@ def _mpl_linestyle2pgfp_linestyle(line_style):
398319
# % e
399320
# )
400321
# return (xdata, ydata)
322+
323+
324+
def _marker(
325+
obj,
326+
data,
327+
marker,
328+
addplot_options,
329+
extra_mark_options,
330+
marker_face_color,
331+
marker_edge_color,
332+
line_xcolor,
333+
):
334+
addplot_options.append("mark=" + marker)
335+
336+
mark_size = obj.get_markersize()
337+
if mark_size:
338+
# setting half size because pgfplots counts the radius/half-width
339+
pgf_size = int(0.5 * mark_size)
340+
# make sure we didn't round off to zero by accident
341+
if pgf_size == 0 and mark_size != 0:
342+
pgf_size = 1
343+
addplot_options.append("mark size=%d" % pgf_size)
344+
345+
mark_every = obj.get_markevery()
346+
if mark_every:
347+
addplot_options.append("mark repeat=%d" % mark_every)
348+
349+
mark_options = ["solid"]
350+
if extra_mark_options:
351+
mark_options.append(extra_mark_options)
352+
if marker_face_color is None or (
353+
isinstance(marker_face_color, six.string_types) and marker_face_color == "none"
354+
):
355+
mark_options.append("fill opacity=0")
356+
else:
357+
data, face_xcolor, _ = mycol.mpl_color2xcolor(data, marker_face_color)
358+
if face_xcolor != line_xcolor:
359+
mark_options.append("fill=" + face_xcolor)
360+
361+
face_and_edge_have_equal_color = marker_edge_color == marker_face_color
362+
# Sometimes, the colors are given as arrays. Collapse them into a
363+
# single boolean.
364+
try:
365+
face_and_edge_have_equal_color = all(face_and_edge_have_equal_color)
366+
except TypeError:
367+
pass
368+
369+
if not face_and_edge_have_equal_color:
370+
data, draw_xcolor, _ = mycol.mpl_color2xcolor(data, marker_edge_color)
371+
if draw_xcolor != line_xcolor:
372+
mark_options.append("draw=" + draw_xcolor)
373+
addplot_options.append("mark options={%s}" % ",".join(mark_options))
374+
375+
return
376+
377+
378+
def _table(obj, content, data):
379+
content.append("table {%\n")
380+
381+
# nschloe, Oct 2, 2015:
382+
# The transform call yields warnings and it is unclear why. Perhaps
383+
# the input data is not suitable? Anyhow, this should not happen.
384+
# Comment out for now.
385+
# xdata, ydata = _transform_to_data_coordinates(obj, *obj.get_data())
386+
xdata, ydata = obj.get_data()
387+
388+
# matplotlib allows plotting of data containing `astropy.units`, but they will
389+
# break the formatted string here. Try to strip the units from the data.
390+
try:
391+
xdata = xdata.value
392+
except AttributeError:
393+
pass
394+
try:
395+
ydata = ydata.value
396+
except AttributeError:
397+
pass
398+
399+
try:
400+
has_mask = ydata.mask.any()
401+
except AttributeError:
402+
has_mask = 0
403+
404+
plot_table = []
405+
if has_mask:
406+
# matplotlib jumps at masked images, while PGFPlots by default
407+
# interpolates. Hence, if we have a masked plot, make sure that
408+
# PGFPlots jumps as well.
409+
data["extra axis options"].add("unbounded coords=jump")
410+
for (x, y, is_masked) in zip(xdata, ydata, ydata.mask):
411+
if is_masked:
412+
plot_table.append("%.15g\tnan\n" % x)
413+
else:
414+
plot_table.append("%.15g\t%.15g\n" % (x, y))
415+
else:
416+
for (x, y) in zip(xdata, ydata):
417+
plot_table.append("%.15g\t%.15g\n" % (x, y))
418+
419+
if data["externalize tables"]:
420+
filename, rel_filepath = files.new_filename(data, "table", ".tsv")
421+
with open(filename, "w") as f:
422+
# No encoding handling required: plot_table is only ASCII
423+
f.write("".join(plot_table))
424+
content.append(rel_filepath)
425+
else:
426+
content.extend(plot_table)
427+
428+
content.append("};\n")
429+
430+
return

0 commit comments

Comments
 (0)