##// 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,7 +1,7
1 import numpy
1 import numpy
2 import datetime
2 import datetime
3 import matplotlib
3 import matplotlib
4 matplotlib.use("TKAgg")
4 matplotlib.use("GTKAgg")
5 import matplotlib.pyplot
5 import matplotlib.pyplot
6 import matplotlib.dates
6 import matplotlib.dates
7 #import scitools.numpyutils
7 #import scitools.numpyutils
@@ -22,6 +22,7 def createFigure(idfigure, wintitle, width, height, facecolor="w"):
22 fig.canvas.manager.set_window_title(wintitle)
22 fig.canvas.manager.set_window_title(wintitle)
23 fig.canvas.manager.resize(width, height)
23 fig.canvas.manager.resize(width, height)
24 matplotlib.pyplot.ion()
24 matplotlib.pyplot.ion()
25 matplotlib.pyplot.show()
25
26
26 return fig
27 return fig
27
28
@@ -33,7 +34,10 def closeFigure():
33 return
34 return
34
35
35 def saveFigure(fig, filename):
36 def saveFigure(fig, filename):
37
38 matplotlib.pyplot.ioff()
36 fig.savefig(filename)
39 fig.savefig(filename)
40 matplotlib.pyplot.ion()
37
41
38 def setWinTitle(fig, title):
42 def setWinTitle(fig, title):
39
43
@@ -45,11 +49,14 def setTitle(fig, title):
45
49
46 def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan):
50 def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan):
47
51
52 matplotlib.pyplot.ioff()
48 matplotlib.pyplot.figure(fig.number)
53 matplotlib.pyplot.figure(fig.number)
49 axes = matplotlib.pyplot.subplot2grid((nrow, ncol),
54 axes = matplotlib.pyplot.subplot2grid((nrow, ncol),
50 (xpos, ypos),
55 (xpos, ypos),
51 colspan=colspan,
56 colspan=colspan,
52 rowspan=rowspan)
57 rowspan=rowspan)
58
59 matplotlib.pyplot.ion()
53 return axes
60 return axes
54
61
55 def setAxesText(ax, text):
62 def setAxesText(ax, text):
@@ -78,7 +85,8 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title=''
78 grid : None, 'both', 'x', 'y'
85 grid : None, 'both', 'x', 'y'
79 """
86 """
80
87
81 ax.plot(x, y)
88 matplotlib.pyplot.ioff()
89
82 ax.set_xlim([xmin,xmax])
90 ax.set_xlim([xmin,xmax])
83 ax.set_ylim([ymin,ymax])
91 ax.set_ylim([ymin,ymax])
84
92
@@ -106,6 +114,7 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title=''
106 for tick in ax.yaxis.get_major_ticks():
114 for tick in ax.yaxis.get_major_ticks():
107 tick.label.set_fontsize(ticksize)
115 tick.label.set_fontsize(ticksize)
108
116
117 ax.plot(x, y)
109 iplot = ax.lines[-1]
118 iplot = ax.lines[-1]
110
119
111 ######################################################
120 ######################################################
@@ -122,8 +131,14 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title=''
122
131
123 matplotlib.pyplot.tight_layout()
132 matplotlib.pyplot.tight_layout()
124
133
134 matplotlib.pyplot.ion()
135
125 return iplot
136 return iplot
126
137
138 def set_linedata(ax, x, y, idline):
139
140 ax.lines[idline].set_data(x,y)
141
127 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
142 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
128
143
129 ax = iplot.get_axes()
144 ax = iplot.get_axes()
@@ -133,11 +148,79 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
133 set_linedata(ax, x, y, idline=0)
148 set_linedata(ax, x, y, idline=0)
134
149
135 def addpline(ax, x, y, color, linestyle, lw):
150 def addpline(ax, x, y, color, linestyle, lw):
151
136 ax.plot(x,y,color=color,linestyle=linestyle,lw=lw)
152 ax.plot(x,y,color=color,linestyle=linestyle,lw=lw)
137
153
138
154
139 def set_linedata(ax, x, y, idline):
155 def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax,
140 ax.lines[idline].set_data(x,y)
156 xlabel='', ylabel='', title='', ticksize = 9,
157 colormap='jet',cblabel='', cbsize="5%",
158 XAxisAsTime=False):
159
160 matplotlib.pyplot.ioff()
161
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 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
225 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
143 ticksize=9, xtick_visible=True, ytick_visible=True,
226 ticksize=9, xtick_visible=True, ytick_visible=True,
@@ -150,6 +233,8 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', tit
150 grid : None, 'both', 'x', 'y'
233 grid : None, 'both', 'x', 'y'
151 """
234 """
152
235
236 matplotlib.pyplot.ioff()
237
153 lines = ax.plot(x.T, y)
238 lines = ax.plot(x.T, y)
154 leg = ax.legend(lines, legendlabels, loc='upper right')
239 leg = ax.legend(lines, legendlabels, loc='upper right')
155 leg.get_frame().set_alpha(0.5)
240 leg.get_frame().set_alpha(0.5)
@@ -187,6 +272,8 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', tit
187
272
188 matplotlib.pyplot.tight_layout()
273 matplotlib.pyplot.tight_layout()
189
274
275 matplotlib.pyplot.ion()
276
190 return iplot
277 return iplot
191
278
192
279
@@ -211,6 +298,8 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel=''
211 grid : None, 'both', 'x', 'y'
298 grid : None, 'both', 'x', 'y'
212 """
299 """
213
300
301 matplotlib.pyplot.ioff()
302
214 lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle)
303 lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle)
215 leg = ax.legend(lines, legendlabels, bbox_to_anchor=(1.05, 1), loc='upper right', numpoints=1, handlelength=1.5, \
304 leg = ax.legend(lines, legendlabels, bbox_to_anchor=(1.05, 1), loc='upper right', numpoints=1, handlelength=1.5, \
216 handletextpad=0.5, borderpad=0.2, labelspacing=0.2, borderaxespad=0.)
305 handletextpad=0.5, borderpad=0.2, labelspacing=0.2, borderaxespad=0.)
@@ -255,6 +344,8 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel=''
255 ax.xaxis.set_major_formatter(FuncFormatter(func))
344 ax.xaxis.set_major_formatter(FuncFormatter(func))
256 ax.xaxis.set_major_locator(LinearLocator(7))
345 ax.xaxis.set_major_locator(LinearLocator(7))
257
346
347 matplotlib.pyplot.ion()
348
258 return iplot
349 return iplot
259
350
260 def pmultilineinyaxis(iplot, x, y, xlabel='', ylabel='', title=''):
351 def pmultilineinyaxis(iplot, x, y, xlabel='', ylabel='', title=''):
@@ -267,73 +358,6 def pmultilineinyaxis(iplot, x, y, xlabel='', ylabel='', title=''):
267 line = ax.lines[i]
358 line = ax.lines[i]
268 line.set_data(x,y[i,:])
359 line.set_data(x,y[i,:])
269
360
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
337 def draw(fig):
361 def draw(fig):
338
362
339 if type(fig) == 'int':
363 if type(fig) == 'int':
General Comments 0
You need to be logged in to leave comments. Login now