I'm trying to dynamically update a figure created using FacetGrid. I made a custom plotting function (plot_heatmap_w_scatter), in which I want to add a correction factor to the scatter function inside. Without the slider it works alright, but when I change the value of the slider (triggering the update function), apparently it doesn't clear the axes of the FacetGrid, showing the same plots.
import seaborn as sns
from matplotlib.widgets import Slider
def plot_heatmap_w_scatter(data, **kws):
correction = kws['correction']
tmp = data.pivot(index='k', columns='x0', values='pdf')
sns.heatmap(tmp)
plt.scatter(another_x - correction, another_y)
f = sns.FacetGrid(data=df, row='context')
f.map_dataframe(plot_heatmap_w_scatter)
plt.figure(f.fig.number)
plt.subplots_adjust(bottom=0.25)
ax_slider = plt.axes([0.25, 0.1, 0.65, 0.03])
slider = Slider(label='correction',
ax=ax_slider, valmin=0, valmax=20)
def update(val):
for ax in f.axes.flat:
ax.clear()
f.map_dataframe(plot_heatmap_w_scatter, kws={'correction': val})
slider.on_changed(update)
f.fig.show()
I've tried with plt.clf()
instead of ax.clear()
, and it erases the full figure, but it doesn't draw anything after (even after calling map_dataframe again in update). Any suggestions on debugging or how to achieve an update of the FacetGrid are welcome. I'm using matplotlib.widgets into ipython (not JupyterLab/notebooks), but would consider changing to JupyterLab if it's easier to achieve the update.