pyqt 中的多个嵌入式 matplotlib 画布在鼠标悬停时更改大小 [英] Multiple embedded matplotlib canvases in pyqt change size on mouse over

查看:239
本文介绍了pyqt 中的多个嵌入式 matplotlib 画布在鼠标悬停时更改大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 PyQt GUI 的多列布局中嵌入多个 matplotlib 图.乍一看,我成功地根据需要设置了布局,但是当将鼠标移到任何画布上时,它们会改变大小并闪烁".当按下工具栏上的缩放按钮时,这会变得更加明显.

I'm trying to embed multiple matplotlib plots in a multi-column layout in a PyQt GUI. At first sight I succeed in setting up the layout as wanted but when moving the mouse over any of the canvasses they change size and 'flicker'. When pressing the zoom button on the toolbar this becomes more pronounced.

对于每个 matplotlib 画布,我都连接了一个工具栏.如果我不连接工具栏,则不会出现问题.我尝试过以多种方式排列工具栏和画布 - 使用 QGridLayout 或嵌套的 QVBoxLayouts 和 QHBoxLayouts.无论哪种方式,如果有彼此并排的图,都会出现问题.如果我将所有图放在一列中,则不会.

For each matplotlib canvas I have connected a toolbar. If I do not connect the toolbars the problem does not appear. I have tried arranging the toolbars and canvases several ways - with a QGridLayout or nested QVBoxLayouts and QHBoxLayouts. Either way the problem appears if there are plots along side each other. If I put all plots in a single column it does not.

我已经在 Windows (Anaconda 5.0.1) 的 Python 3.6 和 Linux (KDE Neon 64 位) 的 Python 3.6 和 Python 3.5.2 以及 PyQt4 和 PyQt5 (v. 5.7.1)、matplotlib 1.5 中尝试过这个.1 但结果相同.我也尝试过使用 add_axes 而不是 add_subplot.有人可以帮助我了解导致这种情况的原因或找到某种解决方法吗?我不能使用 matplotlib 子图.

I have tried this in Python 3.6 in Windows (Anaconda 5.0.1) with PyQt4 and Python 3.5.2 in Linux (KDE Neon 64 bit) and with both PyQt4 and PyQt5 (v. 5.7.1), matplotlib 1.5.1 but with the same result. I have also tried using add_axes instead of add_subplot. Can someone help me understand what is causing this or find some kind of workaround? I can not use matplotlib subplots.

from PyQt5 import QtCore, QtGui, QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()

        figure1 = Figure()
        figure2 = Figure()
        figure3 = Figure()
        figure4 = Figure()
        canvas1 = FigureCanvas(figure1)
        canvas2 = FigureCanvas(figure2)
        canvas3 = FigureCanvas(figure3)
        canvas4 = FigureCanvas(figure4)
        ax1 = figure1.add_subplot(111)
        ax2 = figure2.add_subplot(111)
        ax3 = figure3.add_subplot(111)
        ax4 = figure4.add_subplot(111)

        toolbar1 = NavigationToolbar(canvas1, self)
        toolbar2 = NavigationToolbar(canvas2, self)
        toolbar3 = NavigationToolbar(canvas3, self)
        toolbar4 = NavigationToolbar(canvas4, self)

        mainLayout = QtWidgets.QGridLayout()
        mainLayout.addWidget(toolbar1,0,0)
        mainLayout.addWidget(toolbar2,0,1)
        mainLayout.addWidget(toolbar3,2,0)
        mainLayout.addWidget(toolbar4,2,1)
        mainLayout.addWidget(canvas1,1,0)
        mainLayout.addWidget(canvas2,1,1)
        mainLayout.addWidget(canvas3,3,0)
        mainLayout.addWidget(canvas4,3,1)

        self.setLayout(mainLayout)
        self.setWindowTitle("Flow Layout")

if __name__ == '__main__':

    import sys

    app = QtWidgets.QApplication(sys.argv)
    mainWin = Window()
    mainWin.show()
    sys.exit(app.exec_())

推荐答案

我遇到了类似的问题,我通过以下方式解决了:

I had a similar issue and I solved it by:

toolbar1.setMinimumWidth(canvas1.width())
toolbar2.setMinimumWidth(canvas2.width())
toolbar3.setMinimumWidth(canvas3.width())
toolbar4.setMinimumWidth(canvas4.width())

问题是当工具栏变得比画布宽时.

The problem is when the toolbar becomes wider than the canvas.

这篇关于pyqt 中的多个嵌入式 matplotlib 画布在鼠标悬停时更改大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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