Show Sidebar Hide Sidebar

3D Axes in Python

How to format axes of 3d plots 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!

Range of axes

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

N = 70
trace1 = go.Mesh3d(x=(70*np.random.randn(N)),
                   y=(55*np.random.randn(N)),
                   z=(40*np.random.randn(N)),
                   opacity=0.5,
                   color='rgba(244,22,100,0.6)'
                  )

layout = go.Layout(
                    scene = dict(
                    xaxis = dict(
                        nticks=4, range = [-100,100],),
                    yaxis = dict(
                        nticks=4, range = [-50,100],),
                    zaxis = dict(
                        nticks=4, range = [-100,100],),),
                    width=700,
                    margin=dict(
                    r=20, l=10,
                    b=10, t=10)
                  )
fig = go.Figure(data=[trace1], layout=layout)
py.iplot(fig, filename='3d-axis-range')
Out[1]:

Fixed Ratio Axes

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls
import numpy as np

N = 50

fig = tls.make_subplots(
    rows=2, cols=2,
    specs=[
        [{'is_3d': True}, {'is_3d': True}],
        [{'is_3d': True}, {'is_3d': True}]
    ],
    print_grid=False
)
for i in [1,2]:
    for j in [1,2]:
        fig.append_trace(
            go.Mesh3d(
                x=(60*np.random.randn(N)),
                y=(25*np.random.randn(N)),
                z=(40*np.random.randn(N)),
                opacity=0.5,
              ), 
            row=i, col=j)

fig['layout'].update(go.Layout(
                    width=700,
                    margin=dict(
                    r=10, l=10,
                    b=10, t=10)
                  ))

# fix the ratio in the top left subplot to be a cube
fig['layout']['scene'].update(go.layout.Scene(aspectmode='cube'))

# manually force the z-axis to appear twice as big as the other two
fig['layout']['scene2'].update(go.layout.Scene(
    aspectmode='manual',
    aspectratio=go.layout.scene.Aspectratio(
        x=1, y=1, z=2
    )
))

# draw axes in proportion to the proportion of their ranges
fig['layout']['scene3'].update(go.layout.Scene(aspectmode='data'))

# automatically produce something that is well proportioned using 'data' as the default
fig['layout']['scene4'].update(go.layout.Scene(aspectmode='auto'))

py.iplot(fig, filename='3d-axis-fixed-ratio-axes')
Out[2]:

Set Axes Title

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

N = 50
trace1 = go.Mesh3d(x=(60*np.random.randn(N)),
                   y=(25*np.random.randn(N)),
                   z=(40*np.random.randn(N)),
                   opacity=0.5,
                   color='yellow'
                  )
trace2 = go.Mesh3d(x=(70*np.random.randn(N)),
                   y=(55*np.random.randn(N)),
                   z=(30*np.random.randn(N)),
                   opacity=0.5,
                   color='pink'
                  )
layout = go.Layout(
                    scene = dict(
                    xaxis = dict(
                        title='X AXIS TITLE'),
                    yaxis = dict(
                        title='Y AXIS TITLE'),
                    zaxis = dict(
                        title='Z AXIS TITLE'),),
                    width=700,
                    margin=dict(
                    r=20, b=10,
                    l=10, t=10)
                  )
fig = go.Figure(data=[trace1,trace2], layout=layout)
py.iplot(fig, filename='3d-axis-titles')
Out[3]:

Ticks Formatting

In [4]:
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np

N = 50
trace1 = go.Mesh3d(x=(60*np.random.randn(N)),
                   y=(25*np.random.randn(N)),
                   z=(40*np.random.randn(N)),
                   opacity=0.5,
                   color='rgba(100,22,200,0.5)'
                  )

layout = go.Layout(
                    scene = dict(
                    xaxis = dict(
                        ticktext= ['TICKS','MESH','PLOTLY','PYTHON'],
                        tickvals= [0,50,75,-50]),
                    yaxis = dict(
                        nticks=5, tickfont=dict(
                            color='green',
                            size=12,
                            family='Old Standard TT, serif',),
                        ticksuffix='#'),
                    zaxis = dict(
                        nticks=4, ticks='outside',
                        tick0=0, tickwidth=4),),
                    width=700,
                    margin=dict(
                    r=10, l=10,
                    b=10, t=10)
                  )
fig = go.Figure(data=[trace1], layout=layout)
py.iplot(fig, filename='3d-axis-tick-formatting')
Out[4]:

Background and Grid Color

In [5]:
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np

N = 50
trace1 = go.Mesh3d(x=(30*np.random.randn(N)),
                   y=(25*np.random.randn(N)),
                   z=(30*np.random.randn(N)),
                   opacity=0.5,)


layout = go.Layout(
                    scene = dict(
                    xaxis = dict(
                         backgroundcolor="rgb(200, 200, 230)",
                         gridcolor="rgb(255, 255, 255)",
                         showbackground=True,
                         zerolinecolor="rgb(255, 255, 255)",),
                    yaxis = dict(
                        backgroundcolor="rgb(230, 200,230)",
                        gridcolor="rgb(255, 255, 255)",
                        showbackground=True,
                        zerolinecolor="rgb(255, 255, 255)"),
                    zaxis = dict(
                        backgroundcolor="rgb(230, 230,200)",
                        gridcolor="rgb(255, 255, 255)",
                        showbackground=True,
                        zerolinecolor="rgb(255, 255, 255)",),),
                    width=700,
                    margin=dict(
                    r=10, l=10,
                    b=10, t=10)
                  )
fig = go.Figure(data=[trace1], layout=layout)
py.iplot(fig, filename='3d-axis-background-and-grid-color')
Out[5]:
Still need help?
Contact Us

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