【发布时间】:2017-08-02 15:02:02
【问题描述】:
我正在寻找一种方法来将垂直 IntSlider 的位置更改为 matplotlib 图的右侧。代码如下:
from ipywidgets import interact, fixed, IntSlider
import numpy as np
from matplotlib import pyplot as plt
%matplotlib notebook
fig = plt.figure(figsize=(8,4))
xs = np.random.random_integers(0, 5000, 50)
ys = np.random.random_integers(0, 5000, 50)
ax = fig.add_subplot(111)
scat, = ax.plot(xs, ys, 'kx', markersize=1)
ax.grid(which='both', color='.25', lw=.1)
ax.set_aspect('equal'), ax.set_title('Rotate')
def rotate(theta, xs, ys):
new_xs = xs * np.cos(np.deg2rad(theta)) - ys * np.sin(np.deg2rad(theta))
new_xs -= new_xs.min()
new_ys = xs * np.sin(np.deg2rad(theta)) + ys * np.cos(np.deg2rad(theta))
new_ys -= new_ys.min()
return new_xs, new_ys
def update_plot(theta, xs, ys):
new_xs, new_ys = rotate(theta, xs, ys)
scat.set_xdata(new_xs), scat.set_ydata(new_ys)
ax.set_xlim(new_xs.min() - 500, new_xs.max() + 500)
ax.set_ylim(new_ys.min() - 500, new_ys.max() + 500)
w = interact(update_plot,
theta=IntSlider(min=-180, max=180, step=5,value=0, orientation='vertical'),
xs=fixed(xs),
ys=fixed(ys))
这就是我所拥有的:
这就是我想要的:
可能有一个非常简单的方法可以做到这一点,但我自己想不通。
我尝试将fig 和interactive 小部件都放入VBox,然后用IPython.display 包装VBox,但没有成功。
在示例中找不到直接的解决方案。
编辑1:
ipywidgets 提供了一个Output() 类,用于捕获输出区域并在小部件上下文中使用它。
我会尝试弄清楚如何使用它。
这是对象: https://github.com/jupyter-widgets/ipywidgets/blob/master/ipywidgets/widgets/widget_output.py
【问题讨论】:
-
您是否碰巧尝试了我的解决方案?
-
是的,我认为您走在正确的轨道上。如果您找到一种方法通过从 update_plot 函数中解开图形和轴的构造来使其工作,我将接受您的回答。在 Output() 对象上花很多时间。我认为这将解决我们的问题。
标签: python matplotlib jupyter ipywidgets