我正在寻找一种方法来改变垂直IntSlider的位置在matplotlib图的右侧。下面是代码:如何将ipywidget滑块的默认位置更改为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
然后包裹VBox
与IPython.display
并没有奏效。
在示例中找不到直接解决方案。
EDIT1:
ipywidgets提供一种Output()
类捕获的输出区域和使用它的插件上下文内。
我会试着弄清楚如何使用它。
这是对象: https://github.com/jupyter-widgets/ipywidgets/blob/master/ipywidgets/widgets/widget_output.py
你碰巧试过我的解决方案吗? –
是的,我认为你是在正确的轨道上。如果你找到一种方法,通过从update_plot函数中展开图形和坐标轴的结构来实现它,我会接受你的答案。 在Output()对象上花费很多。我认为这将解决我们的问题。 –