【问题标题】:Saving interactive series on matplotlib figures (html)在 matplotlib 图形 (html) 上保存交互式系列
【发布时间】:2015-10-08 09:39:47
【问题描述】:

我有一个交互式绘图示例(来自 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 保存,但仍然丢失了系列交互。

【问题讨论】:

    标签: python matplotlib mpld3


    【解决方案1】:

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

    您可以使用Static Interactive Widgets for IPython Notebooks 方法在html 版本中实现类似matplotlib 的交互性。

    【讨论】:

    • 感谢您的评论。 python中是否还有其他模块可以生成这些交互式绘图?
    • 也可以查看bokeh 项目。
    • 谢谢。我看过bokeh,但找不到任何交互式系列选择。有什么建议吗?
    • 也可以查看 plotly (plot.ly/python)。当您将笔记本保存为 html 文件时,绘图会保持交互。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 2012-07-30
    • 2012-02-26
    • 1970-01-01
    • 2013-04-17
    • 2017-10-26
    相关资源
    最近更新 更多