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