Skip to content

Commit cb9701d

Browse files
committed
Support plotnine v0.12.1
1 parent 605060f commit cb9701d

File tree

4 files changed

+277
-78
lines changed

4 files changed

+277
-78
lines changed

patchworklib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .patchworklib import *
2-
__version__ = "0.6.0"
2+
__version__ = "0.6.1"

patchworklib/modified_grid.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
_core_docs,
2626
)
2727

28+
2829
#Referred to Seaborn, v0.11.2, Copyright (c) 2012-2021, Michael L. Waskom.
2930
def add_legend(self, legend_data=None, title=None, label_order=None,
3031
adjust_subtitles=False, **kwargs):
@@ -577,3 +578,4 @@ def __plot_for_clustergrid__(self, metric, method, colorbar_kws, row_cluster, co
577578
#mpl.rcParams["figure.subplot.hspace"] = hspace
578579
#mpl.rcParams["figure.subplot.wspace"] = wspace
579580
return self
581+

patchworklib/modified_plotnine.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from copy import deepcopy
2+
3+
def draw(self, return_ggplot=False, show: bool = False):
4+
"""
5+
Render the complete plot
6+
7+
Parameters
8+
----------
9+
show : bool (default: False)
10+
Whether to show the plot.
11+
12+
Returns
13+
-------
14+
fig : ~matplotlib.figure.Figure
15+
Matplotlib figure
16+
"""
17+
import matplotlib as mpl
18+
from plotnine._mpl.layout_engine import PlotnineLayoutEngine
19+
from plotnine.ggplot import plot_context
20+
plot_context.__exit__ = __exit__
21+
plot_context.__enter__ = __enter__
22+
# Do not draw if drawn already.
23+
# This prevents a needless error when reusing
24+
# figure & axes in the jupyter notebook.
25+
if hasattr(self, "figure"):
26+
return self.figure
27+
28+
# Prevent against any modifications to the users
29+
# ggplot object. Do the copy here as we may/may not
30+
# assign a default theme
31+
self = deepcopy(self)
32+
33+
with plot_context(self, show=show):
34+
self._build()
35+
36+
# setup
37+
figure, axs = self._create_figure()
38+
self._setup_parameters()
39+
self.theme.setup()
40+
self.facet.strips.generate()
41+
42+
# Drawing
43+
self._draw_layers()
44+
self._draw_breaks_and_labels()
45+
self._draw_legend()
46+
self._draw_figure_texts()
47+
self._draw_watermarks()
48+
49+
# Artist object theming
50+
self.theme.apply()
51+
figure.set_layout_engine(PlotnineLayoutEngine(self))
52+
53+
if return_ggplot == True:
54+
return figure, self
55+
else:
56+
return figure
57+
58+
def __enter__(self):
59+
"""
60+
Enclose in matplolib & pandas environments
61+
"""
62+
import pandas as pd
63+
import matplotlib as mpl
64+
65+
self.plot.theme._targets = {}
66+
self.rc_context = mpl.rc_context(self.plot.theme.rcParams)
67+
# Pandas deprecated is_copy, and when we create new dataframes
68+
# from slices we do not want complaints. We always uses the
69+
# new frames knowing that they are separate from the original.
70+
self.pd_option_context = pd.option_context(
71+
"mode.chained_assignment", None
72+
)
73+
self.rc_context.__enter__()
74+
self.pd_option_context.__enter__()
75+
return self
76+
77+
def __exit__(self, exc_type, exc_value, exc_traceback):
78+
"""
79+
Exit matplotlib & pandas environments
80+
"""
81+
import matplotlib.pyplot as plt
82+
83+
if exc_type is None:
84+
if self.show:
85+
plt.show()
86+
else:
87+
plt.close(self.plot.figure)
88+
else:
89+
# There is an exception, close any figure
90+
if hasattr(self.plot, "figure"):
91+
plt.close(self.plot.figure)
92+
93+
self.rc_context.__exit__(exc_type, exc_value, exc_traceback)
94+
self.pd_option_context.__exit__(exc_type, exc_value, exc_traceback)

0 commit comments

Comments
 (0)