plotly / plotly/plotly.py

enable display of different ticktext for hover and axis when using `hovermode='x unified'`

未关闭
#4,278 2 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

feature P3
主要语言
Python
星标
18.8k
派生
2.8k
平均合并
16 小时 26 分钟
30 天内合并 PR
21

描述

Hi,

Below you can read a feature request (given that this does not exist already).

The scenario

I am looking for a nice solution for displaying a hover "title" different from the ticks when using hovermode='x unified'.
Let's say that you want to display multiple time series and you have your range defined in 2 different ways. You have the timestamps, and the elapsed time. Now, you don't want to display both on the x axis to keep that clutter free, but you want to display them when hovering over the plot.

The problem

Take this code for example:

import plotly.graph_objects as go

x = ['2023-07-01', '2023-07-02', '2023-07-03', '2023-07-04']
y1 = [10, 15, 7, 12]
y2 = [5, 8, 10, 6]

ticktext = ['Day 1', 'Day 2', 'Day 3', 'Day 4']
tickvals = x  

hovertext = ['Hover 1', 'Hover 2', 'Hover 3', 'Hover 4']

fig = go.Figure()

fig.add_trace(go.Scatter(x=x, y=y1, hovertext=hovertext, name='Series 1'))
fig.add_trace(go.Scatter(x=x, y=y2, name='Series 2'))

fig.update_layout(xaxis=dict(
    tickmode='array',
    ticktext=ticktext,
    tickvals=tickvals
), 
    hovermode='x unified'
)

fig.show()

This will produce the following plot, and the problem is that I want to place "Hover 2" in place of "Day 2", while keeping "Day 2" on the x axis:

image

One solution

One solution I came up with was introducing a 2nd y axis, and placing the hover information there, hiding the axis and hiding the line. This works fine, however results in a hover display that looks "funny", essentially leaving some padding before the displayed text.

import plotly.graph_objects as go

x = ['2023-07-01', '2023-07-02', '2023-07-03', '2023-07-04']
y1 = [10, 15, 7, 12]
y2 = [5, 8, 10, 6]

ticktext = ['Day 1', 'Day 2', 'Day 3', 'Day 4']
tickvals = x  

hovertext = ['Hover 1', 'Hover 2', 'Hover 3', 'Hover 4']

fig = go.Figure()

fig.add_trace(go.Scatter(x=x,
                          y=y1,
                          text=hovertext,
                          yaxis='y2',
                          opacity=0,
                          showlegend=False,
                          hovertemplate = "%{text}<extra></extra>") )
fig.add_trace(go.Scatter(x=x, y=y1, hovertext=hovertext, name='Series 1', hovertemplate = "%{y:.1f}<extra></extra>"))
fig.add_trace(go.Scatter(x=x, y=y2, name='Series 2', hovertemplate = "%{y:.1f}<extra></extra>"))

fig.update_layout(xaxis=dict(
    tickmode='array',
    ticktext=ticktext,
    tickvals=tickvals
    ), 
    hovermode='x unified',
    yaxis2 = dict(
        overlaying='y',
        showticklabels=False
    ),
)

fig.show()

image

Proposed solution

It would be so amazing if one could just have a hoverticktext arg in the layout, that would allow for another piece of text to be set.

Imaginary example code with expected result:

import plotly.graph_objects as go

x = ['2023-07-01', '2023-07-02', '2023-07-03', '2023-07-04']
y1 = [10, 15, 7, 12]
y2 = [5, 8, 10, 6]

ticktext = ['Day 1', 'Day 2', 'Day 3', 'Day 4']
tickvals = x  

hovertext = ['Hover 1', 'Hover 2', 'Hover 3', 'Hover 4']

fig = go.Figure()

fig.add_trace(go.Scatter(x=x, y=y1, name='Series 1'))
fig.add_trace(go.Scatter(x=x, y=y2, name='Series 2'))

fig.update_layout(xaxis=dict(
    tickmode='array',
    ticktext=ticktext,
    tickvals=tickvals,
    hoverticktext = hovertext # added this imaginary line
), 
    hovermode='x unified'
)

fig.show()

And this would result in the following (edited picture) plot:

image

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先查看 issue 中的 Python 示例,以及 plotly.py 中现有的 axis ticktext、tickvals、hovertext 和 unified-hover 行为。未指定源文件或测试,因此请先确定相关的 axis 和 hover 入口点,再决定应在 plotly.py 还是其底层 plotting layer 中进行更改。完成的标准是:在 hovermode='x unified' 下,独立的 hover-axis text 能够正常工作,同时 axis tick label 保持不变,并且为所展示的案例提供覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
data-visualization
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。