Ilya V. Schurov
Ilya V. Schurov

Reputation: 8047

Rename axes in plotly 3d hover text

I'm drawing a picture in 3D with plot.ly and I want my axes to be referenced as (t, x, y) instead of (x, y, z). It is possible to give them different titles (under Scene object in case of 3D), but when I hover on the plot, I get a tooltip that ignores new titles and still uses (x, y, z). Is it possible to rename them too?

My code is

from plotly.graph_objs import Scatter3d, Layout, Scene
from numpy import sin, cos, linspace, pi
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()

t = linspace(0, 4*pi)

trace = Scatter3d(
    x = t,
    y = cos(t),
    z = sin(t),
    mode = 'lines'
)
layout = Layout(
                width = 500, 
                height = 500, 
                scene = Scene(
                    xaxis = {'title': 't'},
                    yaxis = {'title': 'x'},
                    zaxis = {'title': 'y'}
                )
)
iplot(dict(data=[trace], layout=layout))

When I hover on graph, I have:

tooltip

And I want to change x, y, z here to t, x, y or anything else.

Upvotes: 1

Views: 2264

Answers (1)

tfg250
tfg250

Reputation: 436

This is more workaround than solution, but you could define a list of strings, one item for each point on your line, where each item in the list is a string of whatever text you wanted to show when hovering (including the string "<br>" for a line return), then set text=your_list and hoverinfo="text".

Like this:

from plotly.graph_objs import Scatter3d, Layout, Scene
from numpy import sin, cos, linspace, pi
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()
t = linspace(0, 4*pi)
your_list=[]
for iter_t in t:
    iter_string = 't:'+'%1.3f'%iter_t+'<br>' + 'x:'+'%1.3f'%cos(iter_t) + '<br>'+'y:'+'%1.3f'%sin(iter_t)
    your_list.append(iter_string)

trace = Scatter3d(
    x = t,
    y = cos(t),
    z = sin(t),
    mode = 'lines',
    text=your_list,
    hoverinfo='text')

layout = Layout(width = 500, height = 500, scene = Scene(
    xaxis = {'title': 't'},
    yaxis = {'title': 'x'},
    zaxis = {'title': 'y'}))

iplot(dict(data=[trace], layout=layout))

Resulting plot looks like this

enter image description here

You should probably be saving the computed values in the for loop in order to prevent having to compute them more than once, but you get the idea.

Upvotes: 2

Related Questions