我正在關注一個視頻以了解如何使用 plotly 生成交互式繪圖,但我一直收到錯誤:*不支持的操作數(shù)類型:'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) 函數(shù)不接受操作數(shù) *? 但是即使我創(chuàng)建了另一個變量 n = freq * x,它仍然報告錯誤。非常感謝。
1 回答

寶慕林4294392
TA貢獻2021條經(jīng)驗 獲得超8個贊
當您在 Python 中報告錯誤時,您應該在問題中包含完整的回溯(即完整的錯誤消息)。所有輸出中都有有用的信息,包括確切地哪一行觸發(fā)了錯誤。
在這里,這條線似乎是n = freq * x
。你創(chuàng)造freq
的freq = widgets.FloatSlider(min = 1, max = 20, value = 1, description="Freq")
,所以freq
是一個FloatSlider
對象。另一個操作數(shù)x
是一個 numpy 數(shù)組。顯然沒有為這些操作數(shù)定義乘法運算。也就是說,Python 不知道如何將 aFloatSlider
和 numpy 數(shù)組相乘。
要獲得 的實際值FloatSlider
以便您可以對其進行算術運算,請使用該value
屬性。更改n = freq * x
為
n = freq.value * x
添加回答
舉報
0/150
提交
取消