pyqtgraph heapmap 热图 伪彩色
如何设置?
核心代码:
self.IVNetwork = pg.ImageView() self.IVNetwork.ui.menuBtn.setVisible(False) self.IVNetwork.ui.roiBtn.setVisible(False) # self.IVNetwork.ui.histogram.setVisible(False) self.winDDD.verticalLayout_11.addWidget(self.IVNetwork)
self.IVNetwork.setImage(dynamicMI) colors = [ (0, 0, 0), (45, 5, 61), (84, 42, 55), (150, 87, 60), (208, 171, 141), (255, 255, 255) ] cmap = pg.ColorMap(pos=np.linspace(0.0, 1.0, 6), color=colors) self.IVNetwork.setColorMap(cmap)
示例代码:
def slotCreateDDD(self): # "创建Dynamics Degree Distribution Windows" # 默认 Dynamic of MI 的matlab矩阵已经算好了 self.winDDD = uic.loadUi(r'./GUI/DynamiceDegreeDistribution.ui') self.winDDD.setWindowTitle("Dynamic of degree distribution") self.IVDistribution = pg.ImageView() self.IVDistribution.ui.menuBtn.setVisible(False) self.IVDistribution.ui.roiBtn.setVisible(False) self.IVDistribution.ui.histogram.setVisible(False) self.IVNetwork = pg.ImageView() self.IVNetwork.ui.menuBtn.setVisible(False) self.IVNetwork.ui.roiBtn.setVisible(False) # self.IVNetwork.ui.histogram.setVisible(False) self.winDDD.verticalLayout_4.addWidget(self.IVDistribution) self.winDDD.verticalLayout_11.addWidget(self.IVNetwork) # 加载提前计算好的Dynamics of MI array dynamicMatrixDataPath = r'D:\PythonProject\QTNLS\TNCalciumAnalysis\dynamicMI.mat' mat = scio.loadmat(dynamicMatrixDataPath) dynamicMI = mat['dynamicMI'] #设置成热图 有些问题,不好好搞。pyqtGraph的API设计得不行。 self.IVNetwork.setImage(dynamicMI) colors = [ (0, 0, 0), (45, 5, 61), (84, 42, 55), (150, 87, 60), (208, 171, 141), (255, 255, 255) ] cmap = pg.ColorMap(pos=np.linspace(0.0, 1.0, 6), color=colors) self.IVNetwork.setColorMap(cmap) #请把distribution也渲染出来; dynamicDistribution = None #先打印出来把 frames = dynamicMI.shape[0] numNeurons = dynamicMI.shape[1] thres = 3.1 dynamicDistribution = np.ones((frames, 512, 512, 4)) dynamicDistributionDataPath = r'D:\PythonProject\QTNLS\resources\dynamicDistribution.npy' if os.path.exists(dynamicDistributionDataPath): dynamicDistribution = np.load(dynamicDistributionDataPath) else: for t in range(frames): print(t) G = nx.Graph() for i in range(numNeurons): for j in range(i, numNeurons): if dynamicMI[t,i,j] > thres: G.add_edge(i,j) # print(G.degree()) degree_sequence = sorted([d for n, d in G.degree()], reverse=True) fig, ax = plt.subplots(figsize=(4, 4), dpi=128) ax.bar(*np.unique(degree_sequence, return_counts=True)) ax.set_xlim(xmin = 0, xmax = numNeurons)#度分布,最大的度就是所有人都连上了 ax.set_ylim(ymin = 0, ymax = 15)#度分布,最大的度就是所有人都连上了 ax.set_title("Degree histogram") ax.set_xlabel("Degree") ax.set_ylabel("# of Nodes") buf = io.BytesIO() fig.savefig(buf, format='raw', dpi=128) buf.seek(0) img_arr = np.frombuffer(buf.getvalue(), dtype=np.uint8) buf.close() plt.cla() plt.close("all") w, h = fig.canvas.get_width_height() im = img_arr.reshape((int(h), int(w), -1)) im = im.transpose((1, 0, 2)) dynamicDistribution[t,:,:,:] = im np.save(dynamicDistributionDataPath,dynamicDistribution) self.IVDistribution.setImage(dynamicDistribution) self.winDDD.show()