Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

python - How to reset NavigatonToolbar "history" when re-plotting data on the same axis?

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)

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()

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You might try:

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).

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...