Jupyter Lab 交互图:* 不支持的操作数类型:'FloatSlider' 和 'float'
Jupyter Lab Interactive plot: unsupported operand type(s) for *: 'FloatSlider' and 'float'
我正在观看视频以了解如何使用 plotly 生成交互式绘图,但我一直收到错误消息:不支持的 ope运行d type(s) for *: 'FloatSlider' and 'float'。
我很确定其他部分是正确的,原导师运行很好,但是在我的jupyter lab上,它遇到了问题。
代码如下:
import plotly as py
import plotly.graph_objs as go
import ipywidgets as widgets
import numpy as np
from scipy import special
py.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_objs
layout = 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 function
def 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 tuple
signals = widgets.SelectMultiple(options = list(range(6)), value = (0,),
description="Bessel Order")
# Make a freq
freq = widgets.FloatSlider(min = 1, max = 20, value = 1, description="Freq")
# Finally make the interaction
widgets.interactive(update_plot, signals = signals, frequency = freq)
有谁知道怎么解决吗? special.jv(x,y) 函数似乎不接受 ope运行d *?但是即使我创建了另一个变量n = freq * x,它仍然报错。非常感谢。
当您在 Python 中报告错误时,您应该在问题中包含 complete traceback(即完整的错误消息)。所有输出中都有有用的信息,包括确切的哪一行触发了错误。
此处,该行似乎是 n = freq * x
。您将 freq
创建为 freq = widgets.FloatSlider(min = 1, max = 20, value = 1, description="Freq")
,因此 freq
是一个 FloatSlider
对象。另一个操作数 x
是一个 numpy 数组。显然没有为这些操作数定义乘法运算。也就是说,Python 不知道如何将 FloatSlider
和 numpy 数组相乘。
要获取 FloatSlider
的实际值以便对其进行算术运算,请使用 value
属性。将 n = freq * x
更改为
n = freq.value * x
我正在观看视频以了解如何使用 plotly 生成交互式绘图,但我一直收到错误消息:不支持的 ope运行d type(s) for *: 'FloatSlider' and 'float'。
我很确定其他部分是正确的,原导师运行很好,但是在我的jupyter lab上,它遇到了问题。
代码如下:
import plotly as py
import plotly.graph_objs as go
import ipywidgets as widgets
import numpy as np
from scipy import special
py.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_objs
layout = 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 function
def 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 tuple
signals = widgets.SelectMultiple(options = list(range(6)), value = (0,),
description="Bessel Order")
# Make a freq
freq = widgets.FloatSlider(min = 1, max = 20, value = 1, description="Freq")
# Finally make the interaction
widgets.interactive(update_plot, signals = signals, frequency = freq)
有谁知道怎么解决吗? special.jv(x,y) 函数似乎不接受 ope运行d *?但是即使我创建了另一个变量n = freq * x,它仍然报错。非常感谢。
当您在 Python 中报告错误时,您应该在问题中包含 complete traceback(即完整的错误消息)。所有输出中都有有用的信息,包括确切的哪一行触发了错误。
此处,该行似乎是 n = freq * x
。您将 freq
创建为 freq = widgets.FloatSlider(min = 1, max = 20, value = 1, description="Freq")
,因此 freq
是一个 FloatSlider
对象。另一个操作数 x
是一个 numpy 数组。显然没有为这些操作数定义乘法运算。也就是说,Python 不知道如何将 FloatSlider
和 numpy 数组相乘。
要获取 FloatSlider
的实际值以便对其进行算术运算,请使用 value
属性。将 n = freq * x
更改为
n = freq.value * x