更新 PyQt5 Matplotlib 中嵌入的 Networkx 图

Update Networkx graph embedded in PyQt5 Matplotlib

我创建了一个 GUI,用户可以在其中使用按钮打开选项卡。当他这样做时,它会添加以下选项卡:

class data_tab(QtWidgets.QWidget, data_tab_lib.Ui_data_tab):

#=========================================================================================
# Constructor
#=========================================================================================
def __init__(self, parent, title):

    QtWidgets.QWidget.__init__(self, parent)
    self.setupUi(self)

    # initialize save data button icon
    icon = _createIcon("dataname_save")
    self.dataname_save.setIcon(icon)

    self.canvas = Canvas(data_tab)
    self.axe = self.canvas.figure.add_subplot(111)
    self.canvas.figure.subplots_adjust(left=0.025, top=0.965, bottom=0.040, right=0.975)
    # add the tab to the parent
    parent.addTab(self, "")

    # set text name
    parent.setTabText(parent.indexOf(self), title)

parent 正在

self.core_tab = QtWidgets.QTabWidget(self.main_widget)

主要window.

"Canvas"定义如下:

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt

class Canvas(FigureCanvas):
    def __init__(self, parent=None):
        self.figure = plt.figure()
        FigureCanvas.__init__(self, self.figure)
        self.setParent(parent)

单击初始按钮时,会发生这种情况:

     new_data_tab = data_tab(self.core_tab, dataName)

     # associated data to the variable
     associated_data = self._getDataAssociatedToVariable(dataName)

     #1. draw Graph
     self._drawDataGraph(dataName, associated_data, new_data_tab)

dataName 是上面定义的字符串。 _drawDataGraph定义如下:

def _drawDataGraph(self, dataName, associatedData, dataWidget):

    # 1. draw graph
    drawing_dictionary = self._getAllAssociatedVariablesToBeDrawn(dataName, associatedData)
    producer = drawing_dictionary.get(dataName).get("producer")
    consumers = drawing_dictionary.get(dataName).get("consumers")

    color_map = []
    DG = nx.DiGraph()
    DG.add_node(producer)
    for cons in consumers:
        DG.add_node(cons)
    edges_bunch = [(producer, cons, 1) for cons in consumers]
    DG.add_weighted_edges_from(edges_bunch)

    for node in drawing_dictionary.keys():
        if node != dataName:
            DG.add_node(drawing_dictionary.get(node).get("producer"))
            for node_cons in drawing_dictionary.get(node).get("consumers"):
                DG.add_node(node_cons)

            other_edges_bunch = [(drawing_dictionary.get(node).get("producer"), node_cons, 1) for node_cons in
                                 drawing_dictionary.get(node).get("consumers")]
            DG.add_weighted_edges_from(other_edges_bunch)
    for i in range(len(DG.nodes())):
        if i < 1 + len(consumers):
            color_map.append("#DCE46F")
        else:
            color_map.append("#6FA2E4")
    #pos = nx.spring_layout(DG, k=0.4, iterations=20)
    nx.draw_circular(DG, node_color=color_map, with_labels=True, font_size=8, node_size=1000, node_shape='o')
    dataWidget.canvas.draw()

我不会通过 _getAllAssociatedVariablesToBeDrawn 函数,因为它只是 returns 带有键提供列表的字典,这不是这里的问题。

因此,在创建时(最初单击按钮时),一切正常,显示了精美的 Networkx 图。

我的问题是我有另一个按钮,我想在其中刷新当前显示的所有图表:

def refreshFlowsDiagramFromDataTabs(self):

    # loop through all pages
    for tab_index in range(self.core_tab.count()):
        data_tab_widget = self.core_tab.widget(tab_index)
        data_tab_widget.axe.cla()

        # associated data to the variable
        data_name = self.core_tab.tabText(tab_index)

        associated_data = self._getDataAssociatedToVariable(data_name)

        # 1. draw graph
        self._drawDataGraph(data_name, associated_data, data_tab_widget)

遗憾的是,当点击按钮时,只有最后一张图(所以n-th标签中的一张)被刷新,之前的所有图都是空白的(因为它们已经被[=46=清除了) ]data_tab_widget.axe.cla())

