问题描述
我开发了以下 code_1
来呈现 figure_1
。
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
from matplotlib.animation import FuncAnimation
import random
from itertools import count
from matplotlib import gridspec
edge_labels = [0.33,0.55,0.66]
fig = plt.figure(figsize=(10,7))
gs = gridspec.GridSpec(2,2,height_ratios=[3,1])
# ------- Graph Part ---------------------------------------
ax = fig.add_subplot(gs[:-1,:])
G = nx.DiGraph()
l1,l2,l3,l4,l5,l6,l7 ='1','2','3','4','5','6','7'
edgelist= [(l1,l2),(l2,l3),(l3,l4),l5),(l4,l1),(l5,l6),l7)]
e1 = float("{:.3f}".format(edge_labels[0]))
e2 = float("{:.3f}".format(edge_labels[1]))
e3 = float("{:.3f}".format(edge_labels[2]))
e_labels = [e1,e2,e3]
edge_labels = {(l1,l2):e_labels[0],l1):e_labels[0],l3):e_labels[1],l5):e_labels[2],l7):e_labels[2]}
pos = {l1: (10,60),l2: (10,40),l3:(80,l4:(140,l5:(200,20),l6:(250,l7:(250,10)}
ax = nx.draw_networkx_nodes(G,pos=pos,nodelist=list(pos.keys()),node_size=2000,alpha=1)
ax = nx.draw_networkx_edges(G,edgelist= edgelist,edge_color="gray",arrows=True,arrowsize=10,arrowstyle='wedge')
ax = nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels,font_size=15)
ax = nx.draw_networkx_labels(G,labels=dict(zip(pos.keys(),pos.keys())),font_color="black")
# ------- Lines Part -------------------------------------
ax2 = fig.add_subplot(gs[-1,:-1])
ax3 = fig.add_subplot(gs[-1,-1])
x_vals = []
y_vals = []
index = count()
def animate(i):
x_vals.append(next(index))
y_vals.append(random.random())
ax2.cla()
ax2.plot(x_vals,y_vals)
ax3.cla()
ax3.plot(x_vals,y_vals)
ani = FuncAnimation(fig,animate,interval=50,frames= 30,repeat=False)
# -------------------------------------------------------
fig.show()
figure 1
然后我尝试在函数中编写代码以供动态使用,但似乎我出错了,因为不知何故,它没有按预期工作,底部的动画折线图没有显示。这是 code_2
的函数版本的 code_1
。我怀疑它可能在轴声明中,但无法弄清楚我错在哪里。
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
from matplotlib.animation import FuncAnimation
import random
from itertools import count
from matplotlib import gridspec
global ax,ax2,ax3,fig,gs
fig = plt.figure(figsize=(10,1])
def render(edge_labels):
global ax,gs
# ------- Graph Part ---------------------------------------
ax = fig.add_subplot(gs[:-1,:])
G = nx.DiGraph()
l1,'7'
edgelist= [(l1,l7)]
e1 = float("{:.3f}".format(edge_labels[0]))
e2 = float("{:.3f}".format(edge_labels[1]))
e3 = float("{:.3f}".format(edge_labels[2]))
e_labels = [e1,e3]
edge_labels = {(l1,l7):e_labels[2]}
pos = {l1: (10,10)}
ax = nx.draw_networkx_nodes(G,alpha=1)
ax = nx.draw_networkx_edges(G,arrowstyle='wedge')
ax = nx.draw_networkx_edge_labels(G,font_size=15)
ax = nx.draw_networkx_labels(G,font_color="black")
# ------- Lines Part -------------------------------------
ax2 = fig.add_subplot(gs[-1,:-1])
ax3 = fig.add_subplot(gs[-1,-1])
x_vals = []
y_vals = []
index = count()
def animate(i):
global ax2,fig
x_vals.append(next(index))
y_vals.append(random.random())
ax2.cla()
ax2.plot(x_vals,y_vals)
ax3.cla()
ax3.plot(x_vals,y_vals)
ani = FuncAnimation(fig,repeat=False)
# -------------------------------------------------------
fig.show()
render([0.33,0.66])
figure 2
我使用的是 Python 3.6、networkx 2.4、matplotlib 3.3.3 和 IDLE 和 Win10。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)