Last active
October 18, 2022 16:53
-
-
Save Erlemar/5d075fa4cfdc84ce48a0e5445bea0af3 to your computer and use it in GitHub Desktop.
Plot 3d data with slider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import plotly.graph_objects as go | |
import numpy as np | |
import pandas as pd | |
df = pd.read_csv('/Users/andreylukyanenko/Downloads/df_numpy_3d_B4_pred.csv') | |
# Create figure | |
fig = go.Figure() | |
# Add traces, one for each slider step | |
for step in range(1, 66): | |
df_small = df.loc[df['k'] == step] | |
fig.add_trace( | |
go.Scatter3d( | |
visible=False, | |
line=dict(color="#00CED1", width=6), | |
name="𝜈 = " + str(step), | |
x=df_small.i.values, | |
y=df_small.j.values, | |
z=df_small['k'], | |
marker=dict( | |
size=3, | |
color=df_small.pred_P.values, # set color to an array/list of desired values | |
colorscale='Viridis', # choose a colorscale | |
opacity=0.8, | |
colorbar=dict(thickness=20) | |
))) | |
# Make 10th trace visible | |
fig.data[0].visible = True | |
# Create and add slider | |
steps = [] | |
for i in range(len(fig.data)): | |
step = dict( | |
method="update", | |
args=[{"visible": [False] * len(fig.data)}, | |
{"title": "Slider switched to step: " + str(i)}], # layout attribute | |
) | |
step["args"][0]["visible"][i] = True # Toggle i'th trace to "visible" | |
steps.append(step) | |
sliders = [dict( | |
active=10, | |
currentvalue={"prefix": "Frequency: "}, | |
pad={"t": 50}, | |
steps=steps | |
)] | |
fig.update_layout( | |
sliders=sliders | |
) | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment