更改色彩映射时,颜色栏在图像动画期间消失

时间:2017-10-26 13:17:26

标签: matplotlib pyqt colorbar colormap

我在图中创建了一个用于显示imshow图像的子图,我添加了一个颜色条。没有动画,我可以更改颜色图,更改ComboBox的值,并正确更新颜色条。

但是如果我添加动画,每次更改色彩图时颜色条都会消失。我必须单击另一个窗口(其他软件等)或调整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)
        self.Widget_choixPalette_ComboBox.currentIndexChanged.connect(self.changementPalette)

    def changementPalette(self, onglet):
        self.image.set_cmap('binary')
        self.canvas.draw()

    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

  • 操作系统:Windows 7 Pro

  • Matplotlib版本:2.1.0

  • Matplotlib后端:Qt5Agg

  • Python版本:3.6

1 个答案:

答案 0 :(得分:1)

a] blit=False

在动画中使用blit=False。否则只有图像本身会被更新,并且整个画布的重绘被推迟到某些事件,这使得需要重新绘制,例如,调整大小事件。

b]暂停动画

如果您无法使用blit=False进行约会,您可以暂停动画,更改颜色图,绘制画布,然后继续动画。

def changementPalette(self, onglet):
    self.anim.event_source.stop()
    if onglet==0:
        self.image.set_cmap('viridis')
    else:
        self.image.set_cmap('binary')
    self.canvas.draw_idle()
    self.anim.event_source.start()