Bru*_*nho 5 python matplotlib jupyter ipywidgets
我正在寻找一种方法来更改 matplotlib 图右侧的垂直 IntSlider 的位置。这是代码:
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))
Run Code Online (Sandbox Code Playgroud)
这就是我所拥有的:
这就是我要的:
可能有一种非常简单的方法可以做到这一点,但我自己无法弄清楚。
我尝试将fig和interactive小部件放入然后用VBox包装,但它不起作用。VBoxIPython.display
在示例中找不到直接解决此问题的方法。
编辑1:
ipywidgets 提供了一个Output()捕获输出区域并在小部件上下文中使用它的类。
我会尝试弄清楚如何使用它。
这是对象: https://github.com/jupyter-widgets/ipywidgets/blob/master/ipywidgets/widgets/widget_output.py
您可以通过创建交互式小部件然后将其加载children到HBox. 交互式的子部件遵循此约定;(widget_0, widget_1 ..., output) 其中元组的最后一个成员是控制小部件的输出。您可以在声明之前或之后定义 HBox 的布局。阅读有关此处可用布局的更多信息。
以下解决方案有一些注意事项;图表最初可能不会显示,您可能必须在显示之前调整控件,其次,当使用%matplotlib notebook魔法时,控件可能会在更新时导致大量闪烁。除此之外,我认为这应该像你想要的那样工作;
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))
Run Code Online (Sandbox Code Playgroud)
更新:
这是 Jason Grout 在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]))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6863 次 |
| 最近记录: |