Show Sidebar Hide Sidebar

3D Hover Options in Python

How to customize 3d hover options in python with Plotly.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

In [1]:
import plotly.plotly as py
import plotly.graph_objs as go

import pandas as pd

Customize Hover for Spikelines

By default, Plotly's 3D plots display lines called "spikelines" while hovering over a point. These lines project from the hover point to each of the three axes' normal planes and then extend from those projection data points to the planes' wall boundaries.

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

data = [go.Scatter3d(
    x=np.random.randn(50),
    y=np.random.randn(50),
    z=np.random.randn(50),
)]

layout = go.Layout(
    scene=go.layout.Scene(
        xaxis=go.layout.scene.XAxis(
            spikecolor='#1fe5bd',
            spikesides=False,
            spikethickness=6,
        ),
        yaxis=go.layout.scene.YAxis(
            spikecolor='#1fe5bd',
            spikesides=False,
            spikethickness=6,
        ),
        zaxis=go.layout.scene.ZAxis(
            spikecolor='#1fe5bd',
            spikethickness=6,
        ),
    )
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='hover-spikelines')
Out[2]:

Customize Hover for Surface Contours

In addition to spikelines, Plotly 3D Surface plots also display surface contours on hover by default. These are customized by styling the contours attribute in the surface trace.

In [3]:
import plotly.plotly as py
import plotly.graph_objs as go

x = [10,20,30,40]
y = [0,1,2,3]
z = [[2,2,2,3],
     [1,1,1,1],
     [1,1,0,0],
     [0,0,0,0]]

data = [go.Surface(
    opacity=0.9,
    x=x,
    y=y,
    z=z,
    contours=go.surface.Contours(
        x=go.surface.contours.X(
            highlight=True,
            highlightcolor="#41a7b3",
        ),
        y=go.surface.contours.Y(highlight=False),
        z=go.surface.contours.Z(highlight=False),
    )
)]

layout = go.Layout(
    scene=go.layout.Scene(
        xaxis = go.layout.scene.XAxis(showspikes=False),
        yaxis = go.layout.scene.YAxis(showspikes=False),
        zaxis = go.layout.scene.ZAxis(showspikes=False),
    )
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='hover-surface-contours')
Out[3]:

Reference

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.