保存matplotlib图形的交互式系列(html)



我有一个交互式绘图的示例(来自matplotlib),我可以从系列中选择想要在绘图上显示的行。这工作完美,但现在我想把它导出到html。我可以用mpld3.save_html()成功地做到这一点,但在系列选择上失去了交互性。下面是代码

import numpy as np
import matplotlib.pyplot as plt, mpld3
from matplotlib.widgets import CheckButtons
t = np.arange(0.0, 2.0, 0.01)
s0 = np.sin(2*np.pi*t)
s1 = np.sin(4*np.pi*t)
s2 = np.sin(6*np.pi*t)
fig, ax = plt.subplots()
l0, = ax.plot(t, s0, visible=False, lw=2)
l1, = ax.plot(t, s1, lw=2)
l2, = ax.plot(t, s2, lw=2)
plt.subplots_adjust(left=0.2)
rax = plt.axes([0.05, 0.4, 0.1, 0.15])
check = CheckButtons(rax, ('2 Hz', '4 Hz', '6 Hz'), (False, True, True))
def func(label):
    if label == '2 Hz': l0.set_visible(not l0.get_visible())
    elif label == '4 Hz': l1.set_visible(not l1.get_visible())
    elif label == '6 Hz': l2.set_visible(not l2.get_visible())
    plt.draw()
check.on_clicked(func)
mpld3.save_html(fig, 'interactive_fig.html') #save to html here
plt.show()

是否有办法维持这种互动性??

我也试过用pickle保存,但仍然失去了系列交互

不可能在mpld3生成的html文件中维护matplotlib.widget的交互性。这是因为mpld3生成的javascript运行在客户端,不能访问生成它的Python内核。

您可以使用IPython notebook的静态交互小部件方法在html版本中实现类似matplotlib的交互性。

最新更新