如何使用 matplotlib blitting 将 matplot.patches 添加到 wxPython 中的 matplotlib 图?

2023-12-22

我正在使用 matplotlib 库制作一个绘图并在我的 wxPython GUI 中显示它。我正在绘制来自激光雷达仪器的大量数据点。问题是,我想在该图中绘制矩形来指示有趣的区域。但是,当我在与绘图相同的轴上绘制一个矩形时,整个绘图将被重新绘制,这需要花费大量时间。这是因为 self.canvas.draw() 是一个重新绘制所有内容的函数。

代码在 GUI 中显示如下:

GUI 的打印屏幕 https://i.stack.imgur.com/uvzPo.png

这是该问题的一个最小的工作示例。按住鼠标右键可以绘制矩形。一旦使用左侧的按钮绘制 NetCDF 数据,矩形的绘制就会变得非常慢。我使用 ImportanceOfBeingErnest 提供的示例尝试了一些 blitting 的操作,但经过多次尝试,我仍然无法让它工作。

为了使最小工作示例工作,您必须在plot_Data()函数下指定NetCDF文件的路径。我提供了 NetCDF 文件,可以在此处下载:

下载NetCDF文件 https://wetransfer.com/downloads/9b81c54c7eadfccec369e61da99ca1e120180308090343/c1580885a301f27b841c5f690d25662b20180308090343/922a84

如何在 onselect 函数中将 self.square 传输到 self.canvas ?

import netCDF4 as nc

import matplotlib
matplotlib.use('WXAgg')

from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.widgets

import time

import wx

class rightPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent, style=wx.SUNKEN_BORDER)
        self.initiate_Matplotlib_Plot_Canvas()        
        self.add_Matplotlib_Widgets()

    def initiate_Matplotlib_Plot_Canvas(self):
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.colorbar = None
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, proportion=1, flag=wx.ALL | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()
        self.canvas.draw()

    def add_Matplotlib_Widgets(self):

        self.rectangleSelector = matplotlib.widgets.RectangleSelector(self.axes, self.onselect,
                                                                      drawtype="box", useblit=True,
                                                                      button=[3], interactive=False
                                                                      )

    def onselect(self, eclick, erelease):
        tstart = time.time()
        x1, y1 = eclick.xdata, eclick.ydata
        x2, y2 = erelease.xdata, erelease.ydata

        height = y2-y1
        width = x2-x1


        self.square = matplotlib.patches.Rectangle((x1,y1), width, 
                                                   height, angle=0.0, edgecolor='red',
                                                   fill=False
                                                   #blit=True gives Unknown property blit
                                                   )


        self.axes.add_patch(self.square)
        self.canvas.draw()

        # =============================================================================
        #         self.background = self.canvas.copy_from_bbox(self.axes.bbox)
        #         
        #         
        #         self.canvas.restore_region(self.background)
        #        
        #         self.axes.draw_artist(self.square)
        #        
        #         self.canvas.blit(self.axes.bbox)
        # =============================================================================


        tend = time.time()
        print("Took " + str(tend-tstart) + " sec")

    def plot_Data(self):
        """This function gets called by the leftPanel onUpdatePlot. This updates
        the plot to the set variables from the widgets"""

        path = "C:\\Users\\TEST_DATA\\cesar_uvlidar_backscatter_la1_t30s_v1.0_20100501.nc"

        nc_data = self.NetCDF_READ(path)

        print("plotting......")
        vmin_value = 10**2
        vmax_value = 10**-5

        combo_value = nc_data['perp_beta']

        self.axes.clear()
        plot_object = self.axes.pcolormesh(combo_value.T, cmap='rainbow', 
                                           norm=colors.LogNorm(vmin=vmin_value, vmax=vmax_value))

        self.axes.set_title("Insert title here")

        if self.colorbar is None:
            self.colorbar = self.figure.colorbar(plot_object)
        else:
            self.colorbar.update_normal(plot_object)

        self.colorbar.update_normal(plot_object)

        print('canvas draw..............')
        self.canvas.draw()


        print("plotting succesfull")
