-
-
Save pgtwitter/5bd9e244f8b3e2edbb0f865f7b484de4 to your computer and use it in GitHub Desktop.
matplotlibの画像を貼ったnodeを用いたgraphviz
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 tempfile | |
import scipy.interpolate as scipl | |
import networkx as nx | |
import graphviz | |
import numpy as np | |
import matplotlib.pyplot as plt | |
es0 = [ | |
[1, 4], | |
[4, 3], | |
[1, 5], | |
[5, 3], | |
[1, 3], | |
[5, 2] | |
] | |
es1 = [ | |
[1, 2], | |
[1, 3], | |
[1, 4], | |
[1, 5], | |
[3, 4], | |
[4, 5], | |
[4, 6] | |
] | |
es2 = [ | |
[1, 2], | |
[2, 3], | |
[3, 4], | |
[4, 5], | |
[5, 6], | |
[6, 7], | |
] | |
es = es0 | |
G = nx.Graph(es) | |
n = len(G.nodes) | |
A = [[0 for j in range(n)] for i in range(n)] | |
for e in es: | |
A[e[0]-1][e[1]-1] = 1 | |
A[e[1]-1][e[0]-1] = 1 | |
A = np.array(A) | |
step = 15 | |
x = np.array([[1/n for i in range(n)]]).T | |
# x = np.array([[1 if i == 2 else 0] for i in range(n)]) | |
ts = [x] | |
for t in range(step): | |
x = np.dot(A, x) | |
x = x / np.linalg.norm(x) | |
ts.append(x) | |
data = np.array(ts).T[0] | |
x = np.arange(0, 15, 0.1) | |
aa = [] | |
for i in range(len(data)): | |
f_sci = scipl.CubicSpline(range(15+1), data[i]) | |
aa.append(f_sci(x)) | |
dT = np.array(aa).T | |
# %% | |
tmpdir = tempfile.TemporaryDirectory() | |
for t in range(len(dT)): | |
for x in range(len(dT[t])): | |
fig = plt.figure(figsize=(1.5, 2)) | |
plt.ylim(-.1, 1.) | |
plt.bar(1, dT[t][x], width=0.4) | |
plt.xticks([]) | |
plt.xlabel(f'$x_{x+1}$') | |
plt.axhline(0, linestyle='dashed') | |
plt.tight_layout() | |
plt.savefig(os.path.join(tmpdir.name, f'{t:04}_x{x}.png')) | |
# %% | |
for t in range(len(dT)): | |
dot = graphviz.Graph(engine='dot', name=f'{t+1:04}') | |
dot.attr(rankdir='LR') | |
for n in G.nodes: | |
imgpath = os.path.join(tmpdir.name, f'{t:04}_x{n-1}.png') | |
dot.node(f'{n}', shape='box', label='', image=f'{imgpath}') | |
for e in G.edges: | |
dot.edge(f'{e[0]}', f'{e[1]}') | |
dot.render(format='png') | |
''' | |
ffmpeg -an -r 24 -start_number 1 -i %04d.gv.png -vf pad=w=964:h=352:x=0:y=0:color=black -vcodec libx264 -pix_fmt yuv420p ~/Desktop/video.mp4 | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment