作为一个最小的示例,我只是使用pyplot.quiver
绘制矢量,并希望将旋转度作为浮动滑块传递:
import numpy as np
import matplotlib.pyplot as plt
def rotvec(vec, deg=0):
rotmat= np.asarray([[np.cos(deg), -np.sin(deg)],[np.sin(deg), np.cos(deg)]])
return np.matmul(rotmat,vec)
def drawvec(x,y):
plt.plot()
plt.quiver(0,0,x,y,angles='xy', scale_units='xy', scale=1)
plt.xlim(xmin=-2, xmax=2)
plt.ylim(ymin=-2, ymax=2)
plt.show()
现在,如果我们这样做:drawvec(*rotvec([1,0], np.pi/4))
,我们将得到预期的数字。
有没有办法像下面这样将参数传递给内部函数(下面的方法不起作用):
from ipywidgets import interactive, FloatSlider
from IPython.display import display
from functools import partial
deg = FloatSlider(min=0, max=6.4, value=0)
interactive_plot = interactive(drawvec(*rotvec([1,0])), deg=deg)
我知道我可以编写一个新函数:
def rotndraw(vec, deg=0):
rotated = rotvec(vec,deg)
drawvec(*rotated)
并使用固定的矢量和浮动滑块调用它:
但是我想避免使用额外的函数来暴露参数。这可能吗?也许和functools.partial
在一起?我一直无法使其工作。
原因:我想避免编写包装程序来公开此参数,因为在我的实际问题中,我正在进行一系列的转换,其中包含许多函数调用,但我唯一的一个或多个参数希望在内部函数调用之一中进行交互操作。每次调查时写不同的包装以暴露不同的参数似乎很麻烦。