Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added argument in show() to speficy figure number to use #799

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 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,30 @@ 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:
plt.figure(num).clear()
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
Loading