Skip to content

Commit

Permalink
Added argument in show() to set which figure number to use
Browse files Browse the repository at this point in the history
  • Loading branch information
fkgruber committed Jun 12, 2024
1 parent e7b8fda commit e5c0bd5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
19 changes: 12 additions & 7 deletions plotnine/facets/facet.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ def __radd__(self, plot: ggplot) -> ggplot:
plot.facet.environment = plot.environment
return plot

def setup(self, plot: ggplot):
def setup(self, plot: ggplot, num: int):
self.plot = plot
self.layout = plot.layout

if hasattr(plot, "figure"):
self.figure, self.axs = plot.figure, plot.axs
else:
self.figure, self.axs = self.make_figure()
self.figure, self.axs = self.make_figure(num=num)

self.coordinates = plot.coordinates
self.theme = plot.theme
Expand Down Expand Up @@ -376,24 +376,29 @@ def __deepcopy__(self, memo: dict[Any, Any]) -> facet:

return result

def _make_figure(self) -> tuple[Figure, GridSpec]:
def _make_figure(self, num = None) -> tuple[Figure, GridSpec]:
"""
Create figure & gridspec
"""
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
if num is not None:
return plt.figure(num=num), GridSpec(self.nrow, self.ncol)
else:
return plt.figure(), GridSpec(self.nrow, self.ncol)

return plt.figure(), GridSpec(self.nrow, self.ncol)

def make_figure(self) -> tuple[Figure, list[Axes]]:
def make_figure(self, num=None) -> tuple[Figure, list[Axes]]:
"""
Create and return Matplotlib figure and subplot axes
"""
num_panels = len(self.layout.layout)
axsarr = np.empty((self.nrow, self.ncol), dtype=object)

# Create figure & gridspec
figure, gs = self._make_figure()
if num is not None:
figure, gs = self._make_figure(num)
else:
figure, gs = self._make_figure()
self.grid_spec = gs

# Create axes
Expand Down
8 changes: 4 additions & 4 deletions plotnine/ggplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ def _ipython_display_(self):
"""
self._display()

def show(self):
def show(self, num=None):
"""
Show plot using the matplotlib backend set by the user
Users should prefer this method instead of printing or repring
the object.
"""
self._display() if is_inline_backend() else self.draw(show=True)
self._display() if is_inline_backend() else self.draw(show=True, num=num)

def _display(self):
"""
Expand Down Expand Up @@ -242,7 +242,7 @@ def __rrshift__(self, other: DataLike) -> ggplot:
raise TypeError(msg.format(type(other)))
return self

def draw(self, show: bool = False) -> Figure:
def draw(self, show: bool = False, num: int = None) -> Figure:
"""
Render the complete plot
Expand Down Expand Up @@ -272,7 +272,7 @@ def draw(self, show: bool = False) -> Figure:
self._build()

# setup
self.figure, self.axs = self.facet.setup(self)
self.figure, self.axs = self.facet.setup(self,num=num)
self.guides._setup(self)
self.theme.setup(self)

Expand Down

0 comments on commit e5c0bd5

Please sign in to comment.