###############################################################################
###############################################################################
        """BELOW HERE IS JUST DATA MANAGEMENT AND FRAME/PANEL INIT"""
###############################################################################
###############################################################################        
    def NetCDF_READ(self, path):
        in_nc = nc.Dataset(path)

        list_of_keys = in_nc.variables.keys()

        nc_data = {}    #Create an empty dictionary to store NetCDF variables

        for item in list_of_keys:
            variable_shape = in_nc.variables[item].shape
            variable_dimensions = len(variable_shape)
            if variable_dimensions > 1:
                nc_data[item] = in_nc.variables[item][...]      #Adding netCDF variables to dictonary

        return nc_data

class leftPanel(wx.Panel):

    def __init__(self, parent, mainPanel):
        wx.Panel.__init__(self, parent)

        button = wx.Button(self, -1, label="PRESS TO PLOT")
        button.Bind(wx.EVT_BUTTON, self.onButton)
        self.mainPanel = mainPanel

    def onButton(self, event):
        self.mainPanel.rightPanel.plot_Data()

class MainPanel(wx.Panel):

    def __init__(self, parent):
        """Initializing the mainPanel. This class is called by the frame."""

        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour('red')

        """Acquire the width and height of the monitor"""
        width, height = wx.GetDisplaySize()
        """Split mainpanel into two sections"""
        self.vSplitter = wx.SplitterWindow(self, size=(width,(height-100)))

        self.leftPanel = leftPanel(self.vSplitter, self) 
        self.rightPanel = rightPanel(self.vSplitter)

        self.vSplitter.SplitVertically(self.leftPanel, self.rightPanel,102)

class UV_Lidar(wx.Frame):
    """Uppermost class. This class contains everything and calls everything.
    It is the container around the mainClass, which on its turn is the container around
    the leftPanel class and the rightPanel class. This class generates the menubar, menu items,
    toolbar and toolbar items"""

    def __init__(self, parent, id):
        print("UV-lidar> Initializing GUI...")
        wx.Frame.__init__(self, parent, id, 'UV-lidar application')

        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
        self.mainPanel = MainPanel(self)

    def OnCloseWindow(self, event):
        self.Destroy()

if __name__ == '__main__':
    app = wx.App()
    frame = UV_Lidar(parent=None, id=-1)
    frame.Show()
    print("UV-lidar> ")
    print("UV-lidar> Initializing GUI OK")
    app.MainLoop()

我自己找到了解决方案:

为了blit matplotlib补丁,您必须首先将补丁添加到轴上。然后在轴上绘制补丁,然后您可以将补丁传输到画布上。

    square = matplotlib.patches.Rectangle((x1,y1), width, 
                                               height, angle=0.0, edgecolor='red',
                                               fill=False)

    self.axes.add_patch(square)
    self.axes.draw_artist(square)
    self.canvas.blit(self.axes.bbox)

如果您不想使用self.canvas.draw但仍然使用具有 useblit=True 的 matplotlib 小部件,您可以将绘图保存为背景图像:self.background = self.canvas.copy_from_bbox(self.axes.bbox)并稍后使用以下命令恢复它:self.canvas.restore_region(self.background)。这比把所有东西都画完要快得多!

当使用 matplotlib 的 RectangleSelector 小部件并使用 useblit=True 时,它​​将创建另一个背景实例变量,这会干扰您自己的背景实例变量。要解决此问题,您必须将 RectangleSelector 小部件的背景实例变量设置为等于您自己的背景实例变量。但是,这只能在 RectangleSelector 小部件不再处于活动状态后完成。否则它会将一些绘制动画保存到背景中。因此,一旦 RectangleSelector 变为非活动状态,您可以使用以下方法更新其背景:self.rectangleSelector.background = self.background

下面给出了必须编辑的代码。wx.CallLater(0, lambda: self.tbd(square))用于更新 RectangleSelector 小部件的背景实例变量只有当它已变得不活跃。

