# -*- coding: utf-8 -*-
"""
Demonstrates use of PlotWidget class. This is little more than a 
GraphicsView with a PlotItem placed in its center.
"""

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

pg.setConfigOption('background', (255, 255, 200))
pg.setConfigOption('foreground', 'k')

# QtGui.QApplication.setGraphicsSystem('raster')
app = QtGui.QApplication([])
mw = QtGui.QMainWindow()
mw.setWindowTitle('pyqtgraph example: PlotWidget')
mw.resize(800, 800)
cw = QtGui.QWidget()
mw.setCentralWidget(cw)
l = QtGui.QVBoxLayout()
cw.setLayout(l)

pw = pg.PlotWidget(name='Plot1')  # giving the plots names allows us to link their axes together
p1 = pw.plotItem.plot([1, 2, 5], pen='r')
pw.plotItem.showGrid(y=True)


def changeBackground():
    global pw
    pw.plotItem.vb.setBackgroundColor(np.random.randint(low=0, high=255, size=(3,)))

b = QtGui.QPushButton("change background color")
b.clicked.connect(changeBackground)

l.addWidget(b)
l.addWidget(pw)
mw.show()





# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()
