render.plot

render.plot(
    self,
    _fn=None,
    *,
    alt=None,
    width=MISSING,
    height=MISSING,
    **kwargs,
)

Reactively render a plot object as an HTML image.

Parameters

alt : Optional[str] = None

Alternative text for the image if it cannot be displayed or viewed (i.e., the user uses a screen reader).

width : float | None | MISSING_TYPE = MISSING

Width of the plot in pixels. If None or MISSING, the width will be determined by the size of the corresponding output_plot. (You should not need to use this argument in most Shiny apps–set the desired width on output_plot instead.)

height : float | None | MISSING_TYPE = MISSING

Height of the plot in pixels. If None or MISSING, the height will be determined by the size of the corresponding output_plot. (You should not need to use this argument in most Shiny apps–set the desired height on output_plot instead.)

****kwargs** : object = {}

Additional keyword arguments passed to the relevant method for saving the image (e.g., for matplotlib, arguments to savefig(); for PIL and plotnine, arguments to save()).

Returns

:

A decorator for a function that returns any of the following: 1. A matplotlib.figure.Figure instance. 2. An matplotlib.artist.Artist instance. 3. A list/tuple of Figure/Artist instances. 4. An object with a ‘figure’ attribute pointing to a matplotlib.figure.Figure instance. 5. A PIL.Image.Image instance.

: It’s also possible to use the matplotlib.pyplot interface; in that case, your
: function should just call pyplot functions and not return anything. (Note that if
: the decorated function is async, then it’s not safe to use pyplot. Shiny will detect
: this case and throw an error asking you to use matplotlib’s object-oriented
: interface instead.)

Tip

The name of the decorated function (or @output(id=...)) should match the id of a output_plot container (see output_plot for example usage).

See Also

Examples

#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400

## file: app.py
import matplotlib.pyplot as plt
import numpy as np

from shiny import App, Inputs, Outputs, Session, render, ui

app_ui = ui.page_fluid(
    ui.input_slider(
        "n", "input_slider()", min=10, max=100, value=50, step=5, animate=True
    ),
    ui.output_plot("p"),
)


def server(input: Inputs, output: Outputs, session: Session):
    @render.plot
    def p():
        np.random.seed(19680801)
        x_rand = 100 + 15 * np.random.randn(437)
        fig, ax = plt.subplots()
        ax.hist(x_rand, int(input.n()), density=True)
        return fig


app = App(app_ui, server)