def add_Matplotlib_Widgets(self):
    """Calling these instances creates another self.background in memory. Because the widget classes
    restores their self-made background after the widget closes it interferes with the restoring of 
    our leftPanel self.background. In order to compesate for this problem, all background instances 
    should be equal to eachother. They are made equal in the update_All_Background_Instances(self) 
    function"""


    """Creating a widget that serves as the selector to draw a square on the plot"""
    self.rectangleSelector = matplotlib.widgets.RectangleSelector(self.axes, self.onselect,
                                                                  drawtype="box", useblit=True,
                                                                  button=[3], interactive=False
                                                              )

def onselect(self, eclick, erelease):
    self.tstart = time.time()
    x1, y1 = eclick.xdata, eclick.ydata
    x2, y2 = erelease.xdata, erelease.ydata

    height = y2-y1
    width = x2-x1



    square = matplotlib.patches.Rectangle((x1,y1), width, 
                                               height, angle=0.0, edgecolor='red',
                                               fill=False
                                               #blit=True gives Unknown property blit
                                               )

    """In order to keep the right background and not save any rectangle drawing animations 
    on the background, the RectangleSelector widget has to be closed first before saving 
    or restoring the background"""
    wx.CallLater(0, lambda: self.tbd(square))


def tbd(self, square):

    """leftPanel background is restored"""
    self.canvas.restore_region(self.background)

    self.axes.add_patch(square)
    self.axes.draw_artist(square)


    self.canvas.blit(self.axes.bbox)

    """leftPanel background is updated"""
    self.background = self.canvas.copy_from_bbox(self.axes.bbox)


    """Setting all backgrounds equal to the leftPanel self.background"""
    self.update_All_Background_Instances()

    print('Took '+ str(time.time()-self.tstart) + ' s')

