我正在关注一个视频以了解如何使用 plotly 生成交互式绘图,但我一直收到错误:*不支持的操作数类型:'FloatSlider' 和 'float'。我很确定其他部分是正确的,原来的导师运行得很好,但在我的 jupyter 实验室中,它遇到了问题。以下是代码:import plotly as pyimport plotly.graph_objs as goimport ipywidgets as widgetsimport numpy as npfrom scipy import specialpy.offline.init_notebook_mode(connected=True)x = np.linspace(0, np.pi, 1000)# Then layout the graph object in plotly# Every object starts in a new line in the layout of plotly graph_objslayout = go.Layout( # The title of the layout title = "Simple Example of Plotly", # Y axis, everything goes into a dict type, same of X axis yaxis = dict( title = "volts" ), xaxis = dict( title = "nanoseconds" ))# Now get a function with widgets using signals and frequency# Put the trace into the functiondef update_plot(signals, frequency): # Get a data list to put all traces in data = [] for s in signals: # Put traces inside the function's loop trace1 = go.Scatter( x = x, n = freq * x, # Update y value using scipy's special's bessel functions y = special.jv(s, n), # Scatter has three modes, marker, lines, marker+lines mode = "lines", # Set a name name = "bessel {}".format(s), # Set up the interpolation, how the dots are connected with lines # line is going to be a dict line = dict( shape = "spline" ) ) data.append(trace1) # Plotting also goes in the function fig = go.Figure(data = data, layout=layout) # Finally show it py.offline.iplot(fig)# Once the function is done, we create the widget# Value in signals should be a tuplesignals = widgets.SelectMultiple(options = list(range(6)), value = (0,), description="Bessel Order")有谁知道如何解决它?似乎 special.jv(x,y) 函数不接受操作数 *? 但是即使我创建了另一个变量 n = freq * x,它仍然报告错误。非常感谢。
1 回答
宝慕林4294392
TA贡献2021条经验 获得超8个赞
当您在 Python 中报告错误时,您应该在问题中包含完整的回溯(即完整的错误消息)。所有输出中都有有用的信息,包括确切地哪一行触发了错误。
在这里,这条线似乎是n = freq * x
。你创造freq
的freq = widgets.FloatSlider(min = 1, max = 20, value = 1, description="Freq")
,所以freq
是一个FloatSlider
对象。另一个操作数x
是一个 numpy 数组。显然没有为这些操作数定义乘法运算。也就是说,Python 不知道如何将 aFloatSlider
和 numpy 数组相乘。
要获得 的实际值FloatSlider
以便您可以对其进行算术运算,请使用该value
属性。更改n = freq * x
为
n = freq.value * x
添加回答
举报
0/150
提交
取消