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