I'm trying to vizualize simulation results with a figure containing 2 subplots using matplotlib pyplot. Both should contain animation: one uses netgraph library (it's a graph with nodes showing flows of the network) and the other should plot a line graph of another 2 variables (to keep it simple here lets use: sin(x) & cos(x) where both should be updated at each time period--just like the graph). I have an update function to update the graph, but I'm unsure, how to update the line plot at the same time. I would appreciate any suggestions.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from netgraph import Graph
# Simulate a dynamic network with
total_frames = 21
total_nodes = 5
NODE_LABELS = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}
NODE_POS = {0: (0.0, 0.5), 1: (0.65, 0.25), 2: (0.7, 0.5), 3: (0.5, 0.75), 4: (0.25, 0.25)}
adjacency_matrix = np.random.rand(total_nodes, total_nodes) < 0.25
weight_matrix = np.random.randn(total_frames, total_nodes, total_nodes)
# Normalise the weights, such that they are on the interval [0, 1].
vmin, vmax = -2, 2
weight_matrix[weight_matrix<vmin] = vmin
weight_matrix[weight_matrix>vmax] = vmax
weight_matrix -= vmin
weight_matrix /= vmax - vmin
cmap = plt.cm.RdGy
def annotate_axes(fig):
for i, ax in enumerate(fig.axes):
ax.tick_params(labelbottom=False, labelleft=False)
fig = plt.figure(figsize=(11, 6))
ax1 = plt.subplot2grid((6, 11), (0, 0), rowspan=6, colspan=5)
ax2 = plt.subplot2grid((6, 11), (1, 6), rowspan=2, colspan=5)
annotate_axes(fig)
title1 = ax1.set_title('Simulation viz', x=0.25, y=1.25)
title2 = ax2.set_title('Flow @t', x=0.15, y=1.25)
g = Graph(adjacency_matrix, node_labels=NODE_LABELS,
node_layout = NODE_POS, edge_cmap=cmap, arrows=True, ax=ax1)
def update(ii):
artists = []
for jj, kk in zip(*np.where(adjacency_matrix)):
w = weight_matrix[ii, jj, kk]
artist = g.edge_artists[(jj, kk)]
artist.set_facecolor(cmap(w))
artist.update_width(0.03 * np.abs(w-0.5))
artists.append(artist)
return artists
animation = FuncAnimation(fig, update, frames=total_frames, interval=200, blit=True, repeat=False)
plt.show()
It seems your script does not contain lines for sin/cos implementation and plotting. I added them and ran the script, it shows both plots. Try this
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from netgraph import Graph
# Simulate a dynamic network
total_frames = 21
total_nodes = 5
NODE_LABELS = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}
NODE_POS = {0: (0.0, 0.5), 1: (0.65, 0.25), 2: (0.7, 0.5), 3: (0.5, 0.75), 4: (0.25, 0.25)}
adjacency_matrix = np.random.rand(total_nodes, total_nodes) < 0.25
weight_matrix = np.random.randn(total_frames, total_nodes, total_nodes)
# Normalize the weights
vmin, vmax = -2, 2
weight_matrix[weight_matrix < vmin] = vmin
weight_matrix[weight_matrix > vmax] = vmax
weight_matrix -= vmin
weight_matrix /= vmax - vmin
cmap = plt.cm.RdGy
def annotate_axes(fig):
for i, ax in enumerate(fig.axes):
ax.tick_params(labelbottom=False, labelleft=False)
# Prepare figure
fig = plt.figure(figsize=(11, 6))
ax1 = plt.subplot2grid((6, 11), (0, 0), rowspan=6, colspan=5)
ax2 = plt.subplot2grid((6, 11), (1, 6), rowspan=2, colspan=5)
annotate_axes(fig)
title1 = ax1.set_title('Simulation viz', x=0.25, y=1.25)
title2 = ax2.set_title('Flow @t', x=0.15, y=1.25)
# Initialize Graph
g = Graph(adjacency_matrix, node_labels=NODE_LABELS,
node_layout=NODE_POS, edge_cmap=cmap, arrows=True, ax=ax1)
# Prepare x-axis values for sin and cos
x = np.linspace(0, 2*np.pi, 100)
# Initialize two line plots (empty initially)
line_sin, = ax2.plot([], [], label='sin(x)')
line_cos, = ax2.plot([], [], label='cos(x)')
ax2.set_xlim(0, 2*np.pi)
ax2.set_ylim(-1.5, 1.5)
ax2.legend()
# Define update function
def update(ii):
artists = []
# Update the graph (netgraph)
for jj, kk in zip(*np.where(adjacency_matrix)):
w = weight_matrix[ii, jj, kk]
artist = g.edge_artists[(jj, kk)]
artist.set_facecolor(cmap(w))
artist.update_width(0.03 * np.abs(w - 0.5))
artists.append(artist)
# Update the line plots
y_sin = np.sin(x + ii * 0.2) # phase shift over time
y_cos = np.cos(x + ii * 0.2)
line_sin.set_data(x, y_sin)
line_cos.set_data(x, y_cos)
artists.append(line_sin)
artists.append(line_cos)
return artists
# Animate
animation = FuncAnimation(fig, update, frames=total_frames, interval=200, blit=True, repeat=False)
plt.show()