How to embed a plot directly into a Window (python; QT;)
Question: I wanna embed a Matplotlib plot directly into a window, QMainWindow. It should
be part of my program with a more complex GUI. ;)

The only way I found was to add the figure as widget into a QTabWidget. See
sample code below. I lost the link to the webpage what inspired me.

Is there any way to embed the figure directly into the windows like other
elements (buttons, textfield, textarea, ...)?

    
    
    import sys
    from PyQt4.QtGui import QApplication, QMainWindow, QDockWidget, QVBoxLayout,QTabWidget, QWidget
    from matplotlib import pyplot
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
    
    a = QApplication(sys.argv)
    w = QMainWindow()
    t = QTabWidget(w)
    Tab1 = QWidget()
    t.addTab(Tab1, '1st Plot')
    t.resize(1280, 300)
    
    x = [1, 2, 3]
    Fig1 = pyplot.Figure();
    Plot =  Fig1.add_subplot(111);
    Plot.plot(x)
    Plot.grid();
    
    layout = QVBoxLayout();
    layout.addWidget(FigureCanvasQTAgg(Fig1));
    Tab1.setLayout(layout);
    
    w.showMaximized()
    sys.exit(a.exec_())
    

Thank you very much.


Answer: `FigureCanvasQtAgg` is just a `QWidget` like any of the other controls you
mentioned. The main difference being it doesn't allow you to pass a parent in
the constructor like when you write

    
    
    t = QTabWidget(w)
    

You can achieve the same with `FigureCanvasQtAgg` by calling
[`setParent`](http://doc.qt.io/qt-4.8/qwidget.html#setParent)

    
    
    canvas = FigureCanvasQtAgg(Fig1)
    canvas.setParent(w)
    

You can also use `QMainWindow`'s
[`setCentralWidget`](http://doc.qt.io/qt-4.8/qmainwindow.html#setCentralWidget)
method to add the matplotlib `FigureCanvas` directly to your main window.
However, if you want a more complex gui with other controls I don't see any
real problems with your current approach.

Lastly, you shouldn't really be using `pyplot` when embedding matplotlib.
Stick with the object oriented API. Take a look at [this
example](http://matplotlib.org/1.4.3/examples/user_interfaces/embedding_in_qt4.html).

