enable display of different ticktext for hover and axis when using `hovermode='x unified'`
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 18.8k
- Forks
- 2.8k
- Merge moyen
- 16 h 26 min
- PR mergées (30 j)
- 21
Description
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:
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()
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:
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par examiner les exemples Python de l’issue ainsi que le comportement existant de axis ticktext, tickvals, hovertext et unified-hover dans plotly.py. Aucun fichier source ni test n’est indiqué ; identifiez donc les points d’entrée pertinents pour les axes et le hover avant de décider si la modification doit être effectuée dans plotly.py ou dans sa couche de plotting sous-jacente. Le travail est terminé lorsque le texte séparé de l’axe du hover fonctionne avec hovermode='x unified' tout en laissant les libellés des graduations de l’axe inchangés, avec une couverture du cas présenté.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- data-visualization
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100