PyQt 和 Netgraph 交互图
PyQt and Netgraph Interactive Graph
我有一个使用 PyQt 的应用程序,我想将 Netgraph 图形交互性添加到我的应用程序中。
但是,我无法单击图形的顶点和边。
这是一个最小的例子:
import sys
import matplotlib
import matplotlib.pyplot as plt
from netgraph import InteractiveGraph
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT
from matplotlib.figure import Figure
import networkx as nx
matplotlib.use('Qt5Agg')
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
g = nx.house_x_graph()
fig, ax = plt.subplots(figsize=(10, 10))
plot_instance = InteractiveGraph(g)
super(MplCanvas, self).__init__(fig)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
sc = MplCanvas(self, width=5, height=4, dpi=100)
toolbar = NavigationToolbar2QT(sc, self)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(toolbar)
layout.addWidget(sc)
widget = QtWidgets.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.show()
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
app.exec_()
这是我想要的行为示例:
import matplotlib.pyplot as plt
import networkx as nx
from netgraph import EditableGraph
g = nx.house_x_graph()
fig, ax = plt.subplots(figsize=(10, 10))
plot_instance = EditableGraph(g)
plt.show()
关于如何使 Netgraph 交互性与 PyQt 一起工作的任何想法?
有几个错误,但最重要的是:
- 您必须使用 canvas 中的相同图形,而不是使用 pyplot 创建新图形。
- 您必须将轴作为参数传递给 InteractiveGraph,以便它使用已经创建的图形。
- 将 InteractiveGraph 的实例保存为 class 的属性。
import sys
from PyQt5 import QtWidgets
import matplotlib
matplotlib.use("Qt5Agg")
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT
from matplotlib.figure import Figure
from netgraph import InteractiveGraph
import networkx as nx
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
super(MplCanvas, self).__init__(Figure(figsize=(width, height), dpi=dpi))
self.setParent(parent)
self.ax = self.figure.add_subplot(111)
graph = nx.house_x_graph()
self.plot_instance = InteractiveGraph(graph, ax=self.ax)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
self.toolbar = NavigationToolbar2QT(self.canvas, self)
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
layout = QtWidgets.QVBoxLayout(widget)
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
def main():
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
if __name__ == "__main__":
main()
我有一个使用 PyQt 的应用程序,我想将 Netgraph 图形交互性添加到我的应用程序中。
但是,我无法单击图形的顶点和边。
这是一个最小的例子:
import sys
import matplotlib
import matplotlib.pyplot as plt
from netgraph import InteractiveGraph
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT
from matplotlib.figure import Figure
import networkx as nx
matplotlib.use('Qt5Agg')
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
g = nx.house_x_graph()
fig, ax = plt.subplots(figsize=(10, 10))
plot_instance = InteractiveGraph(g)
super(MplCanvas, self).__init__(fig)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
sc = MplCanvas(self, width=5, height=4, dpi=100)
toolbar = NavigationToolbar2QT(sc, self)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(toolbar)
layout.addWidget(sc)
widget = QtWidgets.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.show()
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
app.exec_()
这是我想要的行为示例:
import matplotlib.pyplot as plt
import networkx as nx
from netgraph import EditableGraph
g = nx.house_x_graph()
fig, ax = plt.subplots(figsize=(10, 10))
plot_instance = EditableGraph(g)
plt.show()
关于如何使 Netgraph 交互性与 PyQt 一起工作的任何想法?
有几个错误,但最重要的是:
- 您必须使用 canvas 中的相同图形,而不是使用 pyplot 创建新图形。
- 您必须将轴作为参数传递给 InteractiveGraph,以便它使用已经创建的图形。
- 将 InteractiveGraph 的实例保存为 class 的属性。
import sys
from PyQt5 import QtWidgets
import matplotlib
matplotlib.use("Qt5Agg")
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT
from matplotlib.figure import Figure
from netgraph import InteractiveGraph
import networkx as nx
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
super(MplCanvas, self).__init__(Figure(figsize=(width, height), dpi=dpi))
self.setParent(parent)
self.ax = self.figure.add_subplot(111)
graph = nx.house_x_graph()
self.plot_instance = InteractiveGraph(graph, ax=self.ax)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
self.toolbar = NavigationToolbar2QT(self.canvas, self)
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
layout = QtWidgets.QVBoxLayout(widget)
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
def main():
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
if __name__ == "__main__":
main()