Skip to content

Commit f91b5d0

Browse files
committed
add comback function
1 parent de578b9 commit f91b5d0

1 file changed

Lines changed: 40 additions & 42 deletions

File tree

patchworklib/patchworklib.py

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,14 @@ def __init__(self, bricks_dict=None, ggplot=None, label=None):
907907
_axes_dict[self._case.get_label()] = self._case
908908
self._case_labels = [self._case.get_label()]
909909
self._parent = None
910+
911+
def comeback(self):
912+
global _removed_axes
913+
fig = Brick._figure
914+
for label, ax in _removed_axes.items():
915+
ax.figure = fig
916+
fig.add_axes(ax)
917+
_removed_axes = {}
910918

911919
def get_label(self):
912920
return self._label
@@ -1035,13 +1043,7 @@ def savefig(self, fname=None, transparent=None, quick=True, **kwargs):
10351043
return fig
10361044

10371045
def __or__(self, other):
1038-
global _removed_axes
1039-
fig = Brick._figure
1040-
for label, ax in _removed_axes.items():
1041-
ax.figure = fig
1042-
fig.add_axes(ax)
1043-
_removed_axes = {}
1044-
1046+
self.comeback()
10451047
if other._type == "spacer":
10461048
return other.__ror__(self)
10471049

@@ -1056,13 +1058,7 @@ def __or__(self, other):
10561058
return hstack(self, other)
10571059

10581060
def __truediv__(self, other):
1059-
global _removed_axes
1060-
fig = Brick._figure
1061-
for label, ax in _removed_axes.items():
1062-
ax.figure = fig
1063-
fig.add_axes(ax)
1064-
_removed_axes = {}
1065-
1061+
self.comeback()
10661062
if other._type == "spacer":
10671063
return other.__rtruediv__(self)
10681064

@@ -1211,18 +1207,29 @@ def __init__(self, label=None, figsize=None, ax=None):
12111207
self._case.set_yticks([])
12121208
_axes_dict[self._case.get_label()] = self._case
12131209
self._case_labels = [self._case.get_label()]
1210+
1211+
def comeback(self):
1212+
global _removed_axes
1213+
fig = Brick._figure
1214+
for label, ax in _removed_axes.items():
1215+
ax.figure = fig
1216+
fig.add_axes(ax)
1217+
_removed_axes = {}
12141218

12151219
def get_inner_corner(self, labels=None):
1220+
self.comeback()
12161221
pos = self.get_position()
12171222
return pos.x0, pos.x1, pos.y0, pos.y1
12181223

12191224
def get_middle_corner(self, labels=None):
1225+
self.comeback()
12201226
h, v = Brick._figure.get_size_inches()
12211227
pos = self.get_tightbbox(Brick._figure.canvas.get_renderer())
12221228
pos = TransformedBbox(pos, Affine2D().scale(1./Brick._figure.dpi))
12231229
return pos.x0/h, pos.x1/h, pos.y0/v, pos.y1/v
12241230

12251231
def get_outer_corner(self, labes=None):
1232+
self.comeback()
12261233
h, v = Brick._figure.get_size_inches()
12271234
pos1 = self.get_tightbbox(Brick._figure.canvas.get_renderer())
12281235
pos1 = TransformedBbox(pos1, Affine2D().scale(1./Brick._figure.dpi))
@@ -1276,6 +1283,7 @@ def savefig(self, fname=None, transparent=None, quick=True, **kwargs):
12761283
return fig
12771284

12781285
def change_aspectratio(self, new_size):
1286+
self.comeback()
12791287
if type(new_size) == tuple or type(new_size) == list:
12801288
self.set_position([0, 0, new_size[0], new_size[1]])
12811289
self._originalsize = new_size
@@ -1285,20 +1293,15 @@ def change_aspectratio(self, new_size):
12851293
reset_ggplot_legend(self)
12861294

12871295
def move_legend(self, new_loc, **kws):
1296+
self.comeback()
12881297
old_legend = self.legend_
12891298
handles = old_legend.legendHandles
12901299
labels = [t.get_text() for t in old_legend.get_texts()]
12911300
title = old_legend.get_title().get_text()
12921301
self.legend(handles, labels, loc=new_loc, title=title, **kws)
12931302

