在 plotly.express 中添加悬停文字

问题描述 投票:0回答:1

数据框架 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

我使用交互式图表绘制了一个图表。

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()

然后为列添加额外的悬停数据 C我补充道 hover_data=["C"]:

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

已返回

---------------------------------------------------------------------------
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.

更新。

试过了

---------------------------------------------------------------------------
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()
python pandas matplotlib plotly
1个回答
0
投票

你试过了。

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

如果你读了错误信息,你会发现: ["B"] 应是 df['B']

那就试试吧。

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

在评论后编辑。

我最初的回答仍然有效,但难怪你还会遇到问题。如果你分享了你的数据集样本,我很可能一下子就能看到真正的问题。

总之,看看一些 plotly express docs. 你会看到 px.line 并没有按照你所期望的方式运行。你试图应用数据框架列,如 df['Timestamp'], df['B']df['C'] 这让我相信,你的源数据有所谓的 宽广 格式。Plotly表达函数主要处理所谓的 格式。请看一下 https:/plotly.comythonpx-arguments。 获取更多信息。

如果我是对的,那么你要做的就是将你的数据框从宽格式转换为长格式,以使其适用于你的特定情况。更新你的问题,提供一个样本数据框,我们也可以看看。

© www.soinside.com 2019 - 2024. All rights reserved.