gpt4 book ai didi

Python Matplotlib : reduce render time for interactive plot

转载 作者:太空宇宙 更新时间:2023-11-03 15:44:19 25 4
gpt4 key购买 nike

我有以下代码,可以生成可以交互修改的绘图。单击/按住鼠标左键设置标记位置,按住右键并移动鼠标沿 x 方向移动绘制的数据,并使用鼠标滚轮放大/缩小。此外,调整窗口大小会调用figure.tight_layout(),以便轴的大小适应窗口大小。

# coding=utf-8
from __future__ import division

from Tkinter import *

import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from numpy import arange, sin, pi

matplotlib.use('TkAgg')


class PlotFrame(Frame):
def __init__(self, master, **ops):
Frame.__init__(self, master, **ops)

self.figure = Figure()
self.axes_main = self.figure.add_subplot(111)
for i in range(10):
t = arange(0, 300, 0.01)
s = sin(0.02 * pi * (t + 10 * i))
self.axes_main.plot(t, s)

self.plot = FigureCanvasTkAgg(self.figure, master=self)
self.plot.show()
self.plot.get_tk_widget().pack(fill=BOTH, expand=1)

self.dragging = False
self.dragging_button = None
self.mouse_pos = [0, 0]

self.marker = self.figure.axes[0].plot((0, 0), (-1, 1), 'black', linewidth=3)[0]

self.plot.mpl_connect('button_press_event', self.on_button_press)
self.plot.mpl_connect('button_release_event', self.on_button_release)
self.plot.mpl_connect('motion_notify_event', self.on_mouse_move)
self.plot.mpl_connect('scroll_event', self.on_mouse_scroll)
self.plot.mpl_connect("resize_event", self.on_resize)

def on_resize(self, _):
self.figure.tight_layout()

def axes_size(self):
pos = self.axes_main.get_position()
bbox = self.figure.get_window_extent().transformed(self.figure.dpi_scale_trans.inverted())
width, height = bbox.width * self.figure.dpi, bbox.height * self.figure.dpi
axis_size = [(pos.x1 - pos.x0) * width, (pos.y1 - pos.y0) * height]
return axis_size

def on_button_press(self, event):
# right mouse button clicked
if not self.dragging and event.button in (1, 3):
self.dragging = True
self.dragging_button = event.button
self.mouse_pos = [event.x, event.y]
# left mouse button clicked
if event.button == 1 and event.xdata is not None:
self.move_marker(event.xdata)

def on_button_release(self, event):
if self.dragging and self.dragging_button == event.button:
self.dragging = False

def on_mouse_move(self, event):
if self.dragging and self.dragging_button == 3:
dx = event.x - self.mouse_pos[0]
self.mouse_pos = [event.x, event.y]
x_min, x_max = self.figure.axes[0].get_xlim()
x_range = x_max - x_min
x_factor = x_range / self.axes_size()[0]
self.figure.axes[0].set_xlim([x_min - dx * x_factor, x_max - dx * x_factor])
self.plot.draw()
elif self.dragging and self.dragging_button == 1:
self.move_marker(event.xdata)

def on_mouse_scroll(self, event):
if event.xdata is None:
return
zoom_direction = -1 if event.button == 'up' else 1
zoom_factor = 1 + .4 * zoom_direction
x_min, x_max = self.figure.axes[0].get_xlim()
min = event.xdata + (x_min - event.xdata) * zoom_factor
max = event.xdata + (x_max - event.xdata) * zoom_factor
self.figure.axes[0].set_xlim([min, max])
self.plot.draw()

def move_marker(self, x_position):
y_min, y_max = self.figure.axes[0].get_ylim()
self.marker.set_data((x_position, x_position), (y_min, y_max))
self.plot.draw()


if __name__ == '__main__':
gui = Tk()
vf = PlotFrame(gui)
vf.pack(fill=BOTH, expand=1)
gui.mainloop()

实现工作正常,但显示大量行时渲染速度非常慢。如何才能使渲染速度更快?正如您在上面的实现中所看到的,每次发生任何变化时都会完全绘制整个图,这是不必要的。我对此的看法:

  • 调整窗口大小:绘制所有内容
  • 缩放:绘制所有内容
  • 移动标记:只需重绘标记(一行),而不是绘制所有内容
  • 沿 x 方向移动绘图:左/右移动绘图中当前显示的像素,并且仅绘制移动到可见区域的像素

在调整大小/缩放时绘制所有内容对我来说很好,但我确实需要更快地绘制后两个修改。我已经研究过 matplotlib 的动画,但据我了解,它们对我的情况没有帮助。非常感谢任何帮助,谢谢!

最佳答案

解决方案似乎是缓存像您所说的那样重绘的元素:

重绘的一个主要内容是背景:

    # cache the background
background = fig.canvas.copy_from_bbox(ax.bbox)

缓存后使用恢复区域恢复它,然后在每次需要的调用时重新绘制点/线

        # restore background
fig.canvas.restore_region(background)

# redraw just the points
ax.draw_artist(points)

# fill in the axes rectangle
fig.canvas.blit(ax.bbox)

关于Python Matplotlib : reduce render time for interactive plot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41895597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com