简体   繁体   English

我正在使用 matplotlib 绘制图形,该函数将被多次调用。 如何使图形绘制更快?

[英]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.我读过有一个名为 pyqt 的库,它可以用于更快的图形绘制,并且可以用来代替 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.希望你喜欢这个 pyqtgraph,是的,这对于大量数据来说非常快速和可靠。 Here is the working example with your data using pyqtgraph.这是使用 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.在这里,我使用 PyQT5 窗口来显示图像,这样做对我来说更容易。

Note : You can create your own colormap if it is necessary.注意:如有必要,您可以创建自己的颜色图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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