ipywidgets with seaborn PairGrid plots: speed up performance issue

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


ipywidgets with seaborn PairGrid plots: speed up performance issue



In a Jupyter Notebook I am visualizing the Iris dataset with seaborn in combination with ipywidgets. That works fine, except that is not that fast because the plots have to be rendered every time you select a new combination of the species 'versicolor', 'virginica' and 'setosa'. See first code block.



So I tried to speed up the interaction by pre-processing the plots for each combination op species and storing them in a dictionary. See second code block.
The dictionary seems to contain all plots, but the don't show.



Any suggestions how to fix this?



First code block:


import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from ipywidgets import *

sns.set(style="white")
iris = sns.load_dataset("iris")

def iris_pg(species):
g = sns.PairGrid(iris[iris.species.isin(species)], diag_sharey=False)
g.map_lower(sns.kdeplot)
g.map_upper(sns.scatterplot)
g.map_diag(sns.kdeplot, lw=3)
return plt.show()

interact(iris_pg,
species = widgets.SelectMultiple(options=iris.species.unique(),
value=tuple(iris.species.unique()[-2:]),
rows=len(iris.species.unique()),
description='species',
disabled=False))



Second code block:


import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from ipywidgets import *
from itertools import combinations

sns.set(style="white")
iris = sns.load_dataset("iris")

from itertools import combinations
species_combinations = list()
for i in range(1, len(iris.species.unique()) + 1):
for combi in combinations(iris.species.unique(), i):
species_combinations.append(combi)
species_combinations_plot = dict()
for i in species_combinations:
species_combinations_plot[i] = sns.PairGrid(iris[iris.species.isin(i)], diag_sharey=False);
species_combinations_plot[i].map_lower(sns.kdeplot);
species_combinations_plot[i].map_upper(sns.scatterplot);
species_combinations_plot[i].map_diag(sns.kdeplot, lw=3);

def iris_pg(species):
species_combinations_plot[species]
return plt.show()

options = iris.species.unique()
value = tuple(iris.species.unique()[-2:])
rows = len(iris.species.unique())

interact(iris_pg,
species = widgets.SelectMultiple(options= options,
value=value,
rows=rows,
description='species',
disabled=False))









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived