1# -*- coding: utf-8 -*- 2""" 3This example demonstrates ViewBox and AxisItem configuration to plot a correlation matrix. 4""" 5## Add path to library (just for examples; you do not need this) 6import initExample 7 8import numpy as np 9import pyqtgraph as pg 10from pyqtgraph.Qt import QtWidgets, mkQApp, QtGui 11 12class MainWindow(QtWidgets.QMainWindow): 13 """ example application main window """ 14 def __init__(self, *args, **kwargs): 15 super(MainWindow, self).__init__(*args, **kwargs) 16 gr_wid = pg.GraphicsLayoutWidget(show=True) 17 self.setCentralWidget(gr_wid) 18 self.setWindowTitle('pyqtgraph example: Correlation matrix display') 19 self.resize(600,500) 20 self.show() 21 22 corrMatrix = np.array([ 23 [ 1. , 0.5184571 , -0.70188642], 24 [ 0.5184571 , 1. , -0.86094096], 25 [-0.70188642, -0.86094096, 1. ] 26 ]) 27 columns = ["A", "B", "C"] 28 29 pg.setConfigOption('imageAxisOrder', 'row-major') # Switch default order to Row-major 30 31 correlogram = pg.ImageItem() 32 # create transform to center the corner element on the origin, for any assigned image: 33 tr = QtGui.QTransform().translate(-0.5, -0.5) 34 correlogram.setTransform(tr) 35 correlogram.setImage(corrMatrix) 36 37 plotItem = gr_wid.addPlot() # add PlotItem to the main GraphicsLayoutWidget 38 plotItem.invertY(True) # orient y axis to run top-to-bottom 39 plotItem.setDefaultPadding(0.0) # plot without padding data range 40 plotItem.addItem(correlogram) # display correlogram 41 42 # show full frame, label tick marks at top and left sides, with some extra space for labels: 43 plotItem.showAxes( True, showValues=(True, True, False, False), size=20 ) 44 45 # define major tick marks and labels: 46 ticks = [ (idx, label) for idx, label in enumerate( columns ) ] 47 for side in ('left','top','right','bottom'): 48 plotItem.getAxis(side).setTicks( (ticks, []) ) # add list of major ticks; no minor ticks 49 plotItem.getAxis('bottom').setHeight(10) # include some additional space at bottom of figure 50 51 colorMap = pg.colormap.get("CET-D1") # choose perceptually uniform, diverging color map 52 # generate an adjustabled color bar, initially spanning -1 to 1: 53 bar = pg.ColorBarItem( values=(-1,1), cmap=colorMap) 54 # link color bar and color map to correlogram, and show it in plotItem: 55 bar.setImageItem(correlogram, insert_in=plotItem) 56 57mkQApp("Correlation matrix display") 58main_window = MainWindow() 59 60## Start Qt event loop 61if __name__ == '__main__': 62 pg.exec() 63