How to reset NavigatonToolbar “history” when re-plotting data on the same axis?

前端 未结 2 1392
半阙折子戏
半阙折子戏 2020-12-21 08:59

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

import matplotlib
matplotlib.use(\'WXAgg         


        
相关标签:
2条回答
  • 2020-12-21 09:27

    The home button actually brings the first element in the navigation stack to the top.

    If you want to manually control the limits that are set by the Home button, you can modify the first element in the navigation stack in-place:

    if len(self.fig.canvas.toolbar._nav_stack._elements) > 0:
        # Get the first key in the navigation stack
        key = list(self.fig.canvas.toolbar._nav_stack._elements[0].keys())[0]
        # Construct a new tuple for replacement
        alist = []
        for x in self.fig.canvas.toolbar._nav_stack._elements[0][key]:
            alist.append(x)
        alist[0] = (new_xmin, new_xmax, new_ymin, new_ymax)
        # Replace in the stack
        self.fig.canvas.toolbar._nav_stack._elements[0][key] = tuple(alist)
    

    When you hit the Home button, the displayed range will now be [new_xmin, new_xmax, new_ymin, new_ymax].

    0 讨论(0)
  • 2020-12-21 09:41

    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.

    0 讨论(0)
提交回复
热议问题