PyQt5中嵌入Matplotlib步驟如下:
第一步,通過matplotlib.backends.backend_qt5agg類來連接PyQt5:
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import networkx as nx
第二步,networdX畫具體圖形代碼實現(xiàn)部分,例:
class Figure_Canvas(FigureCanvas):
# 通過繼承FigureCanvas類,使得該類既是一個PyQt5的Qwidget,又是一個matplotlib的FigureCanvas,這是連接pyqt5與matplotlib的關(guān)鍵
def __init__(self):
self.fig = plt.figure()# 可選參數(shù),facecolor為背景顏色
FigureCanvas.__init__(self, self.fig) # 初始化激活widget中的plt部分
def draw_graph(self):
node_color = ['r']
data = {'張三':
{'校友': ['李一', '李二', '李三', '李四'],
'同學(xué)': ['王一', '王二', '王三', '王四', '王五'],
'同事': ['馬一', '馬二', '馬三', '馬四', '馬五', '馬六']
}
}
self.G = nx.Graph()
for k, v in data.items():
self.G.add_node(k, id='0001')
for m, n in v.items():
if m == '校友':
node_color.extend(['b'] * len(n))
for i in n:
self.G.add_node(i, id='0002')
self.G.add_edge(k, i, weight=0.3, relation='校友')
elif m == '同學(xué)':
node_color.extend(['b'] * len(n))
for i in n:
self.G.add_node(i, id='0003')
self.G.add_edge(k, i, weight=0.5, relation='同學(xué)')
else:
node_color.extend(['g'] * len(n))
for i in n:
self.G.add_node(i, id='0003')
self.G.add_edge(k, i, weight=0.9, relation='同事')
pos = nx.spring_layout(self.G)
relations = {}
for po in pos:
print(po, pos[po])
xy = [[pos[po][0] - 0.05, pos[po][0] + 0.05],
[pos[po][1] - 0.05, pos[po][1] + 0.05]]
relations[po] = xy
labels = {}
for edge in self.G.edges(data=True):
labels[(edge[0], edge[1])] = edge[-1]['relation']
#如果不使用canvas.mpl_connect的話將不能激活event
self.fig.canvas.mpl_connect('button_press_event', lambda event: self.on_press(event, relations))
nx.draw(self.G, pos,with_labels=True, node_color=node_color, edge_color='b', node_size=1000,
rotate=True)
nx.draw_networkx_edge_labels(self.G, pos, edge_labels=labels)
第三步,GUI上通過控件呈現(xiàn)matplotlib畫出來的圖形——通過QtWidgets.QGraphicsView控件來實現(xiàn):(代碼部分僅呈現(xiàn)QtWidgets.QGraphicsView的實現(xiàn)步驟)
# 第一步,創(chuàng)建一個QGraphicsView,注意同樣以gridLayoutWidget為參數(shù)
self.graphicview = QtWidgets.QGraphicsView(self.gridLayoutWidget)
# 第二步,設(shè)置graphicview的區(qū)域
self.graphicsView.setGeometry(QtCore.QRect(40, 10, 481, 431))
# 第三步,實例化一個FigureCanvas
dr = Figure_Canvas()
dr.draw_graph() # 畫圖
# 第四步,創(chuàng)建一個QGraphicsScene,因為加載的圖形(FigureCanvas)不能直接放到graphicview控件中,必須先放到graphicScene,然后再把graphicscene放到graphicview中
graphicscene = QtWidgets.QGraphicsScene()
# 第五步,把圖形放到QGraphicsScene中,注意:圖形是作為一個QWidget放到QGraphicsScene中的
graphicscene.addWidget(dr)
# 第六步,把QGraphicsScene放入QGraphicsView
self.graphicview.setScene(graphicscene)
# 第七步,調(diào)用show方法呈現(xiàn)圖形!
self.graphicview.show()
Matplotlib繪圖自適應(yīng)Pyqt5
QGraphicsScene無法放入布局中,導(dǎo)致繪圖無法彈性布局
1、創(chuàng)建一個widget,在widget中加上QVBoxLayout
2、創(chuàng)建你的canvas,注意其父類是widget,不是QVBoxLayout
3、在QVBoxLayout中調(diào)用addWidget把canvas加進去
self.graphicsView = QtWidgets.QGraphicsView(self.tab_2)
self.qvbl = QtWidgets.QVBoxLayout(self.graphicsView)
# 判斷布局中是否有控件
if self.qvbl.count() == 0:
self.qvbl.addWidget(self.dr)
else:
# 先清空布局中的所有控件,再添加新的控件
for i in range(self.qvbl.count()):
self.qvbl.itemAt(i).widget().deleteLater()
self.qvbl.addWidget(self.dr)
QGrphicsView, QGraphicsScene 和 QGraphicsItem 的區(qū)別
1.Scene是一個場景,View是一個視野。如果視圖大于場景時,場景在中間部分顯示[如果不設(shè)置視圖的Alignment屬性];如果視圖小于場景,則只能看到場景的一部分,但是會自動提供滾動條在整個場景中移動
2.QGraphicsView默認帶有內(nèi)邊距和邊界setStyleSheet("padding: 0px; border: 0px;")
3.QGrphicsView類實際上是為QGraphicsScene類的內(nèi)容提空了一個控件,它在一個可滑動視圖空間內(nèi)可視化QGraphicsScene的內(nèi)容。
QGraphicsScene類提供了一個平面來管理大量的2D的圖像項目,像點,直線,多邊形,圓等。它是QGraphicsItem類的容器。
打個比方來說,QGrphicsView類就像是一片空曠的場地,QGraphicsScene類是上面搭建的一個舞臺,QGraphicsItem類是舞臺上的各種東西
參考:
https://www.cnblogs.com/laoniubile/p/5904817.html
https://blog.csdn.net/qq_31809257/article/details/89292824