Plot (Seaborn)

#| standalone: true
#| components: [viewer]
#| viewerHeight: 500

import seaborn as sns
from palmerpenguins import load_penguins
from shiny import App, render, ui

penguins = load_penguins()

app_ui = ui.page_fluid(
    ui.input_slider("n", "Number of bins", 1, 100, 20),
    ui.output_plot("plot"),  
)

def server(input, output, session):
    @render.plot(alt="A Seaborn histogram on penguin body mass in grams.")  
    def plot():  
        ax = sns.histplot(data=penguins, x="body_mass_g", bins=input.n())  
        ax.set_title("Palmer Penguins")
        ax.set_xlabel("Mass (g)")
        ax.set_ylabel("Count")
        return ax  

app = App(app_ui, server, debug=True)
import seaborn as sns
from palmerpenguins import load_penguins
from shiny import render
from shiny.express import input, ui

penguins = load_penguins()

ui.input_slider("n", "Number of bins", 1, 100, 20)

@render.plot(alt="A Seaborn histogram on penguin body mass in grams.")  
def plot():  
    ax = sns.histplot(data=penguins, x="body_mass_g", bins=input.n())  
    ax.set_title("Palmer Penguins")
    ax.set_xlabel("Mass (g)")
    ax.set_ylabel("Count")
    return ax  
import seaborn as sns
from palmerpenguins import load_penguins
from shiny import App, render, ui

penguins = load_penguins()

app_ui = ui.page_fluid(
    ui.input_slider("n", "Number of bins", 1, 100, 20),
    ui.output_plot("plot"),  
)

def server(input, output, session):
    @render.plot(alt="A Seaborn histogram on penguin body mass in grams.")  
    def plot():  
        ax = sns.histplot(data=penguins, x="body_mass_g", bins=input.n())  
        ax.set_title("Palmer Penguins")
        ax.set_xlabel("Mass (g)")
        ax.set_ylabel("Count")
        return ax  

app = App(app_ui, server, debug=True)
No matching items

Relevant Functions

  • ui.output_plot
    ui.output_plot(id, width='100%', height='400px', *, inline=False, click=False, dblclick=False, hover=False, brush=False, fill=MISSING)

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

No matching items

Details

Seaborn is a statistical plotting library that builds on top of Matplotlib.

Follow these steps to display a Seaborn figure in your app:

  1. Add ui.output_plot() to the UI of your app to create a div in which to display the figure. Where you call this function will determine where the figure will appear within the layout of the app. The id parameter you provide will be used to link to other parts of the Shiny app.

  2. Define a function within the server() function that creates the figure.

    • The name of the function should be the same value you passed into the id parameter in your ui.output_plot() function call in the UI.

    • If your function calls reactive values, Shiny will update your figure whenever those values change, in a reactive fashion.

    • Your function should return one of the following objects:

      • A matplotlib.figure.Figure instance
      • A matplotlib.artist.Artist instance
      • A list/tuple of Figure/Artist instances
      • An object with a ‘figure’ attribute pointing to a matplotlib.figure.Figure instance
      • A PIL.Image.Image instance
  3. Decorate your plotting function with a @render.plot() decorator.

    • If your plotting function is not the same as the id you used in the ui.output_plot(), you can add an additional @output(id=...) decorator.
    • If you use the @output() decorator, make sure it is above the @render.plot() decorator.

You can use a plot as an input widget, collecting the locations of user clicks, double clicks, hovers, and brushes. To do this, follow the instructions provided for plots as inputs.

Variations