How to create log scaled axis in plotly Scatter3d?

Viewed 144

There are options like log_x=True in the 2d plots but they do not seem to exist in 3d. Yet I see mention of options for log appearing in dtick options in the documents.

Either search is not indexing this very well or something is not documented in the online docs.

1 Answers

As mentioned in the comments, there is a feature in plotly.express that allows you to log axis settings. For the sample data, I diverted the function from matplotlib's 3D scatterplot sample and created the data to match the log with appropriate parameters.

import plotly.express as px
import pandas as pd
import numpy as np
import random

n = 100

def randrange(n, vmin, vmax):
    """
    Helper function to make an array of random numbers having shape (n, )
    with each number distributed Uniform(vmin, vmax).
    """
    return (vmax - vmin)*np.random.rand(n) + vmin

df = pd.DataFrame({'x':randrange(n, 2000, 100000),
                   'y':randrange(n, 4000, 100000),
                   'z':randrange(n, 3000, 100000),
                   'category':random.choices(['AAA','BBB','CCC'], k=100)})

fig = px.scatter_3d(df, 
                    x='x',
                    y='y',
                    z='z',
                    color='category',
                    log_x=True,
                    log_y=True,
                    log_z=True)

fig.update_layout(autosize=False, height=800, width=800)
fig.show()

enter image description here

Related