##// END OF EJS Templates
Optimizacion del driver para matplotlib. Activacion y descativacion del modo interactivo al crear figures y axes
Miguel Valdez -
r244:b2e7e61d8cbe
parent child
Show More
@@ -1,342 +1,366
1 1 import numpy
2 2 import datetime
3 3 import matplotlib
4 matplotlib.use("TKAgg")
4 matplotlib.use("GTKAgg")
5 5 import matplotlib.pyplot
6 6 import matplotlib.dates
7 7 #import scitools.numpyutils
8 8 from mpl_toolkits.axes_grid1 import make_axes_locatable
9 9
10 10 from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, SecondLocator, DateFormatter
11 11 from matplotlib.ticker import FuncFormatter
12 12 from matplotlib.ticker import *
13 13
14 14 ###########################################
15 15 #Actualizacion de las funciones del driver
16 16 ###########################################
17 17
18 18 def createFigure(idfigure, wintitle, width, height, facecolor="w"):
19 19
20 20 matplotlib.pyplot.ioff()
21 21 fig = matplotlib.pyplot.figure(num=idfigure, facecolor=facecolor)
22 22 fig.canvas.manager.set_window_title(wintitle)
23 23 fig.canvas.manager.resize(width, height)
24 24 matplotlib.pyplot.ion()
25 matplotlib.pyplot.show()
25 26
26 27 return fig
27 28
28 29 def closeFigure():
29 30
30 31 matplotlib.pyplot.ioff()
31 32 matplotlib.pyplot.show()
32 33
33 34 return
34 35
35 36 def saveFigure(fig, filename):
37
38 matplotlib.pyplot.ioff()
36 39 fig.savefig(filename)
40 matplotlib.pyplot.ion()
37 41
38 42 def setWinTitle(fig, title):
39 43
40 44 fig.canvas.manager.set_window_title(title)
41 45
42 46 def setTitle(fig, title):
43 47
44 48 fig.suptitle(title)
45 49
46 50 def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan):
47 51
52 matplotlib.pyplot.ioff()
48 53 matplotlib.pyplot.figure(fig.number)
49 54 axes = matplotlib.pyplot.subplot2grid((nrow, ncol),
50 55 (xpos, ypos),
51 56 colspan=colspan,
52 57 rowspan=rowspan)
58
59 matplotlib.pyplot.ion()
53 60 return axes
54 61
55 62 def setAxesText(ax, text):
56 63
57 64 ax.annotate(text,
58 65 xy = (.1, .99),
59 66 xycoords = 'figure fraction',
60 67 horizontalalignment = 'left',
61 68 verticalalignment = 'top',
62 69 fontsize = 10)
63 70
64 71 def printLabels(ax, xlabel, ylabel, title):
65 72
66 73 ax.set_xlabel(xlabel, size=11)
67 74 ax.set_ylabel(ylabel, size=11)
68 75 ax.set_title(title, size=12)
69 76
70 77 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='',
71 78 ticksize=9, xtick_visible=True, ytick_visible=True,
72 79 nxticks=4, nyticks=10,
73 80 grid=None):
74 81
75 82 """
76 83
77 84 Input:
78 85 grid : None, 'both', 'x', 'y'
79 86 """
80
81 ax.plot(x, y)
87
88 matplotlib.pyplot.ioff()
89
82 90 ax.set_xlim([xmin,xmax])
83 91 ax.set_ylim([ymin,ymax])
84 92
85 93 printLabels(ax, xlabel, ylabel, title)
86 94
87 95 ######################################################
88 96 if (xmax-xmin)<=1:
89 97 xtickspos = numpy.linspace(xmin,xmax,nxticks)
90 98 xtickspos = numpy.array([float("%.1f"%i) for i in xtickspos])
91 99 ax.set_xticks(xtickspos)
92 100 else:
93 101 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
94 102 ax.set_xticks(xtickspos)
95 103
96 104 for tick in ax.get_xticklabels():
97 105 tick.set_visible(xtick_visible)
98 106
99 107 for tick in ax.xaxis.get_major_ticks():
100 108 tick.label.set_fontsize(ticksize)
101 109
102 110 ######################################################
103 111 for tick in ax.get_yticklabels():
104 112 tick.set_visible(ytick_visible)
105 113
106 114 for tick in ax.yaxis.get_major_ticks():
107 115 tick.label.set_fontsize(ticksize)
108
116
117 ax.plot(x, y)
109 118 iplot = ax.lines[-1]
110 119
111 120 ######################################################
112 121 if '0.' in matplotlib.__version__[0:2]:
113 122 print "The matplotlib version has to be updated to 1.1 or newer"
114 123 return iplot
115 124
116 125 if '1.0.' in matplotlib.__version__[0:4]:
117 126 print "The matplotlib version has to be updated to 1.1 or newer"
118 127 return iplot
119 128
120 129 if grid != None:
121 130 ax.grid(b=True, which='major', axis=grid)
122 131
123 132 matplotlib.pyplot.tight_layout()
124 133
134 matplotlib.pyplot.ion()
135
125 136 return iplot
126 137
138 def set_linedata(ax, x, y, idline):
139
140 ax.lines[idline].set_data(x,y)
141
127 142 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
128 143
129 144 ax = iplot.get_axes()
130 145
131 146 printLabels(ax, xlabel, ylabel, title)
132 147
133 148 set_linedata(ax, x, y, idline=0)
134 149
135 150 def addpline(ax, x, y, color, linestyle, lw):
151
136 152 ax.plot(x,y,color=color,linestyle=linestyle,lw=lw)
153
154
155 def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax,
156 xlabel='', ylabel='', title='', ticksize = 9,
157 colormap='jet',cblabel='', cbsize="5%",
158 XAxisAsTime=False):
137 159
160 matplotlib.pyplot.ioff()
138 161
139 def set_linedata(ax, x, y, idline):
140 ax.lines[idline].set_data(x,y)
162 divider = make_axes_locatable(ax)
163 ax_cb = divider.new_horizontal(size=cbsize, pad=0.05)
164 fig = ax.get_figure()
165 fig.add_axes(ax_cb)
166
167 ax.set_xlim([xmin,xmax])
168 ax.set_ylim([ymin,ymax])
169
170 printLabels(ax, xlabel, ylabel, title)
171
172 imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
173 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
174 cb.set_label(cblabel)
175
176 # for tl in ax_cb.get_yticklabels():
177 # tl.set_visible(True)
178
179 for tick in ax.yaxis.get_major_ticks():
180 tick.label.set_fontsize(ticksize)
181
182 for tick in ax.xaxis.get_major_ticks():
183 tick.label.set_fontsize(ticksize)
184
185 for tick in cb.ax.get_yticklabels():
186 tick.set_fontsize(ticksize)
187
188 ax_cb.yaxis.tick_right()
189
190 if '0.' in matplotlib.__version__[0:2]:
191 print "The matplotlib version has to be updated to 1.1 or newer"
192 return imesh
193
194 if '1.0.' in matplotlib.__version__[0:4]:
195 print "The matplotlib version has to be updated to 1.1 or newer"
196 return imesh
197
198 matplotlib.pyplot.tight_layout()
199
200 if XAxisAsTime:
201
202 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
203 ax.xaxis.set_major_formatter(FuncFormatter(func))
204 ax.xaxis.set_major_locator(LinearLocator(7))
205
206 matplotlib.pyplot.ion()
207 return imesh
208
209 def pcolor(imesh, z, xlabel='', ylabel='', title=''):
210
211 z = z.T
212
213 ax = imesh.get_axes()
214
215 printLabels(ax, xlabel, ylabel, title)
216
217 imesh.set_array(z.ravel())
218
219 def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
220
221 printLabels(ax, xlabel, ylabel, title)
222
223 imesh = ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
141 224
142 225 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
143 226 ticksize=9, xtick_visible=True, ytick_visible=True,
144 227 nxticks=4, nyticks=10,
145 228 grid=None):
146 229
147 230 """
148 231
149 232 Input:
150 233 grid : None, 'both', 'x', 'y'
151 234 """
152
235
236 matplotlib.pyplot.ioff()
237
153 238 lines = ax.plot(x.T, y)
154 239 leg = ax.legend(lines, legendlabels, loc='upper right')
155 240 leg.get_frame().set_alpha(0.5)
156 241 ax.set_xlim([xmin,xmax])
157 242 ax.set_ylim([ymin,ymax])
158 243 printLabels(ax, xlabel, ylabel, title)
159 244
160 245 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
161 246 ax.set_xticks(xtickspos)
162 247
163 248 for tick in ax.get_xticklabels():
164 249 tick.set_visible(xtick_visible)
165 250
166 251 for tick in ax.xaxis.get_major_ticks():
167 252 tick.label.set_fontsize(ticksize)
168 253
169 254 for tick in ax.get_yticklabels():
170 255 tick.set_visible(ytick_visible)
171 256
172 257 for tick in ax.yaxis.get_major_ticks():
173 258 tick.label.set_fontsize(ticksize)
174 259
175 260 iplot = ax.lines[-1]
176 261
177 262 if '0.' in matplotlib.__version__[0:2]:
178 263 print "The matplotlib version has to be updated to 1.1 or newer"
179 264 return iplot
180 265
181 266 if '1.0.' in matplotlib.__version__[0:4]:
182 267 print "The matplotlib version has to be updated to 1.1 or newer"
183 268 return iplot
184 269
185 270 if grid != None:
186 271 ax.grid(b=True, which='major', axis=grid)
187 272
188 273 matplotlib.pyplot.tight_layout()
189
274
275 matplotlib.pyplot.ion()
276
190 277 return iplot
191 278
192 279
193 280 def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''):
194 281
195 282 ax = iplot.get_axes()
196 283
197 284 printLabels(ax, xlabel, ylabel, title)
198 285
199 286 for i in range(len(ax.lines)):
200 287 line = ax.lines[i]
201 288 line.set_data(x[i,:],y)
202 289
203 290 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
204 291 ticksize=9, xtick_visible=True, ytick_visible=True,
205 292 nxticks=4, nyticks=10, marker='^', markersize=8, linestyle="solid",
206 293 grid=None, XAxisAsTime=False):
207 294
208 295 """
209 296
210 297 Input:
211 298 grid : None, 'both', 'x', 'y'
212 299 """
213 300
301 matplotlib.pyplot.ioff()
302
214 303 lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle)
215 304 leg = ax.legend(lines, legendlabels, bbox_to_anchor=(1.05, 1), loc='upper right', numpoints=1, handlelength=1.5, \
216 305 handletextpad=0.5, borderpad=0.2, labelspacing=0.2, borderaxespad=0.)
217 306
218 307 ax.set_xlim([xmin,xmax])
219 308 ax.set_ylim([ymin,ymax])
220 309 printLabels(ax, xlabel, ylabel, title)
221 310
222 311 # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
223 312 # ax.set_xticks(xtickspos)
224 313
225 314 for tick in ax.get_xticklabels():
226 315 tick.set_visible(xtick_visible)
227 316
228 317 for tick in ax.xaxis.get_major_ticks():
229 318 tick.label.set_fontsize(ticksize)
230 319
231 320 for tick in ax.get_yticklabels():
232 321 tick.set_visible(ytick_visible)
233 322
234 323 for tick in ax.yaxis.get_major_ticks():
235 324 tick.label.set_fontsize(ticksize)
236 325
237 326 iplot = ax.lines[-1]
238 327
239 328 if '0.' in matplotlib.__version__[0:2]:
240 329 print "The matplotlib version has to be updated to 1.1 or newer"
241 330 return iplot
242 331
243 332 if '1.0.' in matplotlib.__version__[0:4]:
244 333 print "The matplotlib version has to be updated to 1.1 or newer"
245 334 return iplot
246 335
247 336 if grid != None:
248 337 ax.grid(b=True, which='major', axis=grid)
249 338
250 339 matplotlib.pyplot.tight_layout()
251 340
252 341 if XAxisAsTime:
253 342
254 343 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
255 344 ax.xaxis.set_major_formatter(FuncFormatter(func))
256 345 ax.xaxis.set_major_locator(LinearLocator(7))
257 346
347 matplotlib.pyplot.ion()
348
258 349 return iplot
259 350
260 351 def pmultilineinyaxis(iplot, x, y, xlabel='', ylabel='', title=''):
261 352
262 353 ax = iplot.get_axes()
263 354
264 355 printLabels(ax, xlabel, ylabel, title)
265 356
266 357 for i in range(len(ax.lines)):
267 358 line = ax.lines[i]
268 359 line.set_data(x,y[i,:])
269
270 def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax,
271 xlabel='', ylabel='', title='', ticksize = 9,
272 colormap='jet',cblabel='', cbsize="5%",
273 XAxisAsTime=False):
274
275 divider = make_axes_locatable(ax)
276 ax_cb = divider.new_horizontal(size=cbsize, pad=0.05)
277 fig = ax.get_figure()
278 fig.add_axes(ax_cb)
279
280 ax.set_xlim([xmin,xmax])
281 ax.set_ylim([ymin,ymax])
282
283 printLabels(ax, xlabel, ylabel, title)
284
285 imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
286 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
287 cb.set_label(cblabel)
288
289 # for tl in ax_cb.get_yticklabels():
290 # tl.set_visible(True)
291
292 for tick in ax.yaxis.get_major_ticks():
293 tick.label.set_fontsize(ticksize)
294
295 for tick in ax.xaxis.get_major_ticks():
296 tick.label.set_fontsize(ticksize)
297
298 for tick in cb.ax.get_yticklabels():
299 tick.set_fontsize(ticksize)
300
301 ax_cb.yaxis.tick_right()
302
303 if '0.' in matplotlib.__version__[0:2]:
304 print "The matplotlib version has to be updated to 1.1 or newer"
305 return imesh
306
307 if '1.0.' in matplotlib.__version__[0:4]:
308 print "The matplotlib version has to be updated to 1.1 or newer"
309 return imesh
310
311 matplotlib.pyplot.tight_layout()
312
313 if XAxisAsTime:
314
315 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
316 ax.xaxis.set_major_formatter(FuncFormatter(func))
317 ax.xaxis.set_major_locator(LinearLocator(7))
318
319 return imesh
320
321 def pcolor(imesh, z, xlabel='', ylabel='', title=''):
322
323 z = z.T
324
325 ax = imesh.get_axes()
326
327 printLabels(ax, xlabel, ylabel, title)
328
329 imesh.set_array(z.ravel())
330
331 def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
332
333 printLabels(ax, xlabel, ylabel, title)
334
335 imesh = ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
336 360
337 361 def draw(fig):
338 362
339 363 if type(fig) == 'int':
340 364 raise ValueError, "This parameter should be of tpye matplotlib figure"
341 365
342 366 fig.canvas.draw() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now