MatplotlibをQtの画面で使う
Qtで作るGUI画面に、Matplotlibで描くグラフを埋め込む。
その方法の一例を以下に示す。ここではQt Designerは使わず、ただ画面に表示させるのみ。
import sys from PyQt4 import QtGui import numpy as np from matplotlib.figure import Figure from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas class MyForm(FigureCanvas): def __init__(self): self.fig = Figure() self.lines = self.fig.add_subplot(111) self.x = np.arange(0.0, 3.0, 0.01) self.y1 = np.cos(2*np.pi*self.x) self.y2 = np.sin(2*np.pi*self.x) self.lines.plot(self.x, self.y1, color="red", label="cos(x)") self.lines.plot(self.x, self.y2, color="green", label="sin(x)") self.lines.legend() self.lines.set_xlabel("x") self.lines.set_ylabel("f(x)") FigureCanvas.__init__(self, self.fig) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) myapp = MyForm() myapp.show() sys.exit(app.exec_())
Matplotlib for Python Developers: Build Remarkable Publication Quality Plots the Easy Way | |
Sandro Tosi Packt Publishing 2009-10-30 売り上げランキング : 86352 Amazonで詳しく見る by G-Tools |