3

I am currently making a GUI where I want to have some sliders, some buttons and some plots. I am struggling to position the Matplotlib plot where I want it. The plot is in a QVBoxLayout and I have tried putting this inside a Widget without success. I want to be able to choose the position and size of the plot

Here is what I have now:

Current plot

And here is what I want, where I can define position and size so I have space for the other controls:

What I am looking for, where I can define position and size

Here is basic code:

import sys
import numpy as np
from PyQt4 import QtGui, QtCore
# import inspect
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import matplotlib.pyplot as plt

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        #PLOTTING
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.canvas, self)
        self.plot()

        # set the layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        self.setLayout(layout)

        #WINDOW PROPERTIES
        self.resize(800,800)
        self.setWindowTitle('Waveguide Array')
        self.setWindowIcon(QtGui.QIcon('flavicon.png'))
        self.show()

    def plot(self):
        ''' plot some random stuff '''
        data = [np.random.random() for i in range(10)]
        ax = self.figure.add_subplot(111)
        ax.hold(False)
        ax.plot(data, '*-')
        self.canvas.draw()

    # def update_plot(self):

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Thanks for the help!

1
  • Why not use a QGridLayout as the layout, add the canvas to a widget, and add the widget to the GridLayout? Just remember to set the widget either to have a maximumSize() that's roughly a quarter of your view size, or add other widgets and make sure all scale proportionally. Commented Apr 27, 2016 at 0:33

1 Answer 1

2

Here is a solution. Make a plot widget inside of the main widget, seems to work and can control position with setGeometry.

self.main_widget = QtGui.QWidget(self)
self.plot_widget = QtGui.QWidget(self.main_widget)
self.plot_widget.setGeometry(250,180,500,600)
self.figure = plt.figure()
self.plotting = FigureCanvas(self.figure)
self.plot()
plot_box = QtGui.QVBoxLayout()
plot_box.addWidget(self.plotting)
self.plot_widget.setLayout(plot_box)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.