【问题标题】:Matplotlib colorbar invisible with animationMatplotlib 颜色栏在动画中不可见
【发布时间】:2017-10-26 07:18:17
【问题描述】:

当我在图中创建子图以使用 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_())

带动画:

在评论中设置动画行数:

Matplotlib 版本

  • 操作系统:Windows 7 Pro

  • Matplotlib 版本:2.0.2

  • Matplotlib 后端:Qt5Agg

  • Python 版本:3.6

【问题讨论】:

  • 您可以更好地解释错误,显示您得到的和想要得到的图像。
  • 我添加了两个图像,当我有动画刷新图像时,首先显示颜色条不可见,当我没有动画时,颜色条可见。
  • 你有什么版本的matplotlib?当前版本是'2.1.0',你从哪里得到版本4.0.4? :P
  • 是2.0.2 ^.^我的错
  • 我已经在 2.1.0 版本中尝试过,它可以正常工作,我建议您更新它们,也许像您这样的旧版本有这个问题。

标签: matplotlib pyqt colorbar


【解决方案1】:

问题出在 matplotlib 的版本上。 用 2.0.2 就不行了,但是用最后一个(2.1.0)就没有问题了。

【讨论】:

    猜你喜欢
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    相关资源
    最近更新 更多