Skip to content

Instantly share code, notes, and snippets.

@jseabold
Last active May 1, 2020 15:11
Show Gist options
  • Save jseabold/29f8d2f72d5829d8158b5e7bf9557fe7 to your computer and use it in GitHub Desktop.
Save jseabold/29f8d2f72d5829d8158b5e7bf9557fe7 to your computer and use it in GitHub Desktop.
"""
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_options.observe(update_plot, names='value')
display(dropdown_options)
display(plot_output)
plot_some_grid(dta, 'FILTER VALUE')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment