title: Multiple matplotlib plots sharing X Axis and Navigation Toolbar
body: <p>I'm coding an UI where I want to have three separated plots, all sharing the same X Axis and the same Navigation Toolbar, so when I zoom one of them the X axis changes for all of them (to see the same time dataframe).</p>
  
  <p><a href="https://i.stack.imgur.com/z5ryX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z5ryX.png" alt="enter image description here"></a>
  I accomplished part of it: currently I have three separated plots, I have only one Navigation Toolbar and if I can use the zoom tool (for example) on the first plot and it will zoom (the X axis) simultaneously on the three plots. The problem is: I can only use the Navigation Tolbar tools on the first plot, the other two can't be zoomed, moved, etc.</p>
  
  <p>My currently code is:</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  from PyQt5.QtWidgets import (QMainWindow, QApplication, QFileDialog)
  
  from CvPyGui import FilterCvQtContainer
  from CvPyGui.ui import gui3
  
  from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
  from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  
  Ui_MainWindow = gui3.Ui_MainWindow
  
  class MyApp(QMainWindow, Ui_MainWindow):
  
      filter_count = 0
      plots = []
  
      def __init__(self):
          super().__init__()
          QMainWindow.__init__(self)
          Ui_MainWindow.__init__(self)
          self.setupUi(self)
          self.initUI()
  
          self.plots.append(self.plot1)
          self.plots.append(self.plot2)
          self.plots.append(self.plot3)
  
      def initUI(self):
  
          self.toolbar = NavigationToolbar(self.plot1.canvas, self)
          self.addToolBar(self.toolbar)
          self.actionLoad_data.triggered.connect(self.LoadDataFile)
  
      def LoadDataFile(self):
          # Function for selecting the original image
          filter = "Data file (*.csv, *.txt)"
          file_path, _ = QFileDialog.getOpenFileName(
              self, 'Open image', 'Desktop', filter)
          self.path = file_path
          self.original_df = pd.read_csv(self.path, sep='.', delimiter='\t', na_values=['NaN', 'OutOfRange'], skiprows=(0,1,2,3,4,5,7))
          self.original_df['Tempo zerado'] = self.original_df['Tempo'] - self.original_df['Tempo'][0]
          self.original_df = self.original_df.set_index('Tempo zerado')
          for plot in self.plots:
              plot.comboLoadVariable.addItems(list(self.original_df))
              plot.connectButtons()
  </code></pre>
  
  <p>and</p>
  
  <pre><code>import pandas as pd
  from PyQt5.QtCore import Qt
  from PyQt5.QtWidgets import (QWidget, QLabel, QHBoxLayout,
                               QVBoxLayout, QPushButton, QSlider,
                               QComboBox)
  
  from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
  from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  from matplotlib.figure import Figure
  
  from .FilterCvQtContainer import Filter
  
  import random
  
  class SinglePlotContainer(QWidget):
  
      num_plots = 0
  
      def __init__(self, parent=None):
          super().__init__()
  
          self.num_plots += 1
  
          self.variable_df = pd.DataFrame()
  
          self.figure = Figure() # don't use matplotlib.pyplot at all!
          self.canvas = FigureCanvas(self.figure)
  
          self.hLayout = QHBoxLayout(self)
          self.dataConfigColumn = QVBoxLayout()
          self.filtersColumn = QVBoxLayout()
  
          self.hLayout.addLayout(self.dataConfigColumn)
          self.hLayout.addWidget(self.canvas)
          self.hLayout.addLayout(self.filtersColumn)
  
          self.comboLoadVariable = QComboBox()
          self.dataConfigColumn.addWidget(self.comboLoadVariable)
  
          self.filter1 = Filter('Moving Average', 3, 30, 5, 1)
          self.filtersColumn.addWidget(self.filter1)
  
          # drawEvent = self.figure.canvas.mpl_connect('draw', self.updatePlot)
  
          self.plotRandom()
  
      def connectButtons(self):
          self.comboLoadVariable.activated[str].connect(self.loadVariable)
  
      def loadVariable(self, variable):
          self.variable_df = self.parent().parent().original_df[variable]
          self.plot()
  
      def plot(self):
          if self.num_plots != 0:
              self.axes = self.figure.add_subplot(111, sharex=self.parent().parent().plots[0].axes)
          else:
              self.axes = self.figure.add_subplot(111)
          self.axes.clear()
          self.axes.plot(self.variable_df, '-')
          self.canvas.draw()
  
      def updatePlot(self):
          ymax,ymin = self.axes.get_ylim()
          self.axes.clear()
          self.axes.set_ylim(ymax,ymin)
          self.axes.plot(self.variable_df, '-')
          self.canvas.draw()
  
      def plotRandom(self):
          ''' plot some random stuff '''
          data = [random.random() for i in range(10)]
          self.axes = self.figure.add_subplot(111)
          self.axes.clear()
          self.axes.plot(data, '-')
          self.canvas.draw()
  </code></pre>