The following code does not show arrows head and the colormap based on time, apply them…
- User
The following code does not show arrows head and the colormap based on time, apply them on my code: state_var6 = state_var.copy() data_2d = state_var6.reshape(-1, state_var6.shape[-1]) pca = PCA(n_components=3) PC = pca.fit_transform(data_2d)
fig = px.scatter_3d()#(x=PC[:, 0], y=PC[:, 1], z=PC[:, 2], color=data_2d[:, 49], color_continuous_scale='ylgnbu', title='BRNN, 50n, NoCo(Auta=F), seed=2020, iter=100k')
Add vectors to the 3D scatter plot
for i in range(0, len(PC), 4): fig.add_trace(Scatter3d(x=[PC[i, 0], PC[(i+1) % len(PC), 0]], y=[PC[i, 1], PC[(i+1) % len(PC), 1]], z=[PC[i, 2], PC[(i+1) % len(PC), 2]], mode='lines', line=dict(color='black',width=1))) fig.update_traces(marker=dict(size=0)) fig.show()
- Assistant
To add arrowheads to your 3D vectors and color points by time (assuming time corresponds to the index/order in your data), you can use Plotly's
conetrace for arrows and set colors for points. Here's your updated code with these features applied:import numpy as np import plotly.express as px from plotly.graph_objs import Scatter3d, Cone from sklearn.decomposition import PCA state_var6 = state_var.copy() data_2d = state_var6.reshape(-1, state_var6.shape[-1]) pca = PCA(n_components=3) PC = pca.fit_transform(data_2d) # Create a time array for coloring (e.g., use the point index) time = np.arange(len(PC)) # Create scatter3d for points colored by time with a colormap fig = px.scatter_3d(x=PC[:, 0], y=PC[:, 1], z=PC[:, 2], color=time, color_continuous_scale='ylgnbu', title='BRNN, 50n, NoCo(Auta=F), seed=2020, iter=100k') fig.update_traces(marker=dict(size=3)) # Prepare arrow vectors # We will visualize arrows from PC[i] -> PC[i+1] every 4 steps as you did arrow_start = PC[0:-1:4] arrow_end = PC[1::4] # Calculate vector directions vectors = arrow_end - arrow_start # Add cones for arrows with small sized heads 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='Blacks', # Arrow color showscale=False, sizemode='absolute', sizeref=2, # Adjust size of cones (arrow heads) anchor='tip', showscale=False )) fig.show()