本文介绍了在Matplotlib图形上保存交互式系列(html)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个交互式绘图示例(来自 matplotlib),我可以在其中从系列中选择要在绘图上显示的线条.这非常有效,但现在我想将其导出为 html.我可以使用 mpld3.save_html() 成功地做到这一点,但失去了系列选择的交互性.这是代码

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.

推荐答案

不可能在 mpld3 生成的html文件中保持 matplotlib.widget 交互性.这是因为 mpld3 生成的javascript运行在客户端,并且无法访问生成它的Python内核.

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.

您可以使用用于IPython笔记本的静态交互式小部件方法.

这篇关于在Matplotlib图形上保存交互式系列(html)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:00