这是我的数据集:
在按年锁定我的数据框并按月分组后,我继续计算百分比增加/减少作为新列;它最终看起来像这样:
After locking my dataframe by year and grouping by month, I proceed with calculating percentage increase/decrease as a new column; it ends up looking like this:
现在对于我的 Plotly 情节,我使用它来显示轨迹并添加一些悬停信息:
Now for my Plotly plot I use this to display traces and add some hover info:
fig.add_trace(go.Scatter(x=group_dfff.Months, y=group_dfff.Amount, name=i,
hovertemplate='Price: $%{y:.2f}'+'<br>Week: %{x}'))
现在你可以看到有一个参数 hovertemplate
我可以在其中传递我的 x 和 y...但是,我不知道如何包含我的 PERC_CHANGE 也包含其中的价值.
Now as you can see there is an argument hovertemplate
where I can pass my x and y... However, I can't figure out how to include my PERC_CHANGE values in it too.
问题:如何在 hovertemplate
中包含其他想要的列的值?具体来说,我如何包含 PERC_CHANGE 值,因为我在下面显示了所需的输出:
Question: How to include other wanted columns' values inside the hovertemplate
? Specifically, How do I include PERC_CHANGE values as I shown desired output below:
我解决了我的具体问题,请查看下面的图片(添加第 3 个元素,请参阅评论),但是问题仍然存在,因为我看不到如何为第 4 个、第 5 个等元素执行此操作.
I solved my specific problem, check pic below (adding 3rd element it is, please see comments), however question remains the same as I do not see how to do this for 4th, 5th and so on elements.
非常感谢您的帮助!
对于 Plotly Express,您需要在创建图形时使用 custom_data
参数.例如:
For Plotly Express, you need to use the custom_data
argument when you create the figure. For example:
fig = px.scatter(
data_frame=df,
x='ColX',
y='ColY',
custom_data=['Col1', 'Col2', 'Col3']
)
然后使用 update_traces
和 hovertemplate
对其进行修改,将其引用为 customdata
.例如:
and then modify it using update_traces
and hovertemplate
, referencing it as customdata
. For example:
fig.update_traces(
hovertemplate="<br>".join([
"ColX: %{x}",
"ColY: %{y}",
"Col1: %{customdata[0]}",
"Col2: %{customdata[1]}",
"Col3: %{customdata[2]}",
])
)
这需要大量的试验和错误才能弄清楚,因为它没有很好的记录,并且 custom_data
和 customdata
之间的不一致令人困惑.
This took a lot of trial and error to figure out, as it isn't well-documented, and the inconsistency between the custom_data
and customdata
is confusing.
这篇关于Python/Plotly:如何使用要显示的信息自定义悬停模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!