Matplotlib colorbar对动画

时间:2017-10-26 07:18:17

标签: matplotlib pyqt colorbar

当我在图中创建一个用于显示imshow图像的子图时,我添加了一个颜色条。关于这个子图没有动画也没关系。 但是,如果我添加动画,颜色栏不会出现在GUI的启动时(或每次我更改颜色条的cmap)。我必须单击另一个窗口(其他软件等)或调整GUI大小以查看颜色栏。

以下是了解问题的示例:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib import animation

class FenetrePrincipale(QWidget):
    def __init__(self, parent=None):
        super(FenetrePrincipale, self).__init__(parent)
        self.setupUi(self)

    # Fonction de configuration de la classe
    def setupUi(self, Form):
        self.Form = Form

        Form.setMinimumSize(1220, 850)

        self.creation_GUI()
        self.creation_figure()
        self.creation_layout()

        self.tabWidget.setCurrentIndex(0)
        self.Bouton_quitter.clicked.connect(self.close)
        self.anim = animation.FuncAnimation(self.figure, self.animate, interval=10, blit=True)

    def animate(self, i):
        # a = self.thread_1.img
        self.image.set_array(self.imageInit)
        return [self.image]

    def resizeEvent(self, QResizeEvent):
        self.tabWidget.setMinimumSize(QSize(self.width() - 20, self.height() - 60))

    def creation_GUI(self):
        self.tabWidget = QTabWidget()
        self.tab1 = QWidget()
        self.tabWidget.addTab(self.tab1, "  Tab1  ")

        self.Widget_choixPalette_Label = QLabel(self.tab1)
        self.Widget_choixPalette_Label.setText("Text1")
        self.Widget_choixPalette_ComboBox = QComboBox(self.tab1)
        self.Widget_choixPalette_ComboBox.addItem("Try1")
        self.Widget_choixPalette_ComboBox.addItem("Try2")

        self.Bouton_quitter = QPushButton(self.tab1)
        self.Bouton_quitter.setText("Quit")

    def creation_layout(self):
        LayoutForm = QGridLayout(self)
        LayoutForm.addWidget(self.tabWidget, 0, 0, 1, 1)

        LayoutTab1 = QGridLayout(self.tab1)

        LayoutTab1.addWidget(self.Widget_choixPalette_Label, 0, 1, 1, 1)
        LayoutTab1.addWidget(self.Widget_choixPalette_ComboBox, 1, 1, 1, 1)
        self.Widget_choixPalette_ComboBox.setMinimumWidth(200)

        LayoutTab1.addWidget(self.canvas, 2, 0, 1, 3)
        LayoutTab1.addWidget(self.Bouton_quitter, 2, 3, 1, 1, Qt.AlignRight | Qt.AlignBottom)

        LayoutTab1.setRowStretch(2, 1)
        LayoutTab1.setColumnStretch(0, 1)
        LayoutTab1.setColumnStretch(2, 1)

    def creation_figure(self):
        # Create figure (transparent background)
        self.figure = plt.figure()
        # self.figure.patch.set_facecolor('None')
        self.canvas = FigureCanvas(self.figure)
        self.canvas.setStyleSheet("background-color:transparent;")

        # Adding one subplot for image
        self.axe0 = self.figure.add_subplot(111)
        self.axe0.get_xaxis().set_visible(False)
        self.axe0.get_yaxis().set_visible(False)

        # Data for init image
        self.imageInit = [[255] * 320 for i in range(240)]
        self.imageInit[0][0] = 0

        # Init image and add colorbar
        self.image = self.axe0.imshow(self.imageInit, interpolation='none')
        divider = make_axes_locatable(self.axe0)
        cax = divider.new_vertical(size="5%", pad=0.05, pack_start=True)
        self.colorbar = self.figure.add_axes(cax)
        self.figure.colorbar(self.image, cax=cax, orientation='horizontal')

        plt.subplots_adjust(left=0, bottom=0.05, right=1, top=1, wspace=0, hspace=0)

        self.canvas.draw()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    # QApplication.setStyle(QStyleFactory.create("plastique"))
    form = FenetrePrincipale()
    form.show()
    sys.exit(app.exec_())

动画: enter image description here

在评论中设置动画线: enter image description here

Matplotlib版本

  • 操作系统:Windows 7 Pro

  • Matplotlib版本:2.0.2

  • Matplotlib后端:Qt5Agg

  • Python版本:3.6

1 个答案:

答案 0 :(得分:0)

问题来自matplotlib的版本。 使用2.0.2它不起作用,但是使用最后一个(2.1.0),没有更多问题。