如何重置 NavigatonToolbar“历史"在同一轴上重新绘制数据时? [英] How to reset NavigatonToolbar "history" when re-plotting data on the same axis?

查看:13
本文介绍了如何重置 NavigatonToolbar“历史"在同一轴上重新绘制数据时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 wxPython 应用程序,它使用 matplotlib 重复绘制数据.代码如下所示:

I have a wxPython application that uses matplotlib for plotting data repeatedly. The code looks something like this:

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas, 
NavigationToolbar2WxAgg as NavigationToolbar

self.fig = Figure((4,5), dpi = 100, facecolor = "white")
self.canvas = FigCanvas(self.panel, -1, self.fig)
self.toolbar = NavigationToolbar(self.canvas)
self.axes = self.fig.add_subplot(111)

每次我想绘制一些东西时,我只需设置 x 和 y 并执行:

Everytime I want to plot something, I just set x and y and do:

self.axes.plot(x,y, color = self.colours[i], label = text)
self.canvas.draw()

如您所见,我有一个 NavigationToolBar 绑定到画布.当我想绘制一个新图形时,我调用:

As you can see, I have a NavigationToolBar bound to the canvas. When I want to plot a new graph, I call:

self.axes.clear()
self.axes.plot(x,y, color = self.colours[i], label = text)
self.canvas.draw()

问题来了:如果我在可视化绘图时使用工具栏的工具(缩放、步长、平移等),当我稍后绘制新图形时,工具栏的历史"不会重置.如果我尝试在这个新图表中使用工具栏,工具栏将使用的视图(当我单击主页"或任何步骤"时)将是旧图的视图.

Here comes the problem: If I use the toolbar's tools (zoom, steps, pan, etc) when I'm visualizing a plot, the "historic" of the toolbar won't reset when I plot a new graph later. If I try to use the toolbar in this new graph, the views the toolbar will use (when I click "home" or any "step") will be the views of the old plot.

我对 matplotlib 有点陌生,我可能做错了什么.谁能帮我解决这个问题?提前致谢,如有任何语法错误,请见谅,英语不是我的母语.

I'm kinda new to matplotlib and I'm probably doing something wrong. Can anyone help me out with this? Thanks in advance, and sorry for any grammar mistakes, English is not my mother language.

推荐答案

你可以试试:

self.toolbar._views.clear()
self.toolbar._positions.clear()
self.toolbar._update_view() # maybe you don't need this

我应该强调这是没有记录的,你正在进入并戳动图书馆的内部,所以不能保证如果它现在有效,将来也会有效(或者你会收到警告说它将停止工作).

I should stress this is un-documented and you are reaching in and poking at the innards of the library, so there is no guarantee that if it works now, it will work in the future (or you will get warning that it will stop working).

查看 matplotlib/backend_bases.py 中的代码,了解 NavigationToolbar2(Wx 版本的父类)的工作原理.

Have a look at the code in matplotlib/backend_bases.py for how the NavigationToolbar2 (the parent class of the Wx version) works.

这篇关于如何重置 NavigatonToolbar“历史"在同一轴上重新绘制数据时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