Monday, July 16, 2012

Chaco Plots Using PySide Qt Bindings with Widgets in a QVBoxLayout


This small example builds on the example found at:
http://caethan.wordpress.com/2011/09/05/chaco-plots-in-pyside/

This example can be extended to easily include multiple plots and the adjustment of
plot parameters using spin boxes.

#!/usr/bin/python

import sys
from numpy import *
from PySide.QtCore import *
from PySide.QtGui import *

app = QApplication(sys.argv)

from traits.etsconfig.etsconfig import ETSConfig
ETSConfig.toolkit = "qt4"
from enable.api import Window
from chaco.api import ArrayPlotData, Plot

class Viewer(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.mainWidget = QWidget(self) # dummy widget to contain layout manager
        self.plotview = Plotter(self)
        self.setCentralWidget(self.mainWidget)

        self.amplitudeLabel = QLabel("Amplitude: ")
        self.amplitudeSpinBox = QDoubleSpinBox()
        self.amplitudeSpinBox.setRange(-1000000.00, 1000000.00)
        self.amplitudeSpinBox.setValue(1.00)
 
        self.plotButton = QPushButton("Plot")
        self.plotButton.clicked.connect(self.update) 
                
        self.statusBar().showMessage('Ready to rock!')
        self.setWindowTitle('sin(x)')

        x = arange(100)
        y = zeros(100) 
        self.plotview.update_data(x, y)

        layout = QVBoxLayout(self.mainWidget)
        layout.addWidget(self.amplitudeLabel)
        layout.addWidget(self.amplitudeSpinBox)
        layout.addWidget(self.plotButton)
        layout.addWidget(self.plotview.widget)
        self.setLayout(layout)

     
    def update(self):
        print "Ok"
        print self.amplitudeSpinBox.value()
        x = arange(200)
        y = self.amplitudeSpinBox.value() * sin(x)
        self.plotview.update_data(x, y)
    

class Plotter():
    def __init__(self, parent):
        self.plotdata = ArrayPlotData(x=array([]),  y=array([]))
        self.window = self.create_plot(parent)
        self.widget = self.window.control
        

    def update_data(self, x, y):
        self.plotdata.set_data("x", x)
        self.plotdata.set_data("y", y)

    def create_plot(self, parent):
        plot = Plot(self.plotdata, padding=50, border_visible=True)
        plot.plot(("x", "y"), name="data plot", color="green")
        return Window(parent, -1, component=plot)


if __name__ == "__main__":
    plot = Viewer()
    plot.resize(600, 400)
    plot.show()
    sys.exit(app.exec_())


13 comments:

  1. They are constantly proactive and design agencies SF invested in the success of the project.

    ReplyDelete
  2. They were quick to incorporate feedback and were open with their progress throughout the project
    top creative agencies

    ReplyDelete
  3. They did a good job of listening to what we were looking to build but also pushing us beyond what we had thought of
    app designers

    ReplyDelete
  4. The team managed the project excellently, and they were an extremely skilled design partner.
    user interface design company

    ReplyDelete
  5. Their team successfully implemented improved features, contributing to the client's needed outcome.
    best UI design

    ReplyDelete
  6. Thank you I am glad about the encouragement! I love your site, you post outstanding.
    web design firm

    ReplyDelete
  7. Good blog along with the excellent quality stuff and I’m sure this will be greatly helpful.
    logo making companies

    ReplyDelete
  8. Hi Dear, have you been certainly visiting this site daily, if that's the case you then will certainly get good knowledge.
    best web design agency

    ReplyDelete
  9. Whatever you have provided for us in these posts really appreciative.
    web design and development agency

    ReplyDelete
  10. Your blog is extremely brilliant especially the quality content is really appreciable.
    experience design agency

    ReplyDelete
  11. The complete blogs are really inconceivable and definitely everyone will share this information.
    best UI UX design companies

    ReplyDelete
  12. How to Download and Activate Movavi Video Editor Plus? · Select any key from the product key passage and go to the 'Help' section of the software Activation Key For Movavi

    ReplyDelete