在带有PyQt和matplotlib的可滚动小部件中显示多个图 [英] Show several plots in a scrollable widget with PyQt and matplotlib
问题描述
因为我没有得到这个问题的答案我尝试用PyQt解决它. 显然,当涉及到QScrollArea时,这并不容易...
Since I didn't get an answer for this question I tried solving it with PyQt. Apparently, it's not that easy, when QScrollArea is involved...
我写了一个小测试,基本上可以满足我的需求,但是并没有像我期望的那样显示滚动区域和其中的绘图:
I wrote a small test that basically does what I'm looking for, but it's not showing the scroll area and the plots inside it as I expected:
from PyQt4 import QtCore, QtGui
import os,sys
#import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.figure import Figure
qapp = QtGui.QApplication(sys.argv)
qwidget = QtGui.QWidget()
qwidget.setGeometry(QtCore.QRect(0, 0, 500, 500))
qlayout = QtGui.QHBoxLayout(qwidget)
qwidget.setLayout(qlayout)
qscroll = QtGui.QScrollArea(qwidget)
qscroll.setGeometry(QtCore.QRect(0, 0, 500, 500))
qscroll.setFrameStyle(QtGui.QFrame.NoFrame)
qlayout.addWidget(qscroll)
qscrollContents = QtGui.QWidget()
qscrollLayout = QtGui.QVBoxLayout(qscrollContents)
qscrollLayout.setGeometry(QtCore.QRect(0, 0, 1000, 1000))
qscroll.setWidget(qscrollContents)
qscroll.setWidgetResizable(True)
for i in xrange(5):
qfigWidget = QtGui.QWidget(qscrollContents)
fig = Figure((5.0, 4.0), dpi=100)
canvas = FigureCanvas(fig)
canvas.setParent(qfigWidget)
toolbar = NavigationToolbar(canvas, qfigWidget)
axes = fig.add_subplot(111)
axes.plot([1,2,3,4])
qscrollLayout.addWidget(qfigWidget)
qscrollContents.setLayout(qscrollLayout)
qwidget.show()
exit(qapp.exec_())
谁能解释为什么它不起作用?
Can anyone explain why it's not working?
推荐答案
您正在为每个图创建一个QWidget
.但是您不能通过布局将canvas
或toolbar
放入其中,因此它们无法与QWidget
传达尺寸信息.默认情况下,QWidget
没有minimumSize
,并且QScrollArea
内的小部件/布局可以使其尽可能小,以适应可用空间(QScrollArea
的大小).
You are creating a QWidget
for each plot. But you don't put your canvas
or toolbar
in it via a layout, so they can't communicate the size information with the QWidget
. By default, a QWidget
has no minimumSize
and the widget/layout inside the QScrollArea
can make them as small as it wants in order to fit the available space (which is the size of QScrollArea
).
通过布局添加图会有所帮助,但是我发现FigureCanvas
小部件也没有任何最小尺寸,因此可以缩小.要快速修复,可以设置minimumSize
.具有这些修复程序的循环部分应如下所示:
Adding plots via layout helps, but I find that the FigureCanvas
widget also doesn't have any minimum size so it can shrink. For a quick fix, you can set a minimumSize
. The loop part with these fixes should look like this:
for i in xrange(5):
qfigWidget = QtGui.QWidget(qscrollContents)
fig = Figure((5.0, 4.0), dpi=100)
canvas = FigureCanvas(fig)
canvas.setParent(qfigWidget)
toolbar = NavigationToolbar(canvas, qfigWidget)
axes = fig.add_subplot(111)
axes.plot([1,2,3,4])
# place plot components in a layout
plotLayout = QtGui.QVBoxLayout()
plotLayout.addWidget(canvas)
plotLayout.addWidget(toolbar)
qfigWidget.setLayout(plotLayout)
# prevent the canvas to shrink beyond a point
# original size looks like a good minimum size
canvas.setMinimumSize(canvas.size())
qscrollLayout.addWidget(qfigWidget)
这篇关于在带有PyQt和matplotlib的可滚动小部件中显示多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!