Skip to content

Commit

Permalink
render.plot now supports any object with a figure attribute pointing …
Browse files Browse the repository at this point in the history
…to a mpl Figure instance (#230)
  • Loading branch information
cpsievert committed Jun 29, 2022
1 parent ba8db82 commit 5a05b18
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
4 changes: 3 additions & 1 deletion shiny/render/_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ def plot(
1. A :class:`matplotlib.figure.Figure` instance.
2. An :class:`matplotlib.artist.Artist` instance.
3. A list/tuple of Figure/Artist instances.
4. A :class:`PIL.Image.Image` instance.
4. An object with a 'figure' attribute pointing to a
:class:`matplotlib.figure.Figure` instance.
5. A :class:`PIL.Image.Image` instance.
Tip
----
Expand Down
17 changes: 7 additions & 10 deletions shiny/render/_try_render_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@
import io
import os
import sys
from typing import (
Optional,
Union,
cast,
Tuple,
TextIO,
BinaryIO,
Any,
List,
)
from typing import Optional, Union, cast, Tuple, TextIO, BinaryIO, Any, List

if sys.version_info >= (3, 8):
from typing import Literal, Protocol
Expand Down Expand Up @@ -174,6 +165,12 @@ def get_matplotlib_figure(x: object) -> Union[MplFigure, None]:
if isinstance(x, Artist):
return cast(MplArtist, x).get_figure()

# Some other custom figure-like classes such as seaborn.axisgrid.FacetGrid attach
# their figure as an attribute
fig = getattr(x, "figure", None)
if isinstance(fig, Figure):
return cast(MplFigure, fig)

# Sometimes generic plot() methods will return an iterable of Artists,
# If they all refer to the same figure, then it seems reasonable to use it
# https://docs.xarray.dev/en/latest/user-guide/plotting.html#dimension-along-y-axis
Expand Down

0 comments on commit 5a05b18

Please sign in to comment.