|
| 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