我试图评论 data_tab_widget.axe.cla() 以观察发生了什么:在这种情况下,显然第 1 到 (n-1) 个图不是已清除,但最后一张是用之前的所有图制作的,即n-th张图显示在最后一张上。

我绝不是 matplotlib 或 networkx 的专家,所以我不明白我做错了什么,这可能是一件非常简单的事情,但我很乐意就该主题提供一些帮助.

希望我提供的代码足够了,应该可以的。

编辑

请在下面找到完全可重现的代码:添加选项卡然后刷新它们以观察错误。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import networkx as nx

class Canvas(FigureCanvas):
    def __init__(self, parent=None):
        self.figure = plt.figure()
        FigureCanvas.__init__(self, self.figure)
        self.setParent(parent)

class data_tab(QtWidgets.QWidget):

    #=========================================================================================
    # Constructor
    #=========================================================================================
    def __init__(self, parent, title):

        QtWidgets.QWidget.__init__(self, parent)

        self.data_tab_glayout = QtWidgets.QGridLayout(self)
        self.data_tab_glayout.setObjectName("data_tab_glayout")
        self.canvas = Canvas(self)
        self.canvas.setObjectName("canvas")
        self.canvas_vlayout = QtWidgets.QVBoxLayout(self.canvas)
        self.canvas_vlayout.setObjectName("canvas_vlayout")
        self.data_tab_glayout.addWidget(self.canvas, 0, 0, 2, 1)

        self.axe = self.canvas.figure.add_subplot(111)
        self.canvas.figure.subplots_adjust(left=0.025, top=0.965, bottom=0.040, right=0.975)
        # add the tab to the parent
        parent.addTab(self, "")

        # set text name
        parent.setTabText(parent.indexOf(self), title)


class spec_writer(QtWidgets.QMainWindow):


    #=========================================================================================
    # Constructor
    #=========================================================================================
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)

        self.showMaximized()

        self.centralwidget = QtWidgets.QWidget(self)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.core_tab = QtWidgets.QTabWidget(self.centralwidget)
        self.verticalLayout.addWidget(self.core_tab)
        self.add_tab_btn = QtWidgets.QPushButton(self.centralwidget)
        self.verticalLayout.addWidget(self.add_tab_btn)
        self.refresh_tab_btn = QtWidgets.QPushButton(self.centralwidget)
        self.verticalLayout.addWidget(self.refresh_tab_btn)
        self.setCentralWidget(self.centralwidget)

        self.add_tab_btn.setText("Add Tab")
        self.refresh_tab_btn.setText("Refresh Tabs")

        self.core_tab.setEnabled(True)
        self.core_tab.setTabShape(QtWidgets.QTabWidget.Rounded)
        self.core_tab.setElideMode(QtCore.Qt.ElideNone)
        self.core_tab.setDocumentMode(False)
        self.core_tab.setTabsClosable(True)
        self.core_tab.setMovable(True)
        self.core_tab.setTabBarAutoHide(False)

        self.tab_counter = 0

        self.random_tabs = [("a", ["b", "c"]),
                            ("d", ["e", "f", "g"]),
                            ("h", ["i", "j", "k", "l"]),
                            ("m", ["n"]),
                            ("o", ["p", "q"]),
                            ("r", ["s", "t", "u", "v", "w", "x", "y", "z"])]

        self.add_tab_btn.clicked.connect(self.openRandomTab)
        self.refresh_tab_btn.clicked.connect(self.refreshAllTabs)

    def openRandomTab(self):

        tab = data_tab(self.core_tab, "test " + str(self.tab_counter))
        self._drawDataGraph(self.tab_counter % len(self.random_tabs), tab)
        self.tab_counter += 1

        self.core_tab.setCurrentIndex(self.core_tab.indexOf(tab))


    def _drawDataGraph(self, tabNb, dataWidget):

        # 1. draw graph
        producer = self.random_tabs[tabNb][0]
        consumers = self.random_tabs[tabNb][1]

        color_map = []
        DG = nx.DiGraph()
        DG.add_node(producer)
        for cons in consumers:
            DG.add_node(cons)
        edges_bunch = [(producer, cons, 1) for cons in consumers]
        DG.add_weighted_edges_from(edges_bunch)
        for i in range(len(DG.nodes())):
            if i < 1 + len(consumers):
                color_map.append("#DCE46F")
            else:
                color_map.append("#6FA2E4")
        #pos = nx.spring_layout(DG, k=0.4, iterations=20)
        nx.draw_circular(DG, node_color=color_map, with_labels=True, font_size=8, node_size=1000, node_shape='o')
        dataWidget.canvas.draw_idle()


    def refreshAllTabs(self):

        # loop through all pages and associated to get
        for tab_index in range(self.core_tab.count()):
            data_tab_widget = self.core_tab.widget(tab_index)
            data_tab_widget.axe.cla()

            # draw graph
            self._drawDataGraph(tab_index % len(self.random_tabs), data_tab_widget)




sys.argv = ['']
app = QtWidgets.QApplication(sys.argv)
cbtc_spec_writer = spec_writer()
cbtc_spec_writer.show()
app.exec_()

如果要使用后端,则不应使用 pyplot,因为 pyplot 默认仅在当前 canvas 上工作,默认情况下它是最后一个。

例如,以下代码(已重新排序和清理)显示了如何实现它:

from PyQt5 import QtCore, QtWidgets

from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure

import networkx as nx


class DataTab(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)

        self.canvas = FigureCanvas(Figure(figsize=(5, 3)))
        self.axes = self.canvas.figure.add_subplot(111)
        self.canvas.figure.subplots_adjust(
            left=0.025, top=0.965, bottom=0.040, right=0.975
        )
        data_tab_glayout = QtWidgets.QGridLayout(self)
        data_tab_glayout.addWidget(self.canvas)


class Spec_Writer(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.showMaximized()

        self.core_tab = QtWidgets.QTabWidget(
            tabShape=QtWidgets.QTabWidget.Rounded,
            elideMode=QtCore.Qt.ElideNone,
            documentMode=False,
            tabsClosable=True,
            movable=True,
            tabBarAutoHide=False,
        )
        self.add_tab_btn = QtWidgets.QPushButton("Add Tab")
        self.refresh_tab_btn = QtWidgets.QPushButton("Refresh Tabs")

        self.centralwidget = QtWidgets.QWidget()
        self.setCentralWidget(self.centralwidget)

        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.addWidget(self.core_tab)
        self.verticalLayout.addWidget(self.add_tab_btn)
        self.verticalLayout.addWidget(self.refresh_tab_btn)

        self.tab_counter = 0

        self.random_data = [
            ("a", ["b", "c"]),
            ("d", ["e", "f", "g"]),
            ("h", ["i", "j", "k", "l"]),
            ("m", ["n"]),
            ("o", ["p", "q"]),
            ("r", ["s", "t", "u", "v", "w", "x", "y", "z"]),
        ]

        self.add_tab_btn.clicked.connect(self.open_random_tab)
        self.refresh_tab_btn.clicked.connect(self.refresh_all_tabs)

    def open_random_tab(self):
        tab = DataTab()
        index = self.core_tab.addTab(tab, "test {}".format(self.tab_counter))
        self.core_tab.setCurrentIndex(index)
        self.tab_counter += 1
        self._draw_graph(self.tab_counter % len(self.random_data), tab)

    def _draw_graph(self, index, tab):
        tab.axes.cla()
        producer, consumers = self.random_data[index]
        color_map = []
        DG = nx.DiGraph()
        DG.add_node(producer)
        for cons in consumers:
            DG.add_node(cons)
        edges_bunch = [(producer, cons, 1) for cons in consumers]
        DG.add_weighted_edges_from(edges_bunch)
        for i in range(len(DG.nodes())):
            if i < 1 + len(consumers):
                color_map.append("#DCE46F")
            else:
                color_map.append("#6FA2E4")
        # pos = nx.spring_layout(DG, k=0.4, iterations=20)
        nx.draw_circular(
            DG,
            node_color=color_map,
            with_labels=True,
            font_size=8,
            node_size=1000,
            node_shape="o",
            ax=tab.axes,
        )
        tab.canvas.draw()

    def refresh_all_tabs(self):
        for tab_index in range(self.core_tab.count()):
            data_tab_widget = self.core_tab.widget(tab_index)
            self._draw_graph(tab_index % len(self.random_data), data_tab_widget)


def main():
    app = QtWidgets.QApplication([])
    cbtc_spec_writer = Spec_Writer()
    cbtc_spec_writer.show()
    app.exec_()


if __name__ == "__main__":
    main()