简体   繁体   中英

I am plotting a graph using matplotlib, the function would be called multiple times. How can I make the graph plotting faster?

I have read that there is a library called pyqt which can be used for faster graph plotting and can be used in place of matplotlib. How can I use that in my existing piece of code.

import seaborn as sns
import numpy as np


def heatmap2d(arr: np.ndarray):
    sns.heatmap(test_array, linewidths=10, square = True, vmin = 140, vmax=395, cmap='jet')


test_array = [
     [220, 152, 146, 151, 146, 144],
     [142, 156, 290, 174, 152, 151],
     [148, 190, 390, 370, 146, 152],
     [143, 142, 380, 375, 146, 152],
     [154, 146, 154, 172, 150, 152],
     [150, 152, 144, 140, 142, 0]
 ]
heatmap2d(test_array)

这是我的图表

Hope you enjoy doing with this pyqtgraph, Yes, this is pretty fast and reliable for large number of data. Here is the working example with your data using pyqtgraph.

from PyQt5.QtWidgets import QMainWindow, QApplication
import pyqtgraph as pg
import numpy as np
import sys

class mainW(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(mainW, self).__init__(*args, **kwargs)
        imv = pg.GraphicsLayoutWidget(show=True)        
        plot = imv.addPlot(title="non-interactive")
        # prepare demonstration data:
        test_array = [
     [220, 152, 146, 151, 146, 144],
     [142, 156, 290, 174, 152, 151],
     [148, 190, 390, 370, 146, 152],
     [143, 142, 380, 375, 146, 152],
     [154, 146, 154, 172, 150, 152],
     [150, 152, 144, 140, 142, 0]
 ]
        test_array = np.array(test_array)
        print(test_array.shape)

        # Example: False color image with interactive level adjustment
        img = pg.ImageItem(test_array) # create monochrome image from demonstration data
        #img = imv.setImage(test_array)
        plot.addItem( img )            # add to PlotItem 'plot'
        cm = pg.colormap.get('turbo', source='matplotlib') # prepare a color map from matplotlib, you can create your own color map as well.
        bar = pg.ColorBarItem( values= (140, 395), width=10, colorMap=cm ) # prepare interactive color bar
        # Have ColorBarItem control colors of img and appear in 'plot':
        bar.setImageItem( img, insert_in=plot ) 
        self.setWindowTitle('pyqtgraph example: Interactive color bar')
        self.resize(800,700)
        self.setCentralWidget(imv)
        self.show()
    


## Start Qt event loop
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    main_window = mainW()
    sys.exit(app.exec_())

Here, I am using PyQT5 window to display image, its easier for me to do so.

Note : You can create your own colormap if it is necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM