2017-10-26 1 views
0

Wenn ich ein Subplot in einer Figur zur Anzeige eines Bildes mit Imshow erstelle, füge ich eine Farbleiste hinzu. Es ist in Ordnung, keine Animation über diesen Teilprozess. Aber wenn ich eine Animation hinzufüge, erscheint die Farbleiste nicht beim Start der GUI (oder jedes Mal, wenn ich die cmap der Farbleiste ändere). Ich muss auf ein anderes Fenster klicken (andere Software, etc) oder die Größe der GUI ändern, um die Farbleiste zu sehen. HierMatplotlib Farbbalken unsichtbar mit Animation

ist ein Beispiel, das Problem zu verstehen:

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

Mit Animation: enter image description here

einstellen Linien Animation in Kommentar: enter image description here

Matplotlib Version

  • Betriebssystem: Windows 7 Pro

  • Matplotlib Version: 2.0.2

  • Matplotlib Backend: Qt5Agg

  • Python-Version: 3,6

+0

Sie könnten den Fehler besser erklären, zeigen Sie ein Bild von dem, was Sie bekommen und was Sie bekommen möchten. – eyllanesc

+0

Ich habe zwei Bilder hinzugefügt, die die unsichtbare Farbleiste zuerst zeigen, wenn ich eine Animation habe, um das Bild zu aktualisieren, und die Farbleiste sichtbar, wenn ich keine Animation habe. –

+0

Welche Version von Matplotlib hast du? die aktuelle Version ist '2.1.0', wo hast du Version 4.0.4 bekommen? : P – eyllanesc

Antwort

0

Das Problem war, sich von der Version von Matplotlib. Mit 2.0.2 funktioniert es nicht, aber mit dem letzten (2.1.0) gibt es kein Problem mehr.

Verwandte Themen