##// END OF EJS Templates
Ivan Valdez -
r791:90db7fc57d8d
parent child
Show More
@@ -1,451 +1,451
1 1 import numpy
2 2 import datetime
3 3 import sys
4 4 import matplotlib
5 5
6 6 if 'linux' in sys.platform:
7 7 matplotlib.use("TKAgg")
8 8
9 9 if 'darwin' in sys.platform:
10 10 matplotlib.use('TKAgg')
11 11 #Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX'
12 12 import matplotlib.pyplot
13 13
14 14 from mpl_toolkits.axes_grid1 import make_axes_locatable
15 15 from matplotlib.ticker import FuncFormatter, LinearLocator
16 16
17 17 ###########################################
18 18 #Actualizacion de las funciones del driver
19 19 ###########################################
20 20
21 21 def createFigure(id, wintitle, width, height, facecolor="w", show=True):
22 22
23 23 matplotlib.pyplot.ioff()
24 24 fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor)
25 25 fig.canvas.manager.set_window_title(wintitle)
26 26 fig.canvas.manager.resize(width, height)
27 27 matplotlib.pyplot.ion()
28 28 if show:
29 29 matplotlib.pyplot.show()
30 30
31 31 return fig
32 32
33 33 def closeFigure(show=False, fig=None):
34 34
35 35 # matplotlib.pyplot.ioff()
36 36 # matplotlib.pyplot.pause(0)
37 37
38 38 if show:
39 39 matplotlib.pyplot.show()
40 40
41 41 if fig != None:
42 42 matplotlib.pyplot.close(fig)
43 43 # matplotlib.pyplot.pause(0)
44 44 # matplotlib.pyplot.ion()
45 45
46 46 return
47 47
48 48 matplotlib.pyplot.close("all")
49 49 # matplotlib.pyplot.pause(0)
50 50 # matplotlib.pyplot.ion()
51 51
52 52 return
53 53
54 54 def saveFigure(fig, filename):
55 55
56 56 # matplotlib.pyplot.ioff()
57 57 fig.savefig(filename)
58 58 # matplotlib.pyplot.ion()
59 59
60 60 def clearFigure(fig):
61 61
62 62 fig.clf()
63 63
64 64 def setWinTitle(fig, title):
65 65
66 66 fig.canvas.manager.set_window_title(title)
67 67
68 68 def setTitle(fig, title):
69 69
70 70 fig.suptitle(title)
71 71
72 72 def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan, polar=False):
73 73
74 74 matplotlib.pyplot.ioff()
75 75 matplotlib.pyplot.figure(fig.number)
76 76 axes = matplotlib.pyplot.subplot2grid((nrow, ncol),
77 77 (xpos, ypos),
78 78 colspan=colspan,
79 79 rowspan=rowspan,
80 80 polar=polar)
81 81
82 82 matplotlib.pyplot.ion()
83 83 return axes
84 84
85 85 def setAxesText(ax, text):
86 86
87 87 ax.annotate(text,
88 88 xy = (.1, .99),
89 89 xycoords = 'figure fraction',
90 90 horizontalalignment = 'left',
91 91 verticalalignment = 'top',
92 92 fontsize = 10)
93 93
94 94 def printLabels(ax, xlabel, ylabel, title):
95 95
96 96 ax.set_xlabel(xlabel, size=11)
97 97 ax.set_ylabel(ylabel, size=11)
98 98 ax.set_title(title, size=8)
99 99
100 100 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='',
101 101 ticksize=9, xtick_visible=True, ytick_visible=True,
102 102 nxticks=4, nyticks=10,
103 103 grid=None,color='blue'):
104 104
105 105 """
106 106
107 107 Input:
108 108 grid : None, 'both', 'x', 'y'
109 109 """
110 110
111 111 matplotlib.pyplot.ioff()
112 112
113 113 ax.set_xlim([xmin,xmax])
114 114 ax.set_ylim([ymin,ymax])
115 115
116 116 printLabels(ax, xlabel, ylabel, title)
117 117
118 118 ######################################################
119 119 if (xmax-xmin)<=1:
120 120 xtickspos = numpy.linspace(xmin,xmax,nxticks)
121 121 xtickspos = numpy.array([float("%.1f"%i) for i in xtickspos])
122 122 ax.set_xticks(xtickspos)
123 123 else:
124 124 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
125 125 # xtickspos = numpy.arange(nxticks)*float(xmax-xmin)/float(nxticks) + int(xmin)
126 126 ax.set_xticks(xtickspos)
127 127
128 128 for tick in ax.get_xticklabels():
129 129 tick.set_visible(xtick_visible)
130 130
131 131 for tick in ax.xaxis.get_major_ticks():
132 132 tick.label.set_fontsize(ticksize)
133 133
134 134 ######################################################
135 135 for tick in ax.get_yticklabels():
136 136 tick.set_visible(ytick_visible)
137 137
138 138 for tick in ax.yaxis.get_major_ticks():
139 139 tick.label.set_fontsize(ticksize)
140 140
141 141 ax.plot(x, y, color=color)
142 142 iplot = ax.lines[-1]
143 143
144 144 ######################################################
145 145 if '0.' in matplotlib.__version__[0:2]:
146 146 print "The matplotlib version has to be updated to 1.1 or newer"
147 147 return iplot
148 148
149 149 if '1.0.' in matplotlib.__version__[0:4]:
150 150 print "The matplotlib version has to be updated to 1.1 or newer"
151 151 return iplot
152 152
153 153 if grid != None:
154 154 ax.grid(b=True, which='major', axis=grid)
155 155
156 156 matplotlib.pyplot.tight_layout()
157 157
158 158 matplotlib.pyplot.ion()
159 159
160 160 return iplot
161 161
162 162 def set_linedata(ax, x, y, idline):
163 163
164 164 ax.lines[idline].set_data(x,y)
165 165
166 166 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
167 167
168 168 ax = iplot.get_axes()
169 169
170 170 printLabels(ax, xlabel, ylabel, title)
171 171
172 172 set_linedata(ax, x, y, idline=0)
173 173
174 174 def addpline(ax, x, y, color, linestyle, lw):
175 175
176 176 ax.plot(x,y,color=color,linestyle=linestyle,lw=lw)
177 177
178 178
179 179 def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax,
180 180 xlabel='', ylabel='', title='', ticksize = 9,
181 181 colormap='jet',cblabel='', cbsize="5%",
182 182 XAxisAsTime=False):
183 183
184 184 matplotlib.pyplot.ioff()
185 185
186 186 divider = make_axes_locatable(ax)
187 187 ax_cb = divider.new_horizontal(size=cbsize, pad=0.05)
188 188 fig = ax.get_figure()
189 189 fig.add_axes(ax_cb)
190 190
191 191 ax.set_xlim([xmin,xmax])
192 192 ax.set_ylim([ymin,ymax])
193 193
194 194 printLabels(ax, xlabel, ylabel, title)
195 195
196 196 imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
197 197 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
198 198 cb.set_label(cblabel)
199 199
200 200 # for tl in ax_cb.get_yticklabels():
201 201 # tl.set_visible(True)
202 202
203 203 for tick in ax.yaxis.get_major_ticks():
204 204 tick.label.set_fontsize(ticksize)
205 205
206 206 for tick in ax.xaxis.get_major_ticks():
207 207 tick.label.set_fontsize(ticksize)
208 208
209 209 for tick in cb.ax.get_yticklabels():
210 210 tick.set_fontsize(ticksize)
211 211
212 212 ax_cb.yaxis.tick_right()
213 213
214 214 if '0.' in matplotlib.__version__[0:2]:
215 215 print "The matplotlib version has to be updated to 1.1 or newer"
216 216 return imesh
217 217
218 218 if '1.0.' in matplotlib.__version__[0:4]:
219 219 print "The matplotlib version has to be updated to 1.1 or newer"
220 220 return imesh
221 221
222 222 matplotlib.pyplot.tight_layout()
223 223
224 224 if XAxisAsTime:
225 225
226 226 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
227 227 ax.xaxis.set_major_formatter(FuncFormatter(func))
228 228 ax.xaxis.set_major_locator(LinearLocator(7))
229 229
230 230 matplotlib.pyplot.ion()
231 231 return imesh
232 232
233 233 def pcolor(imesh, z, xlabel='', ylabel='', title=''):
234 234
235 235 z = z.T
236 236 ax = imesh.get_axes()
237 237 printLabels(ax, xlabel, ylabel, title)
238 238 imesh.set_array(z.ravel())
239 239
240 240 def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
241 241
242 242 printLabels(ax, xlabel, ylabel, title)
243 243
244 244 ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
245 245
246 246 def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
247 247
248 248 printLabels(ax, xlabel, ylabel, title)
249 249
250 250 ax.collections.remove(ax.collections[0])
251 251
252 252 ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
253 253
254 254 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
255 255 ticksize=9, xtick_visible=True, ytick_visible=True,
256 256 nxticks=4, nyticks=10,
257 257 grid=None):
258 258
259 259 """
260 260
261 261 Input:
262 262 grid : None, 'both', 'x', 'y'
263 263 """
264 264
265 265 matplotlib.pyplot.ioff()
266 266
267 267 lines = ax.plot(x.T, y)
268 268 leg = ax.legend(lines, legendlabels, loc='upper right')
269 269 leg.get_frame().set_alpha(0.5)
270 270 ax.set_xlim([xmin,xmax])
271 271 ax.set_ylim([ymin,ymax])
272 272 printLabels(ax, xlabel, ylabel, title)
273 273
274 274 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
275 275 ax.set_xticks(xtickspos)
276 276
277 277 for tick in ax.get_xticklabels():
278 278 tick.set_visible(xtick_visible)
279 279
280 280 for tick in ax.xaxis.get_major_ticks():
281 281 tick.label.set_fontsize(ticksize)
282 282
283 283 for tick in ax.get_yticklabels():
284 284 tick.set_visible(ytick_visible)
285 285
286 286 for tick in ax.yaxis.get_major_ticks():
287 287 tick.label.set_fontsize(ticksize)
288 288
289 289 iplot = ax.lines[-1]
290 290
291 291 if '0.' in matplotlib.__version__[0:2]:
292 292 print "The matplotlib version has to be updated to 1.1 or newer"
293 293 return iplot
294 294
295 295 if '1.0.' in matplotlib.__version__[0:4]:
296 296 print "The matplotlib version has to be updated to 1.1 or newer"
297 297 return iplot
298 298
299 299 if grid != None:
300 300 ax.grid(b=True, which='major', axis=grid)
301 301
302 302 matplotlib.pyplot.tight_layout()
303 303
304 304 matplotlib.pyplot.ion()
305 305
306 306 return iplot
307 307
308 308
309 309 def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''):
310 310
311 311 ax = iplot.get_axes()
312 312
313 313 printLabels(ax, xlabel, ylabel, title)
314 314
315 315 for i in range(len(ax.lines)):
316 316 line = ax.lines[i]
317 317 line.set_data(x[i,:],y)
318 318
319 319 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
320 320 ticksize=9, xtick_visible=True, ytick_visible=True,
321 321 nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None",
322 322 grid=None, XAxisAsTime=False):
323 323
324 324 """
325 325
326 326 Input:
327 327 grid : None, 'both', 'x', 'y'
328 328 """
329 329
330 330 matplotlib.pyplot.ioff()
331 331
332 332 # lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle)
333 333 lines = ax.plot(x, y.T)
334 334 # leg = ax.legend(lines, legendlabels, loc=2, bbox_to_anchor=(1.01, 1.00), numpoints=1, handlelength=1.5, \
335 335 # handletextpad=0.5, borderpad=0.5, labelspacing=0.5, borderaxespad=0.)
336 336
337 337 leg = ax.legend(lines, legendlabels,
338 loc='upper left', bbox_to_anchor=(0.75, 1), borderaxespad=0)
338 loc='upper right', bbox_to_anchor=(1.16, 1), borderaxespad=0)
339 339
340 340 for label in leg.get_texts(): label.set_fontsize(9)
341 341
342 342 ax.set_xlim([xmin,xmax])
343 343 ax.set_ylim([ymin,ymax])
344 344 printLabels(ax, xlabel, ylabel, title)
345 345
346 346 # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
347 347 # ax.set_xticks(xtickspos)
348 348
349 349 for tick in ax.get_xticklabels():
350 350 tick.set_visible(xtick_visible)
351 351
352 352 for tick in ax.xaxis.get_major_ticks():
353 353 tick.label.set_fontsize(ticksize)
354 354
355 355 for tick in ax.get_yticklabels():
356 356 tick.set_visible(ytick_visible)
357 357
358 358 for tick in ax.yaxis.get_major_ticks():
359 359 tick.label.set_fontsize(ticksize)
360 360
361 361 iplot = ax.lines[-1]
362 362
363 363 if '0.' in matplotlib.__version__[0:2]:
364 364 print "The matplotlib version has to be updated to 1.1 or newer"
365 365 return iplot
366 366
367 367 if '1.0.' in matplotlib.__version__[0:4]:
368 368 print "The matplotlib version has to be updated to 1.1 or newer"
369 369 return iplot
370 370
371 371 if grid != None:
372 372 ax.grid(b=True, which='major', axis=grid)
373 373
374 374 matplotlib.pyplot.tight_layout()
375 375
376 376 if XAxisAsTime:
377 377
378 378 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
379 379 ax.xaxis.set_major_formatter(FuncFormatter(func))
380 380 ax.xaxis.set_major_locator(LinearLocator(7))
381 381
382 382 matplotlib.pyplot.ion()
383 383
384 384 return iplot
385 385
386 386 def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''):
387 387
388 388 ax = iplot.get_axes()
389 389
390 390 printLabels(ax, xlabel, ylabel, title)
391 391
392 392 for i in range(len(ax.lines)):
393 393 line = ax.lines[i]
394 394 line.set_data(x,y[i,:])
395 395
396 396 def createPolar(ax, x, y,
397 397 xlabel='', ylabel='', title='', ticksize = 9,
398 398 colormap='jet',cblabel='', cbsize="5%",
399 399 XAxisAsTime=False):
400 400
401 401 matplotlib.pyplot.ioff()
402 402
403 403 ax.plot(x,y,'bo', markersize=5)
404 404 # ax.set_rmax(90)
405 405 ax.set_ylim(0,90)
406 406 ax.set_yticks(numpy.arange(0,90,20))
407 407 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center' ,size='11')
408 408 # ax.text(0, 50, ylabel, rotation='vertical', va ='center', ha = 'left' ,size='11')
409 409 # ax.text(100, 100, 'example', ha='left', va='center', rotation='vertical')
410 410 ax.yaxis.labelpad = 230
411 411 printLabels(ax, xlabel, ylabel, title)
412 412 iplot = ax.lines[-1]
413 413
414 414 if '0.' in matplotlib.__version__[0:2]:
415 415 print "The matplotlib version has to be updated to 1.1 or newer"
416 416 return iplot
417 417
418 418 if '1.0.' in matplotlib.__version__[0:4]:
419 419 print "The matplotlib version has to be updated to 1.1 or newer"
420 420 return iplot
421 421
422 422 # if grid != None:
423 423 # ax.grid(b=True, which='major', axis=grid)
424 424
425 425 matplotlib.pyplot.tight_layout()
426 426
427 427 matplotlib.pyplot.ion()
428 428
429 429
430 430 return iplot
431 431
432 432 def polar(iplot, x, y, xlabel='', ylabel='', title=''):
433 433
434 434 ax = iplot.get_axes()
435 435
436 436 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center',size='11')
437 437 printLabels(ax, xlabel, ylabel, title)
438 438
439 439 set_linedata(ax, x, y, idline=0)
440 440
441 441 def draw(fig):
442 442
443 443 if type(fig) == 'int':
444 444 raise ValueError, "Error drawing: Fig parameter should be a matplotlib figure object figure"
445 445
446 446 fig.canvas.draw()
447 447
448 448 def pause(interval=0.000001):
449 449
450 450 matplotlib.pyplot.pause(interval)
451 451 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now