<i id='f3oWc'><tr id='f3oWc'><dt id='f3oWc'><q id='f3oWc'><span id='f3oWc'><b id='f3oWc'><form id='f3oWc'><ins id='f3oWc'></ins><ul id='f3oWc'></ul><sub id='f3oWc'></sub></form><legend id='f3oWc'></legend><bdo id='f3oWc'><pre id='f3oWc'><center id='f3oWc'></center></pre></bdo></b><th id='f3oWc'></th></span></q></dt></tr></i><div id='f3oWc'><tfoot id='f3oWc'></tfoot><dl id='f3oWc'><fieldset id='f3oWc'></fieldset></dl></div>
          <bdo id='f3oWc'></bdo><ul id='f3oWc'></ul>
        <legend id='f3oWc'><style id='f3oWc'><dir id='f3oWc'><q id='f3oWc'></q></dir></style></legend>
        <tfoot id='f3oWc'></tfoot>

        <small id='f3oWc'></small><noframes id='f3oWc'>

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

        时间:2023-09-29
        <i id='gXQpY'><tr id='gXQpY'><dt id='gXQpY'><q id='gXQpY'><span id='gXQpY'><b id='gXQpY'><form id='gXQpY'><ins id='gXQpY'></ins><ul id='gXQpY'></ul><sub id='gXQpY'></sub></form><legend id='gXQpY'></legend><bdo id='gXQpY'><pre id='gXQpY'><center id='gXQpY'></center></pre></bdo></b><th id='gXQpY'></th></span></q></dt></tr></i><div id='gXQpY'><tfoot id='gXQpY'></tfoot><dl id='gXQpY'><fieldset id='gXQpY'></fieldset></dl></div>

        • <small id='gXQpY'></small><noframes id='gXQpY'>

        • <legend id='gXQpY'><style id='gXQpY'><dir id='gXQpY'><q id='gXQpY'></q></dir></style></legend>

              <tbody id='gXQpY'></tbody>

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

                  上一篇:有很多字典的列表 VS 有很少列表的字典? 下一篇:Python 中的 Pillow 不允许我打开图像(“超出限制")

                  相关文章

                • <i id='sKjOO'><tr id='sKjOO'><dt id='sKjOO'><q id='sKjOO'><span id='sKjOO'><b id='sKjOO'><form id='sKjOO'><ins id='sKjOO'></ins><ul id='sKjOO'></ul><sub id='sKjOO'></sub></form><legend id='sKjOO'></legend><bdo id='sKjOO'><pre id='sKjOO'><center id='sKjOO'></center></pre></bdo></b><th id='sKjOO'></th></span></q></dt></tr></i><div id='sKjOO'><tfoot id='sKjOO'></tfoot><dl id='sKjOO'><fieldset id='sKjOO'></fieldset></dl></div>
                    <bdo id='sKjOO'></bdo><ul id='sKjOO'></ul>
                    <legend id='sKjOO'><style id='sKjOO'><dir id='sKjOO'><q id='sKjOO'></q></dir></style></legend>

                    <tfoot id='sKjOO'></tfoot>

                    <small id='sKjOO'></small><noframes id='sKjOO'>