微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在一个函数中将 matplotlib 图表与 networkX 图表结合起来?

如何解决如何在一个函数中将 matplotlib 图表与 networkX 图表结合起来?

我开发了以下 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

enter image description here

然后我尝试在函数中编写代码以供动态使用,但似乎我出错了,因为不知何故,它没有按预期工作,底部的动画折线图没有显示。这是 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

enter image description here

我使用的是 Python 3.6、networkx 2.4、matplotlib 3.3.3 和 IDLE 和 Win10。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。