From: peter <com...@ya...> - 2015-07-09 16:41:52
|
hi, my code was working fine, but now i cant figure out what went wrong. any ideas? the code is supposed to plot a timeseries which it does and overlay it with another that is partially defined the input file is contructed like this: the first line is just for information purposes. after that: the first row is a growing number (the x value), the second is the timeseries and the third is the partially defined second timeseries this is the code, after the code is a example input file. the code is also accessible via this paste service: https://dpaste.de/5ZrX it got a nice python code formatter. 1. def plotTimeSeriesAndSAX(inputfile_tmp, verbose=False): 2. 3. if verbose: 4. print "plotTimeSeriesAndSAX()" 5. print "\tinputfile:", inputfile_tmp 6. print "\toutputfile: %s.png" % inputfile_tmp 7. 8. inputfile = open(inputfile_tmp, "r"); 9. 10. 11. # this holds my timeseries 12. x = [] 13. y = [] 14. 15. # this holds my "pattern" 16. pattern_x_values = [] 17. pattern_y_values = [] 18. 19. # these are for temporary use only, hold the current pattern data 20. tmp_x = [] 21. tmp_y = [] 22. 23. 24. # remove pattern/sax string, sax_string_with_Z from the datafile, only used as text in the plot 25. first_line = inputfile.readline() 26. pattern, sax, sax_string_with_Z = first_line.split() 27. 28. 29. 30. 31. for line in inputfile.readlines(): 32. 33. data = line.split() 34. x_data = data[0] 35. y_data = data[1] 36. 37. #if there is a third line (pattern at this position) 38. if len(data) == 3: 39. y2_data = data[2] 40. tmp_y.append(y2_data) 41. tmp_x.append(x_data) 42. else: 43. # if the pattern ends, add it to pattern_x/y_value and clear the tmp list 44. if len(tmp_x) != 0: 45. pattern_x_values.append(tmp_x) 46. pattern_y_values.append(tmp_y) 47. tmp_x = [] 48. tmp_y = [] 49. 50. 51. x.append(x_data) 52. y.append(y_data) 53. 54. #if pattern == "ccd": 55. # print "pattern x_values:", pattern_x_values 56. # print "pattern y_values:", pattern_y_values 57. if verbose: 58. print "\ttimeseries y value", y 59. print "pattern x_values:", pattern_x_values 60. print "pattern y_values:", pattern_y_values 61. 62. 63. 64. colors = ["red", "magenta", "mediumblue", "darkorchid", "grey"] 65. #linestyle = ["-", "--"] 66. 67. # without this, the second plot contains the first and the second 68. # the third plot contains: the first, second and third 69. plot.clf() 70. 71. # plot all my patterns into the plot 72. for s in range(0,len(pattern_x_values)): 73. #if verbose: 74. # print "\tpattern x value:", pattern_x_values[s] 75. # print "\tpattern y value:", pattern_y_values[s] 76. 77. plot.plot(pattern_x_values[s], pattern_y_values[s], colors[1]) 78. 79. 80. #plot.plot(x_all[0], y_all[0]) 81. 82. 83. import matplotlib.patches as mpatches 84. 85. 86. #red_patch = mpatches.Patch(color='red', label='The red data') 87. 88. from time import gmtime, strftime 89. current_date = strftime("%Y-%m-%d%H:%M:%S", gmtime()) 90. 91. 92. fig = plot.figure() 93. 94. 95. fig.text(0, 0, 'bottom-left corner') 96. fig.text(0, 1, current_date, ha='left', va='top') 97. mytext = "pattern: %ssax: %ssax with Z: %s" % (pattern, sax, sax_string_with_Z) 98. fig.text(1,1, mytext ) 99. 100. 101. # add the original timeseries to the plot 102. plot.plot(x,y, "forestgreen") 103. #if pattern == "ccd": 104. # plot.show() 105. 106. 107. directory, filename = os.path.split(inputfile_tmp) 108. 109. plot.savefig(os.path.join(directory, "plots/%s.png" % filename))#, bbox_inches='tight') 110. # remove the last figure from memory 111. #plot.close() 112. 113. 114. 115. 116. 117. 118. 119. 120. #input: 121. dee ccccccccccaacddeedcccccccdc ZZZZZZZZZZZZZZdeeZZZZZZZZZZ 122. 1 -0.015920084 123. 2 -0.044660769 124. 3 -0.044660769 125. 4 -0.092561907 126. 5 0.012820599 127. 6 -0.015920084 128. 7 0.012820599 129. 8 -0.054240996 130. 9 0.031981054 131. 10 0.031981054 132. 11 -0.025500313 133. 12 -0.044660769 134. 13 0.012820599 135. 14 -0.025500313 136. 15 0.0032403709 137. 16 -0.006339857 138. 17 0.0032403709 139. 18 -0.025500313 140. 19 0.031981054 141. 20 0.031981054 142. 21 0.031981054 143. 22 0.022400826 144. 23 0.031981054 145. 24 0.05114151 146. 25 0.079882193 147. 26 0.05114151 148. 27 0.05114151 149. 28 0.05114151 150. 29 0.099042646 151. 30 0.060721738 152. 31 -0.015920084 153. 32 -0.054240996 154. 33 0.23316584 155. 34 0.26190652 156. 35 0.37686926 157. 36 0.12778333 158. 37 -0.044660769 159. 38 -0.26500601 160. 39 -0.41828965 161. 40 -0.38954897 162. 41 -0.26500601 163. 42 -0.14046305 164. 43 -0.073401452 165. 44 -0.12130259 166. 45 -0.082981679 167. 46 -0.14046305 168. 47 -0.054240996 169. 48 -0.082981679 170. 49 -0.015920084 171. 50 -0.073401452 172. 51 -0.015920084 173. 52 0.10862288 174. 53 1.1816084 175. 54 -1.3379915 176. 55 -4.6335899 177. 56 -6.74124 178. 57 -4.7772933 179. 58 -3.4839626 180. 59 -2.075669 181. 60 -1.0984858 182. 61 -0.37038851 183. 62 -0.063821223 184. 63 0.11820311 185. 64 0.13736356 186. 65 0.15652401 187. 66 0.11820311 188. 67 0.32896812 189. 68 0.27148675 190. 69 0.30022744 191. 70 0.31938789 192. 71 0.3577088 0.5449999999999999 193. 72 0.40560994 0.5449999999999999 194. 73 0.44393085 0.5449999999999999 195. 74 0.49183198 0.5449999999999999 196. 75 0.67385632 0.5449999999999999 197. 76 0.79839928 0.84 198. 77 0.9995841 0.84 199. 78 1.1528677 0.84 200. 79 1.4115338 0.84 201. 80 1.5552373 0.84 202. 81 1.7468418 0.84 203. 82 1.7755825 0.84 204. 83 1.7276813 0.84 205. 84 1.4115338 0.84 206. 85 1.0858061 0.84 207. 86 0.65469586 208. 87 0.43435063 209. 88 0.21400538 210. 89 0.14694379 211. 90 0.089462421 212. 91 0.070301966 213. 92 0.031981054 214. 93 0.05114151 215. 94 0.070301966 216. 95 0.13736356 217. 96 0.079882193 218. 97 0.12778333 219. 98 0.15652401 220. 99 0.16610425 221. 100 0.13736356 222. 101 0.13736356 223. 102 0.089462421 224. 103 0.2523263 225. 104 0.21400538 226. 105 0.22358561 227. 106 0.1852647 228. 107 0.19484493 229. 108 0.1852647 230. 109 0.16610425 231. 110 0.13736356 232. 111 0.15652401 233. 112 0.14694379 234. 113 0.16610425 235. 114 0.099042646 236. 115 0.12778333 237. 116 0.13736356 238. 117 0.089462421 239. 118 0.079882193 240. 119 0.089462421 241. 120 0.041561282 242. 121 0.041561282 243. 122 0.079882193 244. 123 0.11820311 245. 124 0.099042646 246. 125 0.089462421 247. 126 0.05114151 248. 127 0.17568447 249. 128 0.30022744 250. 129 0.32896812 251. 130 0.42477039 252. 131 0.17568447 253. 132 0.022400826 254. 133 -0.20752464 255. 134 -0.24584556 256. 135 -0.24584556 |