PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, June 27, 2022

[FIXED] How to add tooltip for Y-axis title in plotly python?

 June 27, 2022     graph, plotly-python, python     No comments   

Issue

I want to add tooltip for y-axis title in subplot because title can be a very long. Please click on this link to see Image


Solution

You can create fake line and stick it to the Y-Axis. Since scatter supports hover you can have custom tooltip. Here is my approach:

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=3, cols=1)


#Trace1
fig.append_trace(go.Scatter(
x=[3.5, 4.5],
y=[1.75, 2.75],
mode='markers'),row=1, col=1)

#Trace1 Fake Y Axis
fig.add_trace(go.Scatter(
x=[0,0,0.01,0.01,0],
y=[0,3.5,3.5,0,0],
fill="toself",
mode='lines',
fillcolor='black',
line=dict(color="black",width=0.1),
text='My Custom Text 11111111111111',
opacity=1), row=1, col=1)


#Trace2
fig.append_trace(go.Scatter(
x=[1.5, 4.5],
y=[0.75, 0.75],
mode='markers'),row=2, col=1)

#Trace2 Fake Y Axis
fig.add_trace(go.Scatter(
x=[0,0,0.01,0.01,0],
y=[0,3.5,3.5,0,0],
fill="toself",
mode='lines',
fillcolor='black',
line=dict(color="black",width=0.1),
text='My Custom Text 22222222222222',
opacity=1), row=2, col=1)


#Trace3
fig.append_trace(go.Scatter(
x=[1.5, 4.5],
y=[0.75, 3.75],
mode='markers'),row=3, col=1)

#Trace3 Fake Y Axis
fig.add_trace(go.Scatter(
x=[0,0,0.01,0.01,0],
y=[0,3.5,3.5,0,0],
fill="toself",
mode='lines',
fillcolor='black',
line=dict(color="black",width=0.1),
text='My Custom Text 33333333333333',
opacity=0), row=3, col=1) # You can make it invisible (opacity=0). Hover on 
                          # still shows text.


fig.update_xaxes(range=[0, 7], showgrid=True)
fig.update_yaxes(range=[0, 3.5])



fig.show()

Example Output: hover y-axis



Answered By - Akroma
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing