IPython.html.widgets:为以后保存值

IPython.html.widgets: saving the value for later

我开始使用 IPython.html.widgets 来探索各种参数对分布的影响。我想使用其中一个值绘制一个图形。例如:

def myHistogram(bins):
    plt.hist(mydata,bins) 
    return fig

from IPython.html.widgets import interact
interact(myHistogram,bins = (10,50,5))

fig=plt.gcf()

例如,在检查我的分布后,我得出结论,对于这个特定案例,我想要 25 个箱子。

fig.savefig(fig_name.jpg)

保存默认绘图。有什么方法可以强制它使用滑块的最后一个值来保存图形吗?

我认为问题在于plt.hist()每次都创建一个新图形,因此您的fig变量保持不变。如果您改为修改图形,它应该可以工作:

# Get the figure and axis
fig, ax = plt.subplots()

def myHistogram(bins):
    # Clear any previous data, and plot the new histogram
    ax.clear()
    ax.hist(mydata, bins)
    fig.show()  # Seems to be necessary

interact(myHistogram, bins=(10, 50, 5))

(我还没有实际测试过)