Python/Plotly:如何使用要显示的信息自定义悬停模板?

2023-09-29Python开发问题
88

本文介绍了Python/Plotly:如何使用要显示的信息自定义悬停模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这是我的数据集:

在按锁定我的数据框并按分组后,我继续计算百分比增加/减少作为新列;它最终看起来像这样:

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_traceshovertemplate 对其进行修改,将其引用为 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_datacustomdata 之间的不一致令人困惑.

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:如何使用要显示的信息自定义悬停模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11