当前位置: 动力学知识库 > 问答 > 编程问答 >

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)
分享给朋友:
您可能感兴趣的文章:
随机阅读: