【发布时间】:2016-08-21 01:30:19
【问题描述】:
我目前正在制作一个 GUI,我想要在其中包含一些滑块、一些按钮和一些图。我正在努力将 Matplotlib 图定位在我想要的位置。该图位于 QVBoxLayout 中,我尝试将其放入 Widget 中但没有成功。我希望能够选择绘图的位置和大小
这是我现在拥有的:
这就是我想要的,我可以在其中定义位置和大小,以便为其他控件留出空间:
What I am looking for, where I can define position and size
这是基本代码:
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()
感谢您的帮助!
【问题讨论】:
-
为什么不使用QGridLayout作为布局,将canvas添加到widget,将widget添加到GridLayout呢?只需记住将小部件设置为具有大约为视图大小四分之一的 maximumSize(),或者添加其他小部件并确保所有小部件按比例缩放。
标签: python python-2.7 matplotlib pyqt pyqt4