Show several plots in a scrollable widget with PyQt and matplotlib

倖福魔咒の 提交于 2019-12-03 21:56:21

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

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!