[英]Add hover text in plotly.express
The dataframe df
: 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"]
:然后为列C
添加额外的 hover 数据,我添加了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()
This is how you do it, actually is a little simpler and you can add as much hover data as you want:这就是你的做法,实际上更简单一些,你可以根据需要添加尽可能多的 hover 数据:
fig = px.line(df,
x='Timestamp',
y='C',
hover_data=[
'B', 'Z',
'A', 'Other'
],
)
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']
如果您阅读错误消息,您会看到["B"]
应该是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 .无论如何,看看一些plotly express 文档。 There you'll see that px.line
does not function the way you seem to expect it to.在那里你会看到px.line
不像你期望的那样 function 。 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.您正在尝试应用 dataframe 列,例如df['Timestamp']
、 df['B']
和df['C']
这让我相信您的源数据具有所谓的宽格式。 Plotly express functions mostly work with data of a so-called long format. Plotly express 函数主要处理所谓的长格式数据。 Take a look at https://plotly.com/python/px-arguments/ for more info on this.查看https://plotly.com/python/px-arguments/了解更多信息。
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.如果我是对的,那么您需要做的就是将您的 dataframe 从宽格式转换为长格式。 Update your question with a sample dataframe, and we can take a look at that too.使用示例 dataframe 更新您的问题,我们也可以看看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.