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

发布于 2025-02-11 02:39:25 字数 678 浏览 1 评论 0原文

我读到有一个称为pyqt的库,可用于更快的图形绘图,可以代替matplotlib。我该如何在现有的代码中使用它。

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)

的图

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)

This is the graph that I have

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作死小能手 2025-02-18 02:39:25

希望您喜欢使用这个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_())

在这里,我使用PYQT5窗口显示图像,对我来说更容易。

Note :如果需要,您可以创建自己的colormap。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文