Surface NMR processing and inversion GUI
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mydynamicmplcanvas.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. from __future__ import unicode_literals
  2. import sys
  3. import os
  4. import random
  5. import matplotlib
  6. # Make sure that we are using QT5
  7. matplotlib.use('Qt5Agg')
  8. from PyQt5 import QtCore, QtWidgets
  9. from numpy import arange, sin, pi
  10. from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
  11. from matplotlib.figure import Figure
  12. import numpy as np
  13. class MyMplCanvas(FigureCanvas):
  14. """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
  15. def __init__(self, parent=None, width=3, height=4, dpi=100):
  16. self.fig = Figure(figsize=(width, height), dpi=dpi)
  17. FigureCanvas.__init__(self, self.fig)
  18. self.setParent(parent)
  19. FigureCanvas.updateGeometry(self)
  20. def compute_initial_figure(self):
  21. pass
  22. def clicked(self):
  23. print ("Clicked")
  24. class MyDynamicMplCanvas(MyMplCanvas):
  25. """A canvas that updates itself every second with a new plot."""
  26. def __init__(self, *args, **kwargs):
  27. MyMplCanvas.__init__(self, *args, **kwargs)
  28. self.ax1 = self.fig.add_axes([.125,.1,.725,.8])
  29. self.ax2 = self.ax1.twinx() # fig.add_axes([.125,.1,.725,.8])
  30. self.compute_initial_figure()
  31. def reAxH(self, num, shx=True, shy=True):
  32. try:
  33. self.fig.clear()
  34. except:
  35. pass
  36. for n in range(num):
  37. if n == 0:
  38. self.ax1 = self.fig.add_subplot( 1, num, 1 )
  39. self.ax1.tick_params(axis='both', which='major', labelsize=8)
  40. self.ax1.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  41. self.ax1.yaxis.get_offset_text().set_size(8)
  42. if n == 1:
  43. self.ax2 = self.fig.add_subplot( 1, num, 2 )
  44. self.ax2.tick_params(axis='both', which='major', labelsize=8)
  45. self.ax2.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  46. self.ax2.yaxis.get_offset_text().set_size(8)
  47. if n == 2:
  48. self.ax3 = self.fig.add_subplot( 1, num, 3 )
  49. self.ax3.tick_params(axis='both', which='major', labelsize=8)
  50. self.ax3.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  51. self.ax3.yaxis.get_offset_text().set_size(8)
  52. if n == 3:
  53. self.ax4 = self.fig.add_subplot( 1, num, 4 )
  54. self.ax4.tick_params(axis='both', which='major', labelsize=8)
  55. self.ax4.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  56. self.ax4.yaxis.get_offset_text().set_size(8)
  57. def reAxH2(self, num, shx=True, shy=True):
  58. try:
  59. for ax in fig.axes:
  60. self.fig.delaxes(ax)
  61. except:
  62. pass
  63. try:
  64. self.fig.clear()
  65. except:
  66. pass
  67. for n in range(num):
  68. if n == 0:
  69. self.ax1 = self.fig.add_subplot( 2, num, 1 )
  70. self.ax1.tick_params(axis='both', which='major', labelsize=8)
  71. self.ax1.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  72. self.ax1.yaxis.get_offset_text().set_size(8)
  73. self.ax21 = self.fig.add_subplot( 2, num, num+1 )
  74. self.ax21.tick_params(axis='both', which='major', labelsize=8)
  75. self.ax21.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  76. self.ax21.yaxis.get_offset_text().set_size(8)
  77. if n == 1:
  78. self.ax2 = self.fig.add_subplot( 2, num, 2, sharex=self.ax1, sharey=self.ax1 )
  79. self.ax2.tick_params(axis='both', which='major', labelsize=8)
  80. self.ax2.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  81. self.ax2.yaxis.get_offset_text().set_size(8)
  82. self.ax22 = self.fig.add_subplot( 2, num, num+2, sharex=self.ax21, sharey=self.ax21 )
  83. self.ax22.tick_params(axis='both', which='major', labelsize=8)
  84. self.ax22.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  85. self.ax22.yaxis.get_offset_text().set_size(8)
  86. if n == 2:
  87. self.ax3 = self.fig.add_subplot( 2, num, 3, sharex=self.ax1, sharey=self.ax1 )
  88. self.ax3.tick_params(axis='both', which='major', labelsize=8)
  89. self.ax3.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  90. self.ax3.yaxis.get_offset_text().set_size(8)
  91. self.ax23 = self.fig.add_subplot( 2, num, num+3, sharex=self.ax21, sharey=self.ax21 )
  92. self.ax23.tick_params(axis='both', which='major', labelsize=8)
  93. self.ax23.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  94. self.ax23.yaxis.get_offset_text().set_size(8)
  95. if n == 3:
  96. self.ax4 = self.fig.add_subplot( 2, num, 4, sharex=self.ax1, sharey=self.ax1 )
  97. self.ax4.tick_params(axis='both', which='major', labelsize=8)
  98. self.ax4.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  99. self.ax4.yaxis.get_offset_text().set_size(8)
  100. self.ax24 = self.fig.add_subplot( 2, num, num+4, sharex=self.ax21, sharey=self.ax21 )
  101. self.ax24.tick_params(axis='both', which='major', labelsize=8)
  102. self.ax24.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  103. self.ax24.yaxis.get_offset_text().set_size(8)
  104. if n == 4:
  105. self.ax5 = self.fig.add_subplot( 2, num, 5, sharex=self.ax1, sharey=self.ax1 )
  106. self.ax5.tick_params(axis='both', which='major', labelsize=8)
  107. self.ax5.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  108. self.ax5.yaxis.get_offset_text().set_size(8)
  109. self.ax25 = self.fig.add_subplot( 2, num, num+5, sharex=self.ax21, sharey=self.ax21 )
  110. self.ax25.tick_params(axis='both', which='major', labelsize=8)
  111. self.ax25.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  112. self.ax25.yaxis.get_offset_text().set_size(8)
  113. if n == 5:
  114. self.ax6 = self.fig.add_subplot( 2, num, 6, sharex=self.ax1, sharey=self.ax1 )
  115. self.ax6.tick_params(axis='both', which='major', labelsize=8)
  116. self.ax6.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  117. self.ax6.yaxis.get_offset_text().set_size(8)
  118. self.ax26 = self.fig.add_subplot( 2, num, num+6, sharex=self.ax21, sharey=self.ax21 )
  119. self.ax26.tick_params(axis='both', which='major', labelsize=8)
  120. self.ax26.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  121. self.ax26.yaxis.get_offset_text().set_size(8)
  122. if n == 6:
  123. self.ax7 = self.fig.add_subplot( 2, num, 7, sharex=self.ax1, sharey=self.ax1 )
  124. self.ax7.tick_params(axis='both', which='major', labelsize=8)
  125. self.ax7.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  126. self.ax7.yaxis.get_offset_text().set_size(8)
  127. self.ax27 = self.fig.add_subplot( 2, num, num+7, sharex=self.ax21, sharey=self.ax21 )
  128. self.ax27.tick_params(axis='both', which='major', labelsize=8)
  129. self.ax27.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  130. self.ax27.yaxis.get_offset_text().set_size(8)
  131. if n == 7:
  132. self.ax8 = self.fig.add_subplot( 2, num, 8, sharex=self.ax1, sharey=self.ax1 )
  133. self.ax8.tick_params(axis='both', which='major', labelsize=8)
  134. self.ax8.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  135. self.ax8.yaxis.get_offset_text().set_size(8)
  136. self.ax28 = self.fig.add_subplot( 2, num, num+8, sharex=self.ax21, sharey=self.ax21 )
  137. self.ax28.tick_params(axis='both', which='major', labelsize=8)
  138. self.ax28.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  139. self.ax28.yaxis.get_offset_text().set_size(8)
  140. def reAx2(self, shx=True, shy=True):
  141. try:
  142. self.fig.clear()
  143. except:
  144. pass
  145. try:
  146. self.ax1.clear()
  147. self.delaxes(self.ax1) #.clear()
  148. except:
  149. pass
  150. try:
  151. self.delaxes(self.ax3) #.clear()
  152. except:
  153. pass
  154. try:
  155. self.ax2.clear()
  156. self.delaxes(self.ax2) #.clear()
  157. except:
  158. pass
  159. self.ax1 = self.fig.add_subplot(211)
  160. if shx and shy:
  161. self.ax2 = self.fig.add_subplot(212, sharex=self.ax1, sharey=self.ax1)
  162. elif shx == True:
  163. self.ax2 = self.fig.add_subplot(212, sharex=self.ax1)
  164. elif shy == True:
  165. self.ax2 = self.fig.add_subplot(212, sharey=self.ax1)
  166. else:
  167. self.ax2 = self.fig.add_subplot(212)
  168. self.ax1.tick_params(axis='both', which='major', labelsize=8)
  169. self.ax2.tick_params(axis='both', which='major', labelsize=8)
  170. self.ax1.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  171. self.ax2.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  172. self.ax1.yaxis.get_offset_text().set_size(8)
  173. self.ax2.yaxis.get_offset_text().set_size(8)
  174. def reAx3(self, shx=True, shy=True):
  175. try:
  176. self.fig.clear()
  177. except:
  178. pass
  179. try:
  180. self.ax1.clear()
  181. self.delaxes(self.ax1) #.clear()
  182. except:
  183. pass
  184. try:
  185. self.ax2.clear()
  186. self.delaxes(self.ax2) #.clear()
  187. except:
  188. pass
  189. try:
  190. self.ax3.clear()
  191. self.delaxes(self.ax3) #.clear()
  192. except:
  193. pass
  194. self.ax1 = self.fig.add_subplot(211)
  195. if shx and shy:
  196. self.ax2 = self.fig.add_subplot(212, sharex=self.ax1, sharey=self.ax1)
  197. elif shx:
  198. self.ax2 = self.fig.add_subplot(212, sharex=self.ax1)
  199. elif shy:
  200. self.ax2 = self.fig.add_subplot(212, sharey=self.ax1)
  201. else:
  202. self.ax2 = self.fig.add_subplot(212)
  203. self.ax3 = self.ax1.twinx()
  204. self.ax1.tick_params(axis='both', which='major', labelsize=8)
  205. self.ax2.tick_params(axis='both', which='major', labelsize=8)
  206. self.ax3.tick_params(axis='both', which='major', labelsize=8)
  207. self.ax1.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  208. self.ax2.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  209. self.ax3.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  210. self.ax1.yaxis.get_offset_text().set_size(8)
  211. self.ax2.yaxis.get_offset_text().set_size(8)
  212. self.ax3.yaxis.get_offset_text().set_size(8)
  213. def reAx4(self):
  214. try:
  215. self.fig.clear()
  216. except:
  217. pass
  218. # two main axes
  219. self.ax1 = self.fig.add_axes([0.15, 0.55, 0.625, 0.3672])
  220. self.ax2 = self.fig.add_axes([0.15, 0.135, 0.625, 0.3672])
  221. # for colourbars
  222. self.cax1 = self.fig.add_axes([0.8, 0.55, 0.025, 0.3672])
  223. self.cax2 = self.fig.add_axes([0.8, 0.135, 0.025, 0.3672])
  224. self.ax1.tick_params(axis='both', which='major', labelsize=8)
  225. self.ax2.tick_params(axis='both', which='major', labelsize=8)
  226. self.ax1.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  227. self.ax2.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  228. self.ax1.yaxis.get_offset_text().set_size(8)
  229. self.ax2.yaxis.get_offset_text().set_size(8)
  230. self.cax1.tick_params(axis='both', which='major', labelsize=8)
  231. self.cax2.tick_params(axis='both', which='major', labelsize=8)
  232. self.cax1.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  233. self.cax2.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  234. self.cax1.yaxis.get_offset_text().set_size(8) #.get_text()
  235. self.cax2.yaxis.get_offset_text().set_size(8) #.get_text()
  236. self.cax1.tick_params(labelsize=8)
  237. self.cax2.tick_params(labelsize=8)
  238. def compute_initial_figure(self):
  239. t = np.arange(0,.3,1e-4)
  240. x = np.cos(t*2000.*np.pi*2)*np.exp(-t/.07)
  241. x2 = np.exp(-t/.07)
  242. dp = self.ax1.plot(t, x, 'r',label='test function')
  243. dp2 = self.ax2.plot(t, x2, 'r',label='test function2')
  244. self.ax1.set_xlabel("Time [s]", fontsize=8)
  245. self.ax1.set_ylabel("Signal [nV]", fontsize=8)
  246. self.ax1.tick_params(axis='both', which='major', labelsize=8)
  247. self.ax1.tick_params(axis='both', which='minor', labelsize=6)
  248. self.ax1.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
  249. self.ax1.legend(prop={'size':6})