Skip to content

Instantly share code, notes, and snippets.

@jseabold
Last active May 1, 2020 15:11

Revisions

  1. jseabold revised this gist May 1, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Notebook Plot with Spinner.py
    Original file line number Diff line number Diff line change
    @@ -56,7 +56,7 @@ def plot_some_grid(dta, filter_value):
    def update_plot(change):
    plot_some_grid(terms, change.new)

    dropdown_market.observe(update_plot, names='value')
    dropdown_options.observe(update_plot, names='value')

    display(dropdown_options)
    display(plot_output)
  2. jseabold created this gist May 1, 2020.
    63 changes: 63 additions & 0 deletions Notebook Plot with Spinner.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    """
    This took me a while to figure out so posting for posterity.
    plt.interactive(False) is important, if you want the grid to only show up
    when `display`ed. Since `sns.FacetGrid` can take a few seconds depending
    on the size of your data, this displays a spinner in the notebook cell
    until the new graph is ready to render.
    """

    import matplotlib.pyplot as plt
    import seaborn as sns

    from ipywidgets import widgets
    from IPython.display import display, clear_output, HTML

    spinner = """
    <style>
    .loader {
    border: 16px solid #f3f3f3; /* Light grey */
    border-top: 16px solid #3498db; /* Blue */
    border-radius: 50%;
    width: 120px;
    height: 120px;
    animation: spin 2s linear infinite;
    }
    @keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
    }
    </style>
    <div style="margin-left:auto;margin-right:auto;margin-top:200px;margin-bottom:200px" class="loader"></div>"""

    plt.interactive(False) # this is important
    plot_output = widgets.Output()

    dropdown_options = widgets.Dropdown(options=dta.ColumnName.unique())

    def plot_some_grid(dta, filter_value):
    with plot_output:
    clear_output(wait=True)
    display(HTML(spinner))
    clear_output(wait=True)
    grid = sns.FacetGrid(
    dta.query(f"ColumnName == '{filter_value}'"),
    col="OtherColumnName",
    col_wrap=4,
    height=3,
    aspect=2
    )
    bins = range(0, 12)
    grid.map(plt.hist, "terms", bins=bins, edgecolor='white', linewidth=1)
    plt.show()

    def update_plot(change):
    plot_some_grid(terms, change.new)

    dropdown_market.observe(update_plot, names='value')

    display(dropdown_options)
    display(plot_output)
    plot_some_grid(dta, 'FILTER VALUE')