Saving interactive series on matplotlib figures (html)

依然范特西╮ 提交于 2021-01-28 04:54:29

问题


I have an example (from matplotlib) for an interactive plot where I can select from the series which lines I want to display on the plot. This works perfectly but now I want to export this to an html. I can successfully do this with mpld3.save_html() but lose the interactivity on the series selection. Here's the code

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

Is there a way to maintain this interactivity???

I have also tried saving with pickle but still lose the series interaction.


回答1:


It is not possible to maintain the matplotlib.widget interactivity in an html file generated by mpld3. This is because the javascript generated by mpld3 runs client-side and cannot access the Python kernel that generated it.

You can achieve something like the matplotlib interactivity in an html version using the Static Interactive Widgets for IPython Notebooks approach.



来源:https://stackoverflow.com/questions/33012025/saving-interactive-series-on-matplotlib-figures-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!