Soerendip
Soerendip

Reputation: 9146

How to plot multiple 3d lines with plotly express?

I would like to have more than just one 3d line in my graph to be able to compare the data unfortunately this .add_ methods are not present for all kinds of plots.

fig = px.line_3d(sample, x='Time', y='y' ,z='intensity')
# fig.add_line_3d(sample2, x='Time', y='y' ,z='intensity')
fig

Can I extract the traces from a figure and then plot them all together somehow?

enter image description here

Upvotes: 4

Views: 5860

Answers (1)

nicolaskruchten
nicolaskruchten

Reputation: 27410

With Plotly Express, you can create multiple lines with a single call, so long as your data is in a "tidy" format. You can use the color attribute to split your lines and color them differently, like this:

import pandas as pd

df = pd.DataFrame(dict(
    X=[0,1,2,3, 1,2,3,4], 
    Y=[0,2,3,1, 1,3,4,2], 
    Z=[0,3,1,2, 1,4,2,3],
    color=["a", "a", "a", "a", "b", "b", "b", "b"]
))

import plotly.express as px

fig = px.line_3d(df, x='X', y='Y', z='Z', color="color")
fig.show()

enter image description here

Upvotes: 5

Related Questions