简体   繁体   中英

Python - PyQt Matplotlib plot positioning

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM