Surface NMR processing and inversion GUI
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

temp.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. """
  2. This demo demonstrates how to embed a matplotlib (mpl) plot
  3. into a PyQt4 GUI application, including:
  4. * Using the navigation toolbar
  5. * Adding data to the plot
  6. * Dynamically modifying the plot's properties
  7. * Processing mpl events
  8. * Saving the plot to a file from a menu
  9. The main goal is to serve as a basis for developing rich PyQt GUI
  10. applications featuring mpl plots (using the mpl OO API).
  11. Eli Bendersky (eliben@gmail.com)
  12. License: this code is in the public domain
  13. Last modified: 19.01.2009
  14. """
  15. import sys, os, random
  16. from PyQt4.QtCore import *
  17. from PyQt4.QtGui import *
  18. import matplotlib
  19. from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  20. from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
  21. from matplotlib.figure import Figure
  22. class AppForm(QMainWindow):
  23. def __init__(self, parent=None):
  24. QMainWindow.__init__(self, parent)
  25. self.setWindowTitle('Demo: PyQt with matplotlib')
  26. self.create_menu()
  27. self.create_main_frame()
  28. self.create_status_bar()
  29. self.textbox.setText('1 2 3 4')
  30. self.on_draw()
  31. def save_plot(self):
  32. file_choices = "PNG (*.png)|*.png"
  33. path = unicode(QFileDialog.getSaveFileName(self,
  34. 'Save file', '',
  35. file_choices))
  36. if path:
  37. self.canvas.print_figure(path, dpi=self.dpi)
  38. self.statusBar().showMessage('Saved to %s' % path, 2000)
  39. def on_about(self):
  40. msg = """ A demo of using PyQt with matplotlib:
  41. * Use the matplotlib navigation bar
  42. * Add values to the text box and press Enter (or click "Draw")
  43. * Show or hide the grid
  44. * Drag the slider to modify the width of the bars
  45. * Save the plot to a file using the File menu
  46. * Click on a bar to receive an informative message
  47. """
  48. QMessageBox.about(self, "About the demo", msg.strip())
  49. def on_pick(self, event):
  50. # The event received here is of the type
  51. # matplotlib.backend_bases.PickEvent
  52. #
  53. # It carries lots of information, of which we're using
  54. # only a small amount here.
  55. #
  56. box_points = event.artist.get_bbox().get_points()
  57. msg = "You've clicked on a bar with coords:\n %s" % box_points
  58. QMessageBox.information(self, "Click!", msg)
  59. def on_draw(self):
  60. """ Redraws the figure
  61. """
  62. str = unicode(self.textbox.text())
  63. self.data = map(int, str.split())
  64. x = range(len(self.data))
  65. # clear the axes and redraw the plot anew
  66. #
  67. self.axes.clear()
  68. self.axes.grid(self.grid_cb.isChecked())
  69. self.axes.bar(
  70. left=x,
  71. height=self.data,
  72. width=self.slider.value() / 100.0,
  73. align='center',
  74. alpha=0.44,
  75. picker=5)
  76. self.canvas.draw()
  77. def create_main_frame(self):
  78. self.main_frame = QWidget()
  79. # Create the mpl Figure and FigCanvas objects.
  80. # 5x4 inches, 100 dots-per-inch
  81. #
  82. self.dpi = 100
  83. self.fig = Figure((5.0, 4.0), dpi=self.dpi)
  84. self.canvas = FigureCanvas(self.fig)
  85. self.canvas.setParent(self.main_frame)
  86. # Since we have only one plot, we can use add_axes
  87. # instead of add_subplot, but then the subplot
  88. # configuration tool in the navigation toolbar wouldn't
  89. # work.
  90. #
  91. self.axes = self.fig.add_subplot(111)
  92. # Bind the 'pick' event for clicking on one of the bars
  93. #
  94. self.canvas.mpl_connect('pick_event', self.on_pick)
  95. # Create the navigation toolbar, tied to the canvas
  96. #
  97. self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)
  98. # Other GUI controls
  99. #
  100. self.textbox = QLineEdit()
  101. self.textbox.setMinimumWidth(200)
  102. self.connect(self.textbox, SIGNAL('editingFinished ()'), self.on_draw)
  103. self.draw_button = QPushButton("&Draw")
  104. self.connect(self.draw_button, SIGNAL('clicked()'), self.on_draw)
  105. self.grid_cb = QCheckBox("Show &Grid")
  106. self.grid_cb.setChecked(False)
  107. self.connect(self.grid_cb, SIGNAL('stateChanged(int)'), self.on_draw)
  108. slider_label = QLabel('Bar width (%):')
  109. self.slider = QSlider(Qt.Horizontal)
  110. self.slider.setRange(1, 100)
  111. self.slider.setValue(20)
  112. self.slider.setTracking(True)
  113. self.slider.setTickPosition(QSlider.TicksBothSides)
  114. self.connect(self.slider, SIGNAL('valueChanged(int)'), self.on_draw)
  115. #
  116. # Layout with box sizers
  117. #
  118. hbox = QHBoxLayout()
  119. for w in [ self.textbox, self.draw_button, self.grid_cb,
  120. slider_label, self.slider]:
  121. hbox.addWidget(w)
  122. hbox.setAlignment(w, Qt.AlignVCenter)
  123. vbox = QVBoxLayout()
  124. vbox.addWidget(self.canvas)
  125. vbox.addWidget(self.mpl_toolbar)
  126. vbox.addLayout(hbox)
  127. self.main_frame.setLayout(vbox)
  128. self.setCentralWidget(self.main_frame)
  129. def create_status_bar(self):
  130. self.status_text = QLabel("This is a demo")
  131. self.statusBar().addWidget(self.status_text, 1)
  132. def create_menu(self):
  133. self.file_menu = self.menuBar().addMenu("&File")
  134. load_file_action = self.create_action("&Save plot",
  135. shortcut="Ctrl+S", slot=self.save_plot,
  136. tip="Save the plot")
  137. quit_action = self.create_action("&Quit", slot=self.close,
  138. shortcut="Ctrl+Q", tip="Close the application")
  139. self.add_actions(self.file_menu,
  140. (load_file_action, None, quit_action))
  141. self.help_menu = self.menuBar().addMenu("&Help")
  142. about_action = self.create_action("&About",
  143. shortcut='F1', slot=self.on_about,
  144. tip='About the demo')
  145. self.add_actions(self.help_menu, (about_action,))
  146. def add_actions(self, target, actions):
  147. for action in actions:
  148. if action is None:
  149. target.addSeparator()
  150. else:
  151. target.addAction(action)
  152. def create_action( self, text, slot=None, shortcut=None,
  153. icon=None, tip=None, checkable=False,
  154. signal="triggered()"):
  155. action = QAction(text, self)
  156. if icon is not None:
  157. action.setIcon(QIcon(":/%s.png" % icon))
  158. if shortcut is not None:
  159. action.setShortcut(shortcut)
  160. if tip is not None:
  161. action.setToolTip(tip)
  162. action.setStatusTip(tip)
  163. if slot is not None:
  164. self.connect(action, SIGNAL(signal), slot)
  165. if checkable:
  166. action.setCheckable(True)
  167. return action
  168. def main():
  169. app = QApplication(sys.argv)
  170. form = AppForm()
  171. form.show()
  172. app.exec_()
  173. if __name__ == "__main__":
  174. main()