in the following code, change the name of color bar to "Coherance": @title Manifold…
- User
in the following code, change the name of color bar to "Coherance":
@title Manifold Vectorized 3D Colored by Magnitude Difference
Suppose original shape of state_var is (num_samples, time_steps, features)
num_samples, time_steps, features = state_var.shape
Reshape state_var for PCA as before
w_state_var = state_var.copy() w_state_var = w_state_var.reshape(-1, features) # shape (num_samples*time_steps, features)
PCA transform
pca = PCA(n_components=3) PC = pca.fit_transform(w_state_var)
Create an array that maps each flattened timepoint back to its trial index
trial_indices = np.repeat(np.arange(num_samples), time_steps) # shape (num_samples*time_steps,)
Convert diff_list to a numpy array for easier handling
diff_array = np.array(diff_list) # shape (num_samples,)
Map trial diff values to each timepoint
point_colors = diff_array[trial_indices] # shape matches PC (num_samples*time_steps,)
Handle NaNs in diff_list by setting them to a neutral middle value for coloring (e.g. 0)
Or a distinct color using color_discrete_map (but here continuous colorscale for scatter3d)
nan_mask = np.isnan(point_colors) point_colors[nan_mask] = 0 # assign to middle color temporarily
Normalize diff values to [0,1] for coloring
Use min/max ignoring NaNs originally, here already handled
vmin = np.nanmin(diff_array) vmax = np.nanmax(diff_array) if vmin == vmax: # avoid zero division norm_colors = np.ones_like(point_colors) * 0.5 else: norm_colors = (point_colors - vmin) / (vmax - vmin)
Custom diverging colorscale from red (0), white(0.5), to blue (1)
colorscale = [ [0.0, 'darkred'], # low values - dark red [0.5, 'white'], # middle - white [1.0, 'darkblue'], # high values - dark blue ]
Create scatter3d colored by normalized diff values
import plotly.graph_objects as go fig = px.scatter_3d( x=PC[:, 0], y=PC[:, 1], z=PC[:, 2], color=norm_colors, color_continuous_scale=colorscale, title='BRNN, 50n, NoCo(Auta=F), seed=2020, iter=100k', )
fig.update_traces(marker=dict(size=1))
Add cones/arrows as before
arrow_start = PC[0:-1:4] arrow_end = PC[1::4] vectors = arrow_end - arrow_start """ fig.add_trace(Cone( x=arrow_start[:, 0], y=arrow_start[:, 1], z=arrow_start[:, 2], u=vectors[:, 0], v=vectors[:, 1], w=vectors[:, 2], colorscale='reds', # Arrow color showscale=False, sizemode='absolute', sizeref=0.6, # Adjust size of cones (arrow heads) anchor='tip', )) """ fig.show()
Optionally, cleanup
del w_state_var
- Assistant
To change the name of the color bar to "Coherance" in your Plotly scatter_3d plot, you simply need to add the parameter
color_continuous_midpoint(optional) as before and update thecolor_continuous_scaleand set thecoloraxis_colorbar_titlein the figure layout or passlabelsmapping when creating the plot.Here's the relevant modified part of your code to set the color bar title:
pythonimport plotly.express as px import plotly.graph_objects as go fig = px.scatter_3d( x=PC[:, 0], y=PC[:, 1], z=PC[:, 2], color=norm_colors, color_continuous_scale=colorscale, title='BRNN, 50n, NoCo(Auta=F), seed=2020, iter=100k', labels={'color': 'Coherance'} # This changes the color bar title ) fig.update_traces(marker=dict(size=1)) # rest of your code... fig.show()Alternatively, if the above arg doesn't work for some reason, you can update colorbar title using: