我正在尋找一種方法來改變垂直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()對象上花費很多。我認爲這將解決我們的問題。 –