更改颜色贴图时,在图像动画过程中,颜色条消失 [英] Colorbar disappear during animation of image when changing colormap

查看:60
本文介绍了更改颜色贴图时,在图像动画过程中,颜色条消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在图形中创建了一个子图,用于显示带有imshow的图像,并添加了一个色条.在没有动画的情况下,我可以更改颜色图,从而更改ComboBox的值,并且可以正确更新颜色栏.

I created a subplot in a figure for displaying an image with imshow, and I add a colorbar. With no animation, I can change the colormap changing the value of the ComboBox and the colorbar is updated correctly.

但是,如果添加动画,则每次更改颜色贴图时,颜色栏都会消失.我必须单击另一个窗口(其他软件,等等)或调整GUI的大小才能再次看到颜色栏.

But if I add an animation, the colorbar disappear each time I change the colormap. I have to click on another window (other software, etc) or resize the GUI to see the colorbar again.

以下是了解该问题的示例:

Here is an example to understand the problem :

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

当我更改颜色图时,请在组合框中选择任何选项:

When I change of colormap selecting any choice in combobox :

我等着看:

  • 操作系统:Windows 7 Pro

  • Operating system: Windows 7 Pro

Matplotlib版本:2.1.0

Matplotlib version: 2.1.0

Matplotlib后端:Qt5Agg

Matplotlib backend: Qt5Agg

Python版本:3.6

Python version: 3.6

推荐答案

a] blit = False

在动画中使用 blit = False .否则,仅图像本身将被更新,并且整个画布的重绘会导致某些需要进行此重绘的事件,例如:调整大小的事件.

a] blit=False

Use blit=False in the animation. Otherwise only the image itself will be updated and the redrawing of the complete canvas is posponed to some event that makes this redrawing necessary, e.g. a resize event.

如果无法使用 blit = False 进行设置,则可以暂停动画,更改颜色图,绘制画布,然后继续动画.

In case you cannot affort using blit=False, you can pause the animation, change the colormap, draw the canvas, then continue the animation.

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

这篇关于更改颜色贴图时,在图像动画过程中,颜色条消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