多个Matplotlib图共享X轴和导航工具栏

时间:2019-03-31 17:16:05

标签: python matplotlib pyqt pyqt5

我正在编写一个UI,在其中我想拥有三个独立的图,它们都共享相同的X轴和相同的导航工具栏,因此,当我缩放其中一个时,X轴将全部更改(以查看相同时间数据帧)。

enter image description here 我完成了其中的一部分:目前我有三个分离的图,我只有一个导航工具栏,如果可以在第一个图上使用缩放工具(例如),它将在三个图上同时缩放(X轴)。问题是:我只能在第一个绘图上使用Navigation Tolbar工具,其他两个不能缩放,移动等。

我当前的代码是:

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

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

0 个答案:

没有答案