使用滑块小部件更新 Seaborn FacetGrid

时间:2021-07-28 07:26:36

标签: python pandas matplotlib seaborn matplotlib-widget

我正在尝试动态更新使用 FacetGrid 创建的图形。我做了一个自定义绘图函数(plot_heatmap_w_scatter),我想在里面的 scatter 函数中添加一个校正因子。没有滑块它工作正常,但是当我更改滑块的值(触发更新功能)时,显然它没有清除 FacetGrid 的轴,显示相同的图。

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()

我已经尝试过使用 plt.clf() 而不是 ax.clear(),它会擦除​​整个图形,但之后它不会绘制任何内容(即使在更新中再次调用 map_dataframe 之后)。欢迎任何有关调试或如何实现 FacetGrid 更新的建议。我正在将 matplotlib.widgets 用于 ipython(不是 JupyterLab/notebooks),但如果更容易实现更新,我会考虑更改为 JupyterLab。

0 个答案:

没有答案
相关问题