Skip to content

Commit cfdf1ec

Browse files
committed
make axes.__init__ less complex
1 parent 6852001 commit cfdf1ec

File tree

1 file changed

+170
-153
lines changed

1 file changed

+170
-153
lines changed

matplotlib2tikz/axes.py

+170-153
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ def __init__(self, data, obj):
7171
# Sort the limits so make sure that the smaller of the two is actually
7272
# *min.
7373
xlim = sorted(list(obj.get_xlim()))
74-
self.axis_options.append("xmin=%.15g" % xlim[0] + ", xmax=%.15g" % xlim[1])
74+
self.axis_options.append("xmin={:.15g}, xmax={:.15g}".format(*xlim))
7575
ylim = sorted(list(obj.get_ylim()))
76-
self.axis_options.append("ymin=%.15g" % ylim[0] + ", ymax=%.15g" % ylim[1])
76+
self.axis_options.append("ymin={:.15g}, ymax={:.15g}".format(*ylim))
7777

7878
# axes scaling
7979
if obj.get_xscale() == "log":
@@ -93,6 +93,91 @@ def __init__(self, data, obj):
9393
else:
9494
aspect_num = float(aspect)
9595

96+
self._width(data, aspect_num, xlim, ylim)
97+
98+
# axis positions
99+
xaxis_pos = obj.get_xaxis().label_position
100+
if xaxis_pos == "bottom":
101+
# this is the default
102+
pass
103+
else:
104+
assert xaxis_pos == "top"
105+
self.axis_options.append("axis x line=top")
106+
107+
yaxis_pos = obj.get_yaxis().label_position
108+
if yaxis_pos == "left":
109+
# this is the default
110+
pass
111+
else:
112+
assert yaxis_pos == "right"
113+
self.axis_options.append("axis y line=right")
114+
115+
self._ticks(data, obj)
116+
117+
self._grid(self, obj, data)
118+
119+
# axis line styles
120+
# Assume that the bottom edge color is the color of the entire box.
121+
axcol = obj.spines["bottom"].get_edgecolor()
122+
data, col, _ = color.mpl_color2xcolor(data, axcol)
123+
if col != "black":
124+
self.axis_options.append("axis line style={%s}" % col)
125+
126+
# background color
127+
try:
128+
# mpl 2.*
129+
bgcolor = obj.get_facecolor()
130+
except AttributeError:
131+
# mpl 1.*
132+
bgcolor = obj.get_axis_bgcolor()
133+
134+
data, col, _ = color.mpl_color2xcolor(data, bgcolor)
135+
if col != "white":
136+
self.axis_options.append("axis background/.style={fill=%s}" % col)
137+
138+
# find color bar
139+
colorbar = _find_associated_colorbar(obj)
140+
if colorbar:
141+
self._colorbar(colorbar, data)
142+
143+
# actually print the thing
144+
if self.is_subplot:
145+
self.content.append("\\nextgroupplot")
146+
else:
147+
self.content.append("\\begin{axis}")
148+
149+
# # anchors
150+
# if hasattr(obj, '_matplotlib2tikz_anchors'):
151+
# try:
152+
# for coord, anchor_name in obj._matplotlib2tikz_anchors:
153+
# self.content.append(
154+
# '\\node (%s) at (axis cs:%e,%e) {};\n' %
155+
# (anchor_name, coord[0], coord[1])
156+
# )
157+
# except:
158+
# print('Axes attribute _matplotlib2tikz_anchors wrongly set:'
159+
# 'Expected a list of ((x,y), anchor_name), got \'%s\''
160+
# % str(obj._matplotlib2tikz_anchors)
161+
# )
162+
163+
return
164+
165+
def get_begin_code(self):
166+
content = self.content
167+
if self.axis_options:
168+
content.append("[\n" + ",\n".join(self.axis_options) + "\n]\n")
169+
return content
170+
171+
def get_end_code(self, data):
172+
if not self.is_subplot:
173+
return "\\end{axis}\n\n"
174+
elif self.is_subplot and self.nsubplots == self.subplot_index:
175+
data["is_in_groupplot_env"] = False
176+
return "\\end{groupplot}\n\n"
177+
178+
return ""
179+
180+
def _width(self, data, aspect_num, xlim, ylim):
96181
if data["fwidth"] and data["fheight"]:
97182
# width and height overwrite aspect ratio
98183
self.axis_options.append("width=" + data["fwidth"])
@@ -128,24 +213,9 @@ def __init__(self, data, obj):
128213
"but neither height nor width of the plot are given. "
129214
"Discard aspect ratio."
130215
)
216+
return
131217

