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