RiffRaffCat
RiffRaffCat

Reputation: 1041

Jupyter Lab Interactive plot: unsupported operand type(s) for *: 'FloatSlider' and 'float'

I am following a video to learn how to use plotly to generate interactive plots, but I kept getting the error: unsupported operand type(s) for *: 'FloatSlider' and 'float'.

I am pretty sure that the other parts are correct, the original tutor ran it well, yet on my jupyter lab, it runs into problem.

Here are the codes:

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)

Does anyone know how to solve it? It seems that the special.jv(x,y) function doesn't accept operand *? But even I created another variable n = freq * x, it still reports error. Thanks a lot.

Upvotes: 1

Views: 1261

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114921

When you report an error in Python, you should include the complete traceback (i.e. the complete error message) in the question. There is useful information in all that output, including exactly which line triggered the error.

Here, the line appears to be n = freq * x. You created freq as freq = widgets.FloatSlider(min = 1, max = 20, value = 1, description="Freq"), so freq is a FloatSlider object. The other operand, x, is a numpy array. Apparently there is no multiplication operation defined for these operands. That is, Python doesn't know how to multiply a FloatSlider and numpy array.

To get the actual value of the FloatSlider so you can do arithmetic with it, use the value attribute. Change n = freq * x to

n = freq.value * x

Upvotes: 2

Related Questions