2

The dataframe df:

        Id      timestamp                C      Date         sig    events1 Start   Peak    Timediff2   datadiff2   B
51253   51494   2020-01-27 06:22:08.330 19.5    2020-01-27   -1.0   0.0     NaN     1.0     NaN          NaN        NaN
51254   51495   2020-01-27 06:22:08.430 19.0    2020-01-27   1.0    1.0     0.0     0.0     NaN          NaN        NaN
51255   51496   2020-01-27 07:19:06.297 19.5    2020-01-27   1.0    0.0     1.0     0.0     3417.967     0.0        0.000000
51256   51497   2020-01-27 07:19:06.397 20.0    2020-01-27   1.0    0.0     0.0     0.0     3417.967     1.0        0.000293
51259   51500   2020-01-27 07:32:19.587 20.5    2020-01-27   1.0    0.0     0.0     0.0     793.290      1.0        0.001261
51260   51501   2020-01-27 07:32:19.687 21.0    2020-01-27

I plotted an interactive graph using:

import pandas as pd
import plotly.express as px

fig = px.line(x=df['Timestamp'], y=df['C'], hover_data=["B"])
fig.add_scatter(x=df['Timestamp'], y=df['C'], mode='markers',  marker_color=df['A'], marker_size=5)

fig.update_layout(plot_bgcolor='#bababa', showlegend=False, width=2400, height=800)

fig.show()

Then to add additional hover data for column C, I added hover_data=["C"]:

fig = px.line(x=df['Timestamp'], y=df['B'], hover_data=["C"])

It returned

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-be5860c5172a> in <module>()
     21 import plotly.express as px
     22 
---> 23 fig = px.line(x=df['Timestamp'], y=df['C'], hover_data=["B"])
     24 fig.add_scatter(x=df['Timestamp'], y=df['C'], mode='markers', marker_color=df['A'], marker_size=5)
     25 

3 frames
/usr/local/lib/python3.6/dist-packages/plotly/express/_core.py in build_dataframe(args, attrables, array_attrables)
    931                         "DataFrame or an array is provided in the `data_frame` "
    932                         "argument. No DataFrame was provided, but argument "
--> 933                         "'%s' is of type str or int." % field
    934                     )
    935                 # Check validity of column name

ValueError: String or int arguments are only possible when a DataFrame or an array is provided in the `data_frame` argument. No DataFrame was provided, but argument 'hover_data_0' is of type str or int.

Update:

Tried

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-be5860c5172a> in <module>()
     21 import plotly.express as px
     22 
---> 23 fig = px.line(x=df['Timestamp'], y=df['C'], hover_data=df["B"])
     24 fig.add_scatter(x=df['Timestamp'], y=df['C'], mode='markers', marker_color=df['A'], marker_size=5)
     25 

/usr/local/lib/python3.6/dist-packages/plotly/express/_core.py in build_dataframe(args, attrables, array_attrables)
    986                 else:  # numpy array, list...
    987                     col_name = _check_name_not_reserved(field, reserved_names)
--> 988                 if length and len(argument) != length:
    989                     raise ValueError(
    990                         "All arguments should have the same length. "

TypeError: object of type 'float' has no len()
0

2 Answers 2

4

This is how you do it, actually is a little simpler and you can add as much hover data as you want:

    fig = px.line(df,
        x='Timestamp',
        y='C',
        hover_data=[
            'B', 'Z', 
            'A', 'Other'
        ],
   )
Sign up to request clarification or add additional context in comments.

Comments

3

You tried:

fig = px.line(x=df['Timestamp'], y=df['C'], hover_data=["B"])

If you read the error message, you'll see that ["B"] should be df['B']

So try:

fig = px.line(x=df['Timestamp'], y=df['C'], hover_data=df["B"])

Edit after comments:

My original answer still stands, but it's no wonder you're still experiencing problems. If you had shared a sample of your dataset, I would most likely have seen the real issue at once.

Anyway, take a look at some of the plotly express docs. There you'll see that px.line does not function the way you seem to expect it to. You're trying to apply dataframe columns such as df['Timestamp'], df['B'] and df['C'] which leads me to believe that your source data has a so-called wide format. Plotly express functions mostly work with data of a so-called long format. Take a look at https://plotly.com/python/px-arguments/ for more info on this.

If I'm right, then all you'll have to do to make this work for you particular case is transforming your dataframe from a wide to a long format. Update your question with a sample dataframe, and we can take a look at that too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.