132-
# axis positions
133-
xaxis_pos = obj.get_xaxis().label_position
134-
if xaxis_pos == "bottom":
135-
# this is the default
136-
pass
137-
else:
138-
assert xaxis_pos == "top"
139-
self.axis_options.append("axis x line=top")
140-
141-
yaxis_pos = obj.get_yaxis().label_position
142-
if yaxis_pos == "left":
143-
# this is the default
144-
pass
145-
else:
146-
assert yaxis_pos == "right"
147-
self.axis_options.append("axis y line=right")
148-
218+
def _ticks(self, data, obj):
149219
# get ticks
150220
self.axis_options.extend(
151221
_get_ticks(data, "x", obj.get_xticks(), obj.get_xticklabels())
@@ -215,6 +285,9 @@ def __init__(self, data, obj):
215285
self.axis_options.append(x_tick_position_string)
216286
self.axis_options.append(y_tick_position_string)
217287

288+
return
289+
290+
def _grid(self, obj, data):
218291
# Don't use get_{x,y}gridlines for gridlines; see discussion on
219292
# <http://sourceforge.net/p/matplotlib/mailman/message/25169234/>
220293
# Coordinate of the lines are entirely meaningless, but styles
@@ -243,149 +316,93 @@ def __init__(self, data, obj):
243316
if col != "black":
244317
self.axis_options.append("y grid style={%s}" % col)
245318

246-
# axis line styles
247-
# Assume that the bottom edge color is the color of the entire box.
248-
axcol = obj.spines["bottom"].get_edgecolor()
249-
data, col, _ = color.mpl_color2xcolor(data, axcol)
250-
if col != "black":
251-
self.axis_options.append("axis line style={%s}" % col)
252-
253-
# background color
254-
try:
255-
# mpl 2.*
256-
bgcolor = obj.get_facecolor()
257-
except AttributeError:
258-
# mpl 1.*
259-
bgcolor = obj.get_axis_bgcolor()
260-
261-
data, col, _ = color.mpl_color2xcolor(data, bgcolor)
262-
if col != "white":
263-
self.axis_options.append("axis background/.style={fill=%s}" % col)
264-
265-
# find color bar
266-
colorbar = _find_associated_colorbar(obj)
267-
if colorbar:
268-
colorbar_styles = []
269-
270-
orientation = colorbar.orientation
271-
limits = colorbar.get_clim()
272-
if orientation == "horizontal":
273-
self.axis_options.append("colorbar horizontal")
274-
275-
colorbar_ticks = colorbar.ax.get_xticks()
276-
colorbar_ticks_minor = colorbar.ax.get_xticks("minor")
277-
axis_limits = colorbar.ax.get_xlim()
278-
279-
# In matplotlib, the colorbar color limits are determined by
280-
# get_clim(), and the tick positions are as usual with respect
281-
# to {x,y}lim. In PGFPlots, however, they are mixed together.
282-
# Hence, scale the tick positions just like {x,y}lim are scaled
283-
# to clim.
284-
colorbar_ticks = (colorbar_ticks - axis_limits[0]) / (
285-
axis_limits[1] - axis_limits[0]
286-
) * (limits[1] - limits[0]) + limits[0]
287-
colorbar_ticks_minor = (colorbar_ticks_minor - axis_limits[0]) / (
288-
axis_limits[1] - axis_limits[0]
289-
) * (limits[1] - limits[0]) + limits[0]
290-
# Getting the labels via get_* might not actually be suitable:
291-
# they might not reflect the current state.
292-
colorbar_ticklabels = colorbar.ax.get_xticklabels()
293-
colorbar_ticklabels_minor = colorbar.ax.get_xticklabels("minor")
294-
295-
colorbar_styles.extend(
296-
_get_ticks(data, "x", colorbar_ticks, colorbar_ticklabels)
297-
)
298-
colorbar_styles.extend(
299-
_get_ticks(
300-
data, "minor x", colorbar_ticks_minor, colorbar_ticklabels_minor
301-
)
302-
)
319+
return
303320

304-
else:
305-
assert orientation == "vertical"
306-
307-
self.axis_options.append("colorbar")
308-
colorbar_ticks = colorbar.ax.get_yticks()
309-
colorbar_ticks_minor = colorbar.ax.get_yticks("minor")
310-
axis_limits = colorbar.ax.get_ylim()
311-
312-
# In matplotlib, the colorbar color limits are determined by
313-
# get_clim(), and the tick positions are as usual with respect
314-
# to {x,y}lim. In PGFPlots, however, they are mixed together.
315-
# Hence, scale the tick positions just like {x,y}lim are scaled
316-
# to clim.
317-
colorbar_ticks = (colorbar_ticks - axis_limits[0]) / (
318-
axis_limits[1] - axis_limits[0]
319-
) * (limits[1] - limits[0]) + limits[0]
320-
colorbar_ticks_minor = (colorbar_ticks_minor - axis_limits[0]) / (
321-
axis_limits[1] - axis_limits[0]
322-
) * (limits[1] - limits[0]) + limits[0]
323-
324-
# Getting the labels via get_* might not actually be suitable:
325-
# they might not reflect the current state.
326-
colorbar_ticklabels = colorbar.ax.get_yticklabels()
327-
colorbar_ylabel = colorbar.ax.get_ylabel()
328-
colorbar_ticklabels_minor = colorbar.ax.get_yticklabels("minor")
329-
colorbar_styles.extend(
330-
_get_ticks(data, "y", colorbar_ticks, colorbar_ticklabels)
331-
)
332-
colorbar_styles.extend(
333-
_get_ticks(
334-
data, "minor y", colorbar_ticks_minor, colorbar_ticklabels_minor
335-
)
321+
def _colorbar(self, colorbar, data):
322+
colorbar_styles = []
323+
324+
orientation = colorbar.orientation
325+
limits = colorbar.get_clim()
326+
if orientation == "horizontal":
327+
self.axis_options.append("colorbar horizontal")
328+
329+
colorbar_ticks = colorbar.ax.get_xticks()
330+
colorbar_ticks_minor = colorbar.ax.get_xticks("minor")
331+
axis_limits = colorbar.ax.get_xlim()
332+
333+
# In matplotlib, the colorbar color limits are determined by
334+
# get_clim(), and the tick positions are as usual with respect
335+
# to {x,y}lim. In PGFPlots, however, they are mixed together.
336+
# Hence, scale the tick positions just like {x,y}lim are scaled
337+
# to clim.
338+
colorbar_ticks = (colorbar_ticks - axis_limits[0]) / (
339+
axis_limits[1] - axis_limits[0]
340+
) * (limits[1] - limits[0]) + limits[0]
341+
colorbar_ticks_minor = (colorbar_ticks_minor - axis_limits[0]) / (
342+
axis_limits[1] - axis_limits[0]
343+
) * (limits[1] - limits[0]) + limits[0]
344+
# Getting the labels via get_* might not actually be suitable:
345+
# they might not reflect the current state.
346+
colorbar_ticklabels = colorbar.ax.get_xticklabels()
347+
colorbar_ticklabels_minor = colorbar.ax.get_xticklabels("minor")
348+
349+
colorbar_styles.extend(
350+
_get_ticks(data, "x", colorbar_ticks, colorbar_ticklabels)
351+
)
352+
colorbar_styles.extend(
353+
_get_ticks(
354+
data, "minor x", colorbar_ticks_minor, colorbar_ticklabels_minor
336355
)
337-
colorbar_styles.append("ylabel={" + colorbar_ylabel + "}")
338-
339-
mycolormap, is_custom_cmap = _mpl_cmap2pgf_cmap(colorbar.get_cmap())
340-
if is_custom_cmap:
341-
self.axis_options.append("colormap=" + mycolormap)
342-
else:
343-
self.axis_options.append("colormap/" + mycolormap)
344-
345-
self.axis_options.append("point meta min=%.15g" % limits[0])
346-
self.axis_options.append("point meta max=%.15g" % limits[1])
356+
)
347357

348-
if colorbar_styles:
349-
self.axis_options.append(
350-
"colorbar style={%s}" % ",".join(colorbar_styles)
358+
else:
359+
assert orientation == "vertical"
360+
361+
self.axis_options.append("colorbar")
362+
colorbar_ticks = colorbar.ax.get_yticks()
363+
colorbar_ticks_minor = colorbar.ax.get_yticks("minor")
364+
axis_limits = colorbar.ax.get_ylim()
365+
366+
# In matplotlib, the colorbar color limits are determined by
367+
# get_clim(), and the tick positions are as usual with respect
368+
# to {x,y}lim. In PGFPlots, however, they are mixed together.
369+
# Hence, scale the tick positions just like {x,y}lim are scaled
370+
# to clim.
371+
colorbar_ticks = (colorbar_ticks - axis_limits[0]) / (
372+
axis_limits[1] - axis_limits[0]
373+
) * (limits[1] - limits[0]) + limits[0]
374+
colorbar_ticks_minor = (colorbar_ticks_minor - axis_limits[0]) / (
375+
axis_limits[1] - axis_limits[0]
376+
) * (limits[1] - limits[0]) + limits[0]
377+
378+
# Getting the labels via get_* might not actually be suitable:
379+
# they might not reflect the current state.
380+
colorbar_ticklabels = colorbar.ax.get_yticklabels()
381+
colorbar_ylabel = colorbar.ax.get_ylabel()
382+
colorbar_ticklabels_minor = colorbar.ax.get_yticklabels("minor")
383+
colorbar_styles.extend(
384+
_get_ticks(data, "y", colorbar_ticks, colorbar_ticklabels)
385+
)
386+
colorbar_styles.extend(
387+
_get_ticks(
388+
data, "minor y", colorbar_ticks_minor, colorbar_ticklabels_minor
351389
)
390+
)
391+
colorbar_styles.append("ylabel={" + colorbar_ylabel + "}")
352392

353-
# actually print the thing
354-
if self.is_subplot:
355-
self.content.append("\\nextgroupplot")
393+
mycolormap, is_custom_cmap = _mpl_cmap2pgf_cmap(colorbar.get_cmap())
394+
if is_custom_cmap:
395+
self.axis_options.append("colormap=" + mycolormap)
356396
else:
357-
self.content.append("\\begin{axis}")
358-
359-
# # anchors
360-
# if hasattr(obj, '_matplotlib2tikz_anchors'):
361-
# try:
362-
# for coord, anchor_name in obj._matplotlib2tikz_anchors:
363-
# self.content.append(
364-
# '\\node (%s) at (axis cs:%e,%e) {};\n' %
365-
# (anchor_name, coord[0], coord[1])
366-
# )
367-
# except:
368-
# print('Axes attribute _matplotlib2tikz_anchors wrongly set:'
369-
# 'Expected a list of ((x,y), anchor_name), got \'%s\''
370-
# % str(obj._matplotlib2tikz_anchors)
371-
# )
397+
self.axis_options.append("colormap/" + mycolormap)
372398

373-
return
399+
self.axis_options.append("point meta min=%.15g" % limits[0])
400+
self.axis_options.append("point meta max=%.15g" % limits[1])
374401

375-
def get_begin_code(self):
376-
content = self.content
377-
if self.axis_options:
378-
content.append("[\n" + ",\n".join(self.axis_options) + "\n]\n")
379-
return content
380-
381-
def get_end_code(self, data):
382-
if not self.is_subplot:
383-
return "\\end{axis}\n\n"
384-
elif self.is_subplot and self.nsubplots == self.subplot_index:
385-
data["is_in_groupplot_env"] = False
386-
return "\\end{groupplot}\n\n"
402+
if colorbar_styles:
403+
self.axis_options.append("colorbar style={%s}" % ",".join(colorbar_styles))
387404

388-
return ""
405+
return
389406

390407

391408
def _get_label_rotation_and_horizontal_alignment(obj, data, axes_obj):

0 commit comments

Comments
 (0)