Skip to content

Commit 6d51d04

Browse files
dougndnschloe
authored andcommitted
Respect Z-Orders (#138)
* Respect Z-Orders * Change Fancybox phash due to z-order
1 parent 7d65096 commit 6d51d04

File tree

4 files changed

+74
-14
lines changed

4 files changed

+74
-14
lines changed

matplotlib2tikz/save.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,35 @@ def _print_pgfplot_libs_message(data):
215215
print('=========================================================')
216216
return
217217

218+
class _ContentManager(object):
219+
""" Basic Content Manager for matplotlib2tikz
220+
221+
This manager uses a dictionary to map z-order to an array of content
222+
to be drawn at the z-order.
223+
"""
224+
def __init__(self):
225+
self._content = dict()
226+
227+
def extend(self, content, zorder):
228+
""" Extends with a list and a z-order
229+
"""
230+
if zorder not in self._content:
231+
self._content[zorder] = []
232+
self._content[zorder].extend(content)
233+
234+
def flatten(self):
235+
content_out = []
236+
all_z = sorted(self._content.keys())
237+
for z in all_z:
238+
content_out.extend(self._content[z])
239+
return content_out
240+
218241

219242
def _recurse(data, obj):
220243
'''Iterates over all children of the current object, gathers the contents
221244
contributing to the resulting PGFPlots file, and returns those.
222245
'''
223-
content = []
246+
content = _ContentManager()
224247
for child in obj.get_children():
225248
if isinstance(child, mpl.axes.Axes):
226249
# Reset 'extra axis options' for every new Axes environment.
@@ -234,34 +257,35 @@ def _recurse(data, obj):
234257
if data['extra axis options']:
235258
ax.axis_options.extend(data['extra axis options'])
236259
# populate content
237-
content.extend(ax.get_begin_code())
238-
content.extend(children_content)
239-
content.extend(ax.get_end_code(data))
260+
content.extend(
261+
ax.get_begin_code() +
262+
children_content +
263+
[ax.get_end_code(data)], 0)
240264
elif isinstance(child, mpl.lines.Line2D):
241265
data, cont = line2d.draw_line2d(data, child)
242-
content.extend(cont)
266+
content.extend(cont, child.get_zorder())
243267
elif isinstance(child, mpl.image.AxesImage):
244268
data, cont = img.draw_image(data, child)
245-
content.extend(cont)
269+
content.extend(cont, child.get_zorder())
246270
# # Really necessary?
247271
# data, children_content = _recurse(data, child)
248272
# content.extend(children_content)
249273
elif isinstance(child, mpl.patches.Patch):
250274
data, cont = patch.draw_patch(data, child)
251-
content.extend(cont)
275+
content.extend(cont, child.get_zorder())
252276
elif isinstance(child, mpl.collections.PatchCollection) or \
253277
isinstance(child, mpl.collections.PolyCollection):
254278
data, cont = patch.draw_patchcollection(data, child)
255-
content.extend(cont)
279+
content.extend(cont, child.get_zorder())
256280
elif isinstance(child, mpl.collections.PathCollection):
257281
data, cont = path.draw_pathcollection(data, child)
258-
content.extend(cont)
282+
content.extend(cont, child.get_zorder())
259283
elif isinstance(child, mpl.collections.LineCollection):
260284
data, cont = line2d.draw_linecollection(data, child)
261-
content.extend(cont)
285+
content.extend(cont, child.get_zorder())
262286
elif isinstance(child, mpl.collections.QuadMesh):
263287
data, cont = qmsh.draw_quadmesh(data, child)
264-
content.extend(cont)
288+
content.extend(cont, child.get_zorder())
265289
elif isinstance(child, mpl.legend.Legend):
266290
data = legend.draw_legend(data, child)
267291
elif isinstance(child, mpl.axis.XAxis) or \
@@ -276,5 +300,5 @@ def _recurse(data, obj):
276300
if isinstance(obj, mpl.axes.Subplot) or isinstance(obj, mpl.figure.Figure):
277301
for text in obj.texts:
278302
data, cont = mytext.draw_text(data, text)
279-
content.extend(cont)
280-
return data, content
303+
content.extend(cont, text.get_zorder())
304+
return data, content.flatten()

test/testfunctions/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'basic_sin',
66
'boxplot',
77
'barchart_legend',
8+
'barchart_errorbars',
89
'barchart',
910
'dual_axis',
1011
'errorband',
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
""" Bar Chart With Errorbar test
3+
4+
This tests plots a bar chart with error bars. The errorbars need to be drawn
5+
at the correct z-order to be sucessful.
6+
7+
"""
8+
desc = 'Bar Chart With Errorbars'
9+
phash = '5f09a9e4b37287c2'
10+
11+
def plot():
12+
import matplotlib.pyplot as plt
13+
import numpy as np
14+
15+
# plot data
16+
fig = plt.figure()
17+
ax = fig.add_subplot(111)
18+
19+
x = np.arange(3)
20+
y1 = [1, 2, 3]
21+
y1err = [0.1, 0.2, 0.5]
22+
y2 = [3, 2, 4]
23+
y2err = [0.4, 0.2, 0.5]
24+
y3 = [5, 3, 1]
25+
y3err = [0.1, 0.2, 0.1]
26+
w = 0.25
27+
28+
errBarStyle = dict(ecolor='black', lw=5, capsize=8, capthick=5)
29+
30+
ax.bar(x-w, y1, w, color='b', yerr=y1err, align='center', error_kw=errBarStyle)
31+
ax.bar(x, y2, w, color='g', yerr=y2err, align='center', error_kw=errBarStyle)
32+
ax.bar(x+w, y3, w, color='r', yerr=y3err, align='center', error_kw=errBarStyle)
33+
34+
return fig
35+

test/testfunctions/fancybox.py

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

99
desc = 'Fancybox'
1010
# phash = 'dd232dd0239dd85a'
11-
phash = '9d2325dc23cdd85a'
11+
phash = '9d2327dc23cdd81a'
1212

1313

1414
# Bbox object around which the fancy box will be drawn.

0 commit comments

Comments
 (0)