3d
3D Scatter Plots in Python
How to make 3D scatter plots in Python with Plotly.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.
In [1]:
import plotly.express as px df = px.data.iris() fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', color='species') fig.show()
A 4th dimension of the data can be represented thanks to the color of the markers. Also, values from the species column are used below to assign symbols to markers.
In [2]:
import plotly.express as px df = px.data.iris() fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', color='petal_length', symbol='species') fig.show()
Style 3d scatter plot¶
It is possible to customize the style of the figure through the parameters of px.scatter_3d for some options, or by updating the traces or the layout of the figure through fig.update.
In [3]:
import plotly.express as px df = px.data.iris() fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', color='petal_length', size='petal_length', size_max=18, symbol='species', opacity=0.7) # tight layout fig.update_layout(margin=dict(l=0, r=0, b=0, t=0)) fig.show()
3d scatter plots in Dash¶
Dash is the best way to build analytical apps in Python using Plotly figures. To run the app below, run pip install dash, click "Download" to get the code and run python app.py.
Get started with the official Dash docs and learn how to effortlessly style & publish apps like this with Dash Enterprise or Plotly Cloud.
Sign up for Dash Club → Free cheat sheets plus updates from Chris Parmer and Adam Schroeder delivered to your inbox every two months. Includes tips and tricks, community apps, and deep dives into the Dash architecture. Join now.
In [5]:
import plotly.graph_objects as go import numpy as np # Helix equation t = np.linspace(0, 10, 50) x, y, z = np.cos(t), np.sin(t), t fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode='markers')]) fig.show()
3D Scatter Plot with Colorscaling and Marker Styling¶
In [6]:
import plotly.graph_objects as go import numpy as np # Helix equation t = np.linspace(0, 20, 100) x, y, z = np.cos(t), np.sin(t), t fig = go.Figure(data=[go.Scatter3d( x=x, y=y, z=z, mode='markers', marker=dict( size=12, color=z, # set color to an array/list of desired values colorscale='Viridis', # choose a colorscale opacity=0.8 ) )]) # tight layout fig.update_layout(margin=dict(l=0, r=0, b=0, t=0)) fig.show()
What About Dash?¶
Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash at https://dash.plot.ly/installation.
Everywhere in this page that you see fig.show(), you can display the same figure in a Dash application by passing it to the figure argument of the Graph component from the built-in dash_core_components package like this:
import plotly.graph_objects as go # or plotly.express as px fig = go.Figure() # or any Plotly Express function e.g. px.bar(...) # fig.add_trace( ... ) # fig.update_layout( ... ) from dash import Dash, dcc, html app = Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter