How can I change the distance between the axis title and the axis values in a 3D scatterplot in Plotly (Python)?

Question:

i have the following example code of a 3d Scatterplot in plotly and i want to place the x,y and z title with more distance to the axis.

import plotly.graph_objects as go

# set up data
x = [1,2,3,4,5]
y = [2,4,6,8,10]
z = [2,4,6,8,10]

# create the figure
fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode='markers')])

# set up the axes
fig.update_layout(
    title='3D Scatterplot',
    scene = dict(
        xaxis_title='X Axis',
        yaxis_title='Y Axis',
        zaxis_title='Z Axis'
    )
)

# show the figure
fig.show()
Asked By: Alex

||

Answers:

While plotly does have a title_standoff property (as described here), it does not work for 3d scatter plots (I believe this was first noticed here).

One workaround would be to use 3d annotations – you can place a text annotation halfway between the lower and upper bounds of the x-axis, the max value of the y-axis, and at z = 0 (you can generalize this based on your use case). Then we can use ax=150 which will shift the entire annotation right by 150px (on the page, not in the coordinate system of the figure). We also need to prevent any default x-axis title from showing up by setting it to be an empty string.

This is a bit clumsy because the annotation won’t be placed consistently next to the x-axis, so this solution is probably ideal if you need to take a screenshot of a 3d figure, but not ideal if you rotate the figure too much.

import plotly.graph_objects as go

# set up data
x = [1,2,3,4,5]
y = [2,4,6,8,10]
z = [2,4,6,8,10]

# create the figure
fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode='markers')])

# set up the axes
fig.update_layout(
    title='3D Scatterplot',
    scene = dict(
        xaxis_title='',
        yaxis_title='Y Axis',
        zaxis_title='Z Axis',
        annotations=[dict(
            x=3,
            y=10,
            z=0,
            xshift=150,
            text="X Axis",
            showarrow=False,
        )]
    )
)

# show the figure
fig.show()

enter image description here

Answered By: Derek O
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.