I want to plot three figures in one row, please write the code accordingly:…
- User
I want to plot three figures in one row, please write the code accordingly: plot_weights(primary_weights['W_rec'], "before train") plot_weights(weights['W_rec'], "after train") plot_weights(weights['W_rec']- primary_weights['W_rec'], "difference") where def plot_weights(weights, title=""): cmap = plt.set_cmap('RdBu_r') img = plt.matshow(weights, norm=Normalize(vmin=-.5, vmax=.5)) plt.title(title) plt.colorbar()
- Assistant
To plot these three figures side by side in one row, you can use
plt.subplotsto create a single row with three columns of subplots. Then call yourplot_weightsfunction adjusting it to accept anAxesobject so it plots in the correct subplot instead of creating a new figure each time.Here’s the complete code including the updated
plot_weightsand the plotting code for the three figures in a row:import matplotlib.pyplot as plt from matplotlib.colors import Normalize def plot_weights(weights, title="", ax=None): cmap = plt.cm.RdBu_r # use colormap directly, don't set globally if ax is None: ax = plt.gca() img = ax.matshow(weights, cmap=cmap, norm=Normalize(vmin=-.5, vmax=.5)) ax.set_title(title) plt.colorbar(img, ax=ax) # Create subplots with 1 row and 3 columns fig, axs = plt.subplots(1, 3, figsize=(15, 5)) # adjust figsize as needed plot_weights(primary_weights['W_rec'], "before train", ax=axs[0]) plot_weights(weights['W_rec'], "after train", ax=axs[1]) plot_weights(weights['W_rec'] - primary_weights['W_rec'], "difference", ax=axs[2]) plt.tight_layout() plt.show() - User
why the colorbarof the following code is empty:
@title All Trials Desicion Making Manifold Vectorized 2D Over Time
n_samples, n_time_steps, n_features = state_var.shape
w_state_var = state_var.reshape(-1, n_features)
pca = PCA(n_components=3) PC = pca.fit_transform(w_state_var)
time_steps = np.tile(np.arange(n_time_steps), n_samples)
norm = colors.Normalize(vmin=0, vmax=n_time_steps - 1) cmap = cm.viridis
plt.figure(figsize=(10, 8))
ax = plt.gca()
Plot arrows
for i in range(len(PC) - 1): color = cmap(norm(time_steps[i])) if i % 5 == 0: plt.arrow(PC[i, 0], PC[i, 1], PC[i + 1, 0] - PC[i, 0], PC[i + 1, 1] - PC[i, 1], color=color, width=0.0005, head_width=0.04)
Plot a scatter (invisible) just to create a mappable that colorbar can use
sc = ax.scatter(PC[:, 0], PC[:, 1], c=time_steps, cmap="viridis", norm=norm, alpha=0)
plt.xlabel('Principal Component 1') plt.ylabel('Principal Component 2') plt.title('Principal Component Analysis of State Variable Colored by Time Step')
cbar = plt.colorbar(sc, ticks=np.linspace(0, n_time_steps - 1, 6)) cbar.set_label('Time Step')
plt.show() del w_state_var
- Assistant
- User
the code gave this error: ValueError: Unable to determine Axes to steal space for Colorbar. Either provide the cax argument to use as the Axes for the Colorbar, provide the ax argument to steal space from it, or add mappable to an Axes.
- Assistant
- User
add cones of the same size with the direction of time to the following plot :
@title All Trials Desicion Making Manifold Over Time
suppose state_var shape = (n_samples, n_time_steps, n_features)
n_samples, n_time_steps, n_features = state_var.shape
flatten the first two dims to a 2D array with shape (n_samples*n_time_steps, n_features)
w_state_var = state_var.reshape(-1, n_features) print(w_state_var.shape)
apply PCA
pca = PCA(n_components=3) PC = pca.fit_transform(w_state_var)
create color vector corresponding to time steps, repeated over samples
time_colors = np.tile(np.arange(n_time_steps), n_samples)
plot PCA components with color by time
fig = px.scatter_3d(x=PC[:, 0], y=PC[:, 1], z=PC[:, 2], color=time_colors, color_continuous_scale='jet', labels={'color': 'Time Step'}, title='BRNN, 50n, NoCo(Auta=F), seed=2020, iter=100k') fig.show() del w_state_var
- Assistant