def update_All_Background_Instances(self):
    """This function sets all of the background instance variables equal 
    to the lefPanel self.background instance variable"""
    self.rectangleSelector.background = self.background        
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 matplotlib blitting 将 matplot.patches 添加到 wxPython 中的 matplotlib 图? 的相关文章

  • Python列表内存存储[重复]

    这个问题在这里已经有答案了 据我了解 Python 列表本质上是 C 数组 它们分配特定的顺序内存块 但是 这些内存块实际上存储列表中的数据还是它们只是指向内存中存储实际数据的另一个位置 它可能取决于列表中存储的对象的大小吗 因为您可以轻松
  • python sqlite3从excel创建数据库

    我正在尝试从 Excel 电子表格创建数据库 我有下面的代码 问题是当我运行代码时 我的数据库为每列创建一个表 我想为工作簿中列出的每个电子表格创建一个表格 工作表名称为工作表 1 和工作表 2 import sqlite3 import
  • Pandas:如何根据另一个数据框的值对数据框上的列求和

    我是 Pandas 新手 我正在尝试做以下事情 我有一个名为的数据框comms包含articleID和commentScore列 等等 我有另一个名为arts带有列文章 ID 我需要创建arts一个名为文章评分 每篇文章必须具有articl
  • 如何使用 win32com.client api 访问 MS Word 的脚注

    我正在尝试使用 win32com client api 访问 MS Word 文件的脚注 我已经用谷歌搜索过 但没能找到合适的方法 我使用 python docx 来实现上述目的 但我发现当前版本的 python docx 无法访问 MS
  • 导入错误:无法导入名称线程

    这是我第一次学习Python 我继续尝试线程这篇博文 http www saltycrane com blog 2008 09 simplistic python thread example 问题是它似乎已经过时了 import time
  • 无法使用 beautifulsoup 模块 python 从 HTML 检索温度值

    我正在使用 BeautifulSoup4 来解析此 HTML 查看源代码 https weather com en IN weather today l 17 39 78 49 https weather com en IN weather
  • 尽管 ioff() 和 matplotlib.use('Agg'),Pyplot“无法连接到 X 服务器 localhost:10.0”

    我有一段代码 它被不同的函数调用 为我执行一些计算 然后将输出绘制到文件中 鉴于整个脚本可能需要一段时间才能运行更大的数据集 并且由于我可能想在给定时间分析多个数据集 所以我开始它screen然后断开连接并关闭我的腻子会话 并在第二天再检查
  • Python3如何安装.ttf字体文件?

    我想使用 python3 更精确的 Python 3 6 代码在 Windows 10 上安装 ttf 字体文件 我用谷歌搜索 但我发现的唯一的就是这个使用python在windows上安装TTF字体 https stackoverflow
  • matplotlibplot_曲面图

    matplotlib 教程提供了如何绘制球面的一个很好的示例 from mpl toolkits mplot3d import Axes3D import matplotlib pyplot as plt import numpy as n
  • 初始化 dask 分布式工作线程的状态

    我正在尝试做类似的事情 resource MyResource def fn x something dosemthing x resource return something client Client results client m
  • 如何在刻度标签和轴之间添加空间

    我已成功增加刻度标签的字体 但现在它们距离轴太近了 我想在刻度标签和轴之间添加一点呼吸空间 如果您不想全局更改间距 通过编辑 rcParams 并且想要更简洁的方法 请尝试以下操作 ax tick params axis both whic
  • 使用 Pycharm 在 Windows 下启动应用程序时出现 UnicodeDecodeError

    问题是当我尝试启动应用程序 app py 时 我收到以下错误 UnicodeDecodeError utf 8 编解码器无法解码位置 5 中的字节 0xb3 起始字节无效 整个文件app py coding utf 8 from flask
  • 如何将交互式 matplotlib 图形插入 tkinter 画布

    我正在尝试将交互式 matplotlib 图形 具有滑块 重置按钮和单选按钮的图形 放入 tkinter Canvas 中 我已成功添加非交互式图表 但当它变为交互式时找不到问题 我尝试将所有内容更改为使用 matplotlib Figur
  • 类型错误:只能使用标量值执行操作

    如果您能让我知道如何为所提供的表格绘制一些信息丰富的图表 我将不胜感激here https www iasplus com en resources ifrs topics use of ifrs 例如 我需要一个名为 国内非上市公司 非上
  • 如何使用google colab在jupyter笔记本中显示GIF?

    我正在使用 google colab 想嵌入一个 gif 有谁知道如何做到这一点 我正在使用下面的代码 它并没有在笔记本中为 gif 制作动画 我希望笔记本是交互式的 这样人们就可以看到代码的动画效果 而无需运行它 我发现很多方法在 Goo
  • Python极坐标图:绘制与角度对应的值

    我正在尝试绘制以不同角度记录的传感器数据 import pandas as pd import matplotlib pyplot as plt create dataframe each row contains an angle and
  • 在 MatPlotLib 中检索自定义破折号

    关于如何使用 matplotlib 线中设置自定义破折号有很多问题Line2D set linestyle and Line2D set dashes 但是 我似乎找不到在设置后检索破折号图案的方法 这是在主站点上设置破折号的示例 我将在下
  • 使用 Python 绘制 2D 核密度估计

    I would like to plot a 2D kernel density estimation I find the seaborn package very useful here However after searching
  • 绘制与Fig.show()内联的IPython Notebook图形?

    我正在使用 IPython Notebook 调用内联模式 pylab inline 以下代码立即在单元格处绘制一个图形 fig plt figure axes fig add axes 0 0 1 1 不过 我想在一个单元格中创建绘图 轴
  • 使用 Python 的 matplotlib 选择在屏幕上显示哪些图形以及将哪些图形保存到文件中

    我想用Python创建不同的图形matplotlib pyplot 然后 我想将其中一些保存到文件中 而另一些则应使用show 命令 然而 show 显示all创建的数字 我可以通过调用来避免这种情况close 创建我不想在屏幕上显示的绘图

随机推荐