2017-08-02 97 views
1

我正在寻找一种方法来改变垂直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)) 

这是我有:

enter image description here

这就是我想要的:

enter image description here

有可能是一个非常简单的方式做到这一点,但我无法弄清楚自己。

我试图把双方figinteractive部件成VBox然后包裹VBoxIPython.display并没有奏效。

在示例中找不到直接解决方案。

EDIT1:

ipywidgets提供一种Output()类捕获的输出区域和使用它的插件上下文内。

我会试着弄清楚如何使用它。

这是对象: https://github.com/jupyter-widgets/ipywidgets/blob/master/ipywidgets/widgets/widget_output.py

+0

你碰巧试过我的解决方案吗? –

+1

是的,我认为你是在正确的轨道上。如果你找到一种方法,通过从update_plot函数中展开图形和坐标轴的结构来实现它,我会接受你的答案。 在Output()对象上花费很多。我认为这将解决我们的问题。 –

回答

1

您可以通过创建一个互动的小部件,然后加载childrenHBox解决这个问题。互动的子部件遵循这个约定; (widget_0,widget_1 ...,output),其中元组的最后一个成员是控件的输出。您可以在声明之前或之后定义HBox的布局。 Read more on the layouts available here

以下解决方案有一些注意事项;图形可能不会在最初出现时显示出来,您可能必须在控件出现之前对其进行调整,其次使用魔术时控件可能导致更新时发生大量闪烁。除此之外,我认为这应该像你想要的那样工作;

from IPython.display import display 
from ipywidgets import interactive, fixed, IntSlider, HBox, Layout 
import numpy as np 
import matplotlib.pylab as plt 
%matplotlib notebook 

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): 
    fig = plt.figure(figsize=(8,4)) 
    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') 
    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) 

xs = np.random.randint(0, 5000, 50) 
ys = np.random.randint(0, 5000, 50) 
w = interactive(update_plot, 
       theta=IntSlider(min=-180, max=180, step=5, value=0,orientation='vertical'), 
       xs=fixed(xs), 
       ys=fixed(ys)) 

# Define the layout here. 
box_layout = Layout(display='flex', flex_flow='row', justify_content='space-between', align_items='center') 

display(HBox([w.children[1],w.children[0]], layout=box_layout)) 

更新:

这是从ipywidgets gitter贾森灌浆的解决方案。

from IPython.display import display, clear_output 
from ipywidgets import interact, fixed, IntSlider, HBox, Layout, Output, VBox 
import numpy as np 
import matplotlib.pyplot as plt 
%matplotlib inline 

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 

out = Output(layout={'width': '300px', 'height': '300px'}) 

def update_plot(change): 
    theta = change['new'] # new slider value 
    with out: 
     clear_output(wait=True) 
     fig = plt.figure(figsize=(4,4)) 
     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') 
     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) 
     plt.show() 

xs = np.random.randint(0, 5000, 50) 
ys = np.random.randint(0, 5000, 50) 

slider = IntSlider(min=-180, max=180, step=5, value=0, orientation='vertical') 
slider.observe(update_plot, 'value') 
update_plot({'new': slider.value}) 
display(HBox([out, slider])) 
+1

你部分解决了这个问题,但是在update_plot函数中包装了matplotlib图的结构导致了这种不好的刷新行为,并且该图在第一次更新滑块时生成。可能有一种方法可以通过它内部的matplotlib捕获输出。 选中此项: https://github.com/jupyter-widgets/ipywidgets/blob/master/ipywidgets/widgets/widget_output.py –

+0

@BrunoRuasDePinho闪烁可能与您使用的浏览器或版本有关您正在使用的ipywidgets正如[本期]中所述(https://github.com/jupyter-widgets/ipywidgets/issues/1532)。使用'%matplotlib inline'代替''matplotlib notebook''可以看到一些改进,但是你使用了一些交互功能。所以IDK可能会像现在这样好。我会继续寻找,但至少让我对局部soln :) upvote –

+1

肯定詹姆斯!谢谢!! –

2

我决定用bqplot代替matplotlib要尝试这个例子,它竟然是方式更加简单。

import numpy as np 
from bqplot import pyplot as plt 
from IPython.display import display 
from ipywidgets import interactive, fixed, IntSlider, HBox, Layout 

plt.figure(min_aspect_ratio=1, max_aspect_ratio=1) 

xs = np.random.randint(0, 5000 + 1, 100) 
ys = np.random.randint(0, 5000 + 1, 100) 

scat = plt.scatter(xs, ys) 

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.x, scat.y = new_xs, new_ys 

w = interactive(update_plot, 
      theta=IntSlider(min=-180, max=180, step=5,value=0, orientation='vertical'), 
      xs=fixed(xs), 
      ys=fixed(ys)) 

box_layout = Layout(display='flex', flex_flow='row', justify_content='center', align_items='center') 
display(HBox([plt.current_figure(), w], layout=box_layout)) 

bqplot的设计是一个互动的小部件。这样可以简单地将它添加到输出中,而无需将其包装到update_plot函数中。

bqplot文档:

在bqplot,情节的每一个属性是一个互动的 部件。这允许用户将任何绘图与IPython 窗口小部件集成在一起,以创建一个复杂且功能丰富的GUI,只需几行简单的Python代码即可完成。

我会继续接受詹姆斯的答案,因为它回答了原来的问题。