12941303
def __or__(self, other):
1295-
global _removed_axes
1296-
fig = Brick._figure
1297-
for label, ax in _removed_axes.items():
1298-
ax.figure = fig
1299-
fig.add_axes(ax)
1300-
_removed_axes = {}
1301-
1304+
self.comeback()
13021305
if other._type == "spacer":
13031306
return other.__ror__(self)
13041307

@@ -1313,13 +1316,7 @@ def __or__(self, other):
13131316
return hstack(self, other)
13141317

13151318
def __truediv__(self, other):
1316-
global _removed_axes
1317-
fig = Brick._figure
1318-
for label, ax in _removed_axes.items():
1319-
ax.figure = fig
1320-
fig.add_axes(ax)
1321-
_removed_axes = {}
1322-
1319+
self.comeback()
13231320
if other._type == "spacer":
13241321
return other.__rtruediv__(self)
13251322

@@ -1448,18 +1445,29 @@ def __init__(self, label=None, figsize=None, ax=None):
14481445
self._case.set_yticks([])
14491446
_axes_dict[self._case.get_label()] = self._case
14501447
self._case_labels = [self._case.get_label()]
1448+
1449+
def comeback(self):
1450+
global _removed_axes
1451+
fig = Brick._figure
1452+
for label, ax in _removed_axes.items():
1453+
ax.figure = fig
1454+
fig.add_axes(ax)
1455+
_removed_axes = {}
14511456

14521457
def get_inner_corner(self, labels=None):
1458+
self.comeback()
14531459
pos = self.get_position()
14541460
return pos.x0, pos.x1, pos.y0, pos.y1
14551461

14561462
def get_middle_corner(self, labels=None):
1463+
self.comeback()
14571464
h, v = Brick._figure.get_size_inches()
14581465
pos = self.get_tightbbox(Brick._figure.canvas.get_renderer())
14591466
pos = TransformedBbox(pos, Affine2D().scale(1./Brick._figure.dpi))
14601467
return pos.x0/h, pos.x1/h, pos.y0/v, pos.y1/v
14611468

14621469
def get_outer_corner(self, labes=None):
1470+
self.comeback()
14631471
h, v = Brick._figure.get_size_inches()
14641472
pos1 = self.get_tightbbox(Brick._figure.canvas.get_renderer())
14651473
pos1 = TransformedBbox(pos1, Affine2D().scale(1./Brick._figure.dpi))
@@ -1513,6 +1521,7 @@ def savefig(self, fname=None, transparent=None, quick=True, **kwargs):
15131521
return fig
15141522

15151523
def change_aspectratio(self, new_size):
1524+
self.comeback()
15161525
if type(new_size) == tuple or type(new_size) == list:
15171526
self.set_position([0, 0, new_size[0], new_size[1]])
15181527
self._originalsize = new_size
@@ -1522,20 +1531,15 @@ def change_aspectratio(self, new_size):
15221531
reset_ggplot_legend(self)
15231532

15241533
def move_legend(self, new_loc, **kws):
1534+
self.comeback()
15251535
old_legend = self.legend_
15261536
handles = old_legend.legendHandles
15271537
labels = [t.get_text() for t in old_legend.get_texts()]
15281538
title = old_legend.get_title().get_text()
15291539
self.legend(handles, labels, loc=new_loc, title=title, **kws)
15301540

15311541
def __or__(self, other):
1532-
global _removed_axes
1533-
fig = Brick._figure
1534-
for label, ax in _removed_axes.items():
1535-
ax.figure = fig
1536-
fig.add_axes(ax)
1537-
_removed_axes = {}
1538-
1542+
self.comeback()
15391543
if other._type == "spacer":
15401544
return other.__ror__(self)
15411545

@@ -1550,13 +1554,7 @@ def __or__(self, other):
15501554
return hstack(self, other)
15511555

15521556
def __truediv__(self, other):
1553-
global _removed_axes
1554-
fig = Brick._figure
1555-
for label, ax in _removed_axes.items():
1556-
ax.figure = fig
1557-
fig.add_axes(ax)
1558-
_removed_axes = {}
1559-
1557+
self.comeback()
15601558
if other._type == "spacer":
15611559
return other.__rtruediv__(self)
15621560

0 commit comments

Comments
 (0)