toolbar - Matplotlib : use a different NavigationToolBar for a specific axe - Stack Overflow

In the program bellow, I have a Matpltolib window (plt.show()). I have 2 axes displayed together (ax1

In the program bellow, I have a Matpltolib window (plt.show()). I have 2 axes displayed together (ax1 / ax2) controlled bu the default toolbar. When I click on "Visualisation", ax1 / ax2 are hidden, to create a new axe 'ax3' in bigger. It works, but what I am looking for is changed the NavigationToolBar to have a new one specific to ax3, created at the same time (and hide the toolbar controlling ax1 / ax2). When I click back on "Visualisation", It removes 'ax3'. I want that the NavigationToolBar specific to ax3 be removed to, and that when ax1 / ax2 are visibles and not hidden anymore, that their NavigationToolBar specific to them reappears

Here the code I have :

import matplotlib.pyplot as plt
from matplotlib.widgets import Button
from matplotlib.backend_bases import NavigationToolbar2

class CustomToolbar(NavigationToolbar2):
    def __init__(self, canvas, toolitems, parent=None):
        self.toolitems = toolitems
        NavigationToolbar2.__init__(self, canvas)

def gestion_ax3(event):
    global ax3, toolbar_ax3

    if not ax3:
        ax1.set_visible(False), ax2.set_visible(False)
        ax3 = figure.add_subplot(111)
        ax3.plot([1, 2, 3, 4, 5], [11, 14, 16, 17, 20], color="green")
        toolbar_ax3 = CustomToolbar(figure.canvas, NavigationToolbar2.toolitems)
        figure.canvas.manager.toolbar = toolbar_ax3
    else:
        ax3.remove()
        ax3 = None
        toolbar_ax3 = None
        ax1.set_visible(True), ax2.set_visible(True)
        figure.canvas.manager.toolbar = toolbar

    figure.canvas.draw()

figure, (ax1, ax2) = plt.subplots(2, 1)
ax1.plot([1, 2, 3, 4], [10, 15, 13, 18], color="blue")
ax2.plot([1, 2, 3, 4], [12, 16, 14, 19], color="red")
ax3 = None

toolbar = CustomToolbar(figure.canvas, NavigationToolbar2.toolitems)
figure.canvas.manager.toolbar = toolbar

button_ax = plt.axes([0.9, 0.01, 0.1, 0.075])
button = Button(button_ax, 'Visualisation', color='purple', hovercolor='yellow')
button.on_clicked(gestion_ax3)

plt.show()

The problem I meet here is that the NavigationToolBar never changes, event when ax3 is created, I have still the possibility to control ax1 / ax2 zoom (when I click on the arrows 'back' and 'following' view)

Thank you for the help

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745127423a4612775.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信