source: branches/alma/python/asaplotbase.py@ 3103

Last change on this file since 3103 was 1782, checked in by Kana Sugimoto, 14 years ago

New Development: Yes

JIRA Issue: No (merge)

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: No

Module(s): single dish package

Description:

Merged some unimplemented developments from the trunk.
[Details]

  1. Restored PKSmsg.cc, PKSmsg.h to alma branch from trunk
  2. Updated pks_matchs.cc and src/SConscript to ones in trunk
  3. uncommented "@print_log_dec"
  4. implemented bug fixes to add(), sub(), mul(), and div() in scantable.py
  5. implemented get_region() and get_point() in asaplotbase.py
  6. activated interactive keyword for text(), arrow(), annotate(), axvline(), axhline(), axvspan(), and axhspan() in asapplotter.py


  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.2 KB
RevLine 
[705]1"""
2ASAP plotting class based on matplotlib.
3"""
4
5import sys
6from re import match
7
8import matplotlib
9
10from matplotlib.figure import Figure, Text
[1147]11from matplotlib.font_manager import FontProperties as FP
[1757]12from numpy import sqrt
[705]13from matplotlib import rc, rcParams
[710]14from asap import rcParams as asaprcParams
[1259]15from matplotlib.ticker import OldScalarFormatter
[1147]16
[1603]17# API change in mpl >= 0.98
18try:
19 from matplotlib.transforms import blended_transform_factory
20except ImportError:
[1757]21 from matplotlib.transforms import blend_xy_sep_transform as blended_transform_factory
[1603]22
[1614]23from asap import asaplog
[1612]24
[1095]25if int(matplotlib.__version__.split(".")[1]) < 87:
[1612]26 #print "Warning: matplotlib version < 0.87. This might cause errors. Please upgrade."
[1614]27 asaplog.push( "matplotlib version < 0.87. This might cause errors. Please upgrade." )
28 print_log( 'WARN' )
[1019]29
[705]30class asaplotbase:
31 """
32 ASAP plotting base class based on matplotlib.
33 """
34
[1757]35 def __init__(self, rows=1, cols=0, title='', size=None, buffering=False):
[1019]36 """
37 Create a new instance of the ASAPlot plotting class.
[705]38
[1019]39 If rows < 1 then a separate call to set_panels() is required to define
40 the panel layout; refer to the doctext for set_panels().
41 """
[705]42 self.is_dead = False
[1019]43 self.figure = Figure(figsize=size, facecolor='#ddddee')
[705]44 self.canvas = None
45
[1019]46 self.set_title(title)
47 self.subplots = []
48 if rows > 0:
49 self.set_panels(rows, cols)
[705]50
[710]51 # Set matplotlib default colour sequence.
52 self.colormap = "green red black cyan magenta orange blue purple yellow pink".split()
[1019]53
[710]54 c = asaprcParams['plotter.colours']
55 if isinstance(c,str) and len(c) > 0:
56 self.colormap = c.split()
57
58 self.lsalias = {"line": [1,0],
59 "dashdot": [4,2,1,2],
60 "dashed" : [4,2,4,2],
61 "dotted" : [1,2],
62 "dashdotdot": [4,2,1,2,1,2],
63 "dashdashdot": [4,2,4,2,1,2]
64 }
65
66 styles = "line dashed dotted dashdot".split()
67 c = asaprcParams['plotter.linestyles']
68 if isinstance(c,str) and len(c) > 0:
69 styles = c.split()
70 s = []
71 for ls in styles:
72 if self.lsalias.has_key(ls):
73 s.append(self.lsalias.get(ls))
74 else:
75 s.append('-')
76 self.linestyles = s
77
[705]78 self.color = 0;
[710]79 self.linestyle = 0;
[1019]80 self.attributes = {}
81 self.loc = 0
[705]82
[1019]83 self.buffering = buffering
[705]84
85 def clear(self):
[1019]86 """
[1147]87 Delete all lines from the plot. Line numbering will restart from 0.
[1019]88 """
[705]89
[1019]90 for i in range(len(self.lines)):
91 self.delete(i)
92 self.axes.clear()
93 self.color = 0
94 self.lines = []
[705]95
[710]96 def palette(self, color, colormap=None, linestyle=0, linestyles=None):
[705]97 if colormap:
[710]98 if isinstance(colormap,list):
99 self.colormap = colormap
100 elif isinstance(colormap,str):
101 self.colormap = colormap.split()
[705]102 if 0 <= color < len(self.colormap):
103 self.color = color
[710]104 if linestyles:
105 self.linestyles = []
106 if isinstance(linestyles,list):
107 styles = linestyles
108 elif isinstance(linestyles,str):
109 styles = linestyles.split()
110 for ls in styles:
111 if self.lsalias.has_key(ls):
112 self.linestyles.append(self.lsalias.get(ls))
113 else:
114 self.linestyles.append(self.lsalias.get('line'))
115 if 0 <= linestyle < len(self.linestyles):
116 self.linestyle = linestyle
[705]117
118 def delete(self, numbers=None):
[1019]119 """
120 Delete the 0-relative line number, default is to delete the last.
121 The remaining lines are NOT renumbered.
122 """
[705]123
[1019]124 if numbers is None: numbers = [len(self.lines)-1]
[705]125
[1019]126 if not hasattr(numbers, '__iter__'):
127 numbers = [numbers]
[705]128
[1019]129 for number in numbers:
130 if 0 <= number < len(self.lines):
131 if self.lines[number] is not None:
132 for line in self.lines[number]:
133 line.set_linestyle('None')
134 self.lines[number] = None
135 self.show()
[705]136
137 def get_line(self):
[1019]138 """
139 Get the current default line attributes.
140 """
141 return self.attributes
[705]142
143
[1086]144 def hist(self, x=None, y=None, fmt=None, add=None):
[1019]145 """
146 Plot a histogram. N.B. the x values refer to the start of the
147 histogram bin.
[705]148
[1019]149 fmt is the line style as in plot().
150 """
[1757]151 from numpy import array
152 from numpy.ma import MaskedArray
[1019]153 if x is None:
154 if y is None: return
[1023]155 x = range(len(y))
[705]156
[1019]157 if len(x) != len(y):
158 return
159 l2 = 2*len(x)
[1023]160 x2 = range(l2)
[1086]161 y2 = range(12)
[1023]162 y2 = range(l2)
163 m2 = range(l2)
[1603]164 ymsk = None
165 ydat = None
166 if hasattr(y, "raw_mask"):
167 # numpy < 1.1
168 ymsk = y.raw_mask()
169 ydat = y.raw_data()
170 else:
171 ymsk = y.mask
172 ydat = y.data
[1023]173 for i in range(l2):
[1019]174 x2[i] = x[i/2]
[1086]175 m2[i] = ymsk[i/2]
[705]176
[1023]177 y2[0] = 0.0
[1019]178 for i in range(1,l2):
[1086]179 y2[i] = ydat[(i-1)/2]
[705]180
[1086]181 self.plot(x2, MaskedArray(y2,mask=m2,copy=0), fmt, add)
[705]182
183
184 def hold(self, hold=True):
[1019]185 """
186 Buffer graphics until subsequently released.
187 """
188 self.buffering = hold
[705]189
190
191 def legend(self, loc=None):
[1019]192 """
193 Add a legend to the plot.
[705]194
[1019]195 Any other value for loc else disables the legend:
196 1: upper right
197 2: upper left
198 3: lower left
199 4: lower right
200 5: right
201 6: center left
202 7: center right
203 8: lower center
204 9: upper center
205 10: center
[705]206
[1019]207 """
[1095]208 if isinstance(loc, int):
[1098]209 self.loc = None
210 if 0 <= loc <= 10: self.loc = loc
[1095]211 else:
212 self.loc = None
213 #self.show()
[705]214
215
[1086]216 def plot(self, x=None, y=None, fmt=None, add=None):
[1019]217 """
218 Plot the next line in the current frame using the current line
219 attributes. The ASAPlot graphics window will be mapped and raised.
[705]220
[1019]221 The argument list works a bit like the matlab plot() function.
222 """
223 if x is None:
224 if y is None: return
225 x = range(len(y))
[705]226
[1019]227 elif y is None:
228 y = x
229 x = range(len(y))
[1086]230 if fmt is None:
231 line = self.axes.plot(x, y)
[1019]232 else:
[1086]233 line = self.axes.plot(x, y, fmt)
[705]234
[1019]235 # Add to an existing line?
[1086]236 i = None
[1019]237 if add is None or len(self.lines) < add < 0:
238 # Don't add.
239 self.lines.append(line)
240 i = len(self.lines) - 1
241 else:
242 if add == 0: add = len(self.lines)
243 i = add - 1
244 self.lines[i].extend(line)
[705]245
[1019]246 # Set/reset attributes for the line.
247 gotcolour = False
248 for k, v in self.attributes.iteritems():
249 if k == 'color': gotcolour = True
250 for segment in self.lines[i]:
251 getattr(segment, "set_%s"%k)(v)
[705]252
[1019]253 if not gotcolour and len(self.colormap):
254 for segment in self.lines[i]:
255 getattr(segment, "set_color")(self.colormap[self.color])
[710]256 if len(self.colormap) == 1:
257 getattr(segment, "set_dashes")(self.linestyles[self.linestyle])
[1086]258
[1019]259 self.color += 1
260 if self.color >= len(self.colormap):
261 self.color = 0
[705]262
[710]263 if len(self.colormap) == 1:
264 self.linestyle += 1
[1019]265 if self.linestyle >= len(self.linestyles):
266 self.linestyle = 0
[710]267
[1019]268 self.show()
[705]269
270
271 def position(self):
[1019]272 """
273 Use the mouse to get a position from a graph.
274 """
[705]275
[1019]276 def position_disable(event):
277 self.register('button_press', None)
278 print '%.4f, %.4f' % (event.xdata, event.ydata)
[705]279
[1019]280 print 'Press any mouse button...'
281 self.register('button_press', position_disable)
[705]282
283
[1782]284 def get_region(self):
285 pos = []
286 print "Please select the bottom/left point"
287 pos.append(self.figure.ginput(n=1, show_clicks=False)[0])
288 print "Please select the top/right point"
289 pos.append(self.figure.ginput(n=1, show_clicks=False)[0])
290 return pos
[1757]291
[1782]292 def get_point(self):
293 print "Please select the point"
294 pt = self.figure.ginput(n=1, show_clicks=False)
295 if pt:
296 return pt[0]
297 else:
298 return None
[1757]299
[705]300 def region(self):
[1019]301 """
302 Use the mouse to get a rectangular region from a plot.
[705]303
[1019]304 The return value is [x0, y0, x1, y1] in world coordinates.
305 """
[705]306
[1019]307 def region_start(event):
[1620]308 self.rect = {'x': event.x, 'y': event.y,
[1019]309 'world': [event.xdata, event.ydata,
310 event.xdata, event.ydata]}
311 self.register('button_press', None)
312 self.register('motion_notify', region_draw)
313 self.register('button_release', region_disable)
[705]314
[1019]315 def region_draw(event):
[1620]316 self.figmgr.toolbar.draw_rubberband(event, event.x, event.y,
317 self.rect['x'], self.rect['y'])
318
[1019]319 def region_disable(event):
320 self.register('motion_notify', None)
321 self.register('button_release', None)
[705]322
[1019]323 self.rect['world'][2:4] = [event.xdata, event.ydata]
324 print '(%.2f, %.2f) (%.2f, %.2f)' % (self.rect['world'][0],
325 self.rect['world'][1], self.rect['world'][2],
326 self.rect['world'][3])
[1620]327 self.figmgr.toolbar.release(event)
[705]328
[1019]329 self.register('button_press', region_start)
[705]330
[1019]331 # This has to be modified to block and return the result (currently
332 # printed by region_disable) when that becomes possible in matplotlib.
[705]333
[1019]334 return [0.0, 0.0, 0.0, 0.0]
[705]335
336
337 def register(self, type=None, func=None):
[1019]338 """
339 Register, reregister, or deregister events of type 'button_press',
340 'button_release', or 'motion_notify'.
[705]341
[1019]342 The specified callback function should have the following signature:
[705]343
[1019]344 def func(event)
[705]345
[1019]346 where event is an MplEvent instance containing the following data:
[705]347
[1019]348 name # Event name.
349 canvas # FigureCanvas instance generating the event.
350 x = None # x position - pixels from left of canvas.
351 y = None # y position - pixels from bottom of canvas.
352 button = None # Button pressed: None, 1, 2, 3.
353 key = None # Key pressed: None, chr(range(255)), shift,
354 win, or control
355 inaxes = None # Axes instance if cursor within axes.
356 xdata = None # x world coordinate.
357 ydata = None # y world coordinate.
[705]358
[1019]359 For example:
[705]360
[1019]361 def mouse_move(event):
362 print event.xdata, event.ydata
[705]363
[1019]364 a = asaplot()
365 a.register('motion_notify', mouse_move)
[705]366
[1019]367 If func is None, the event is deregistered.
[705]368
[1019]369 Note that in TkAgg keyboard button presses don't generate an event.
370 """
[705]371
[1019]372 if not self.events.has_key(type): return
[705]373
[1019]374 if func is None:
375 if self.events[type] is not None:
376 # It's not clear that this does anything.
377 self.canvas.mpl_disconnect(self.events[type])
378 self.events[type] = None
[705]379
[1766]380 # It seems to be necessary to return events to the toolbar. <-- Not ture. 2010.Jul.14.kana.
381 #if type == 'motion_notify':
382 # self.canvas.mpl_connect(type + '_event',
383 # self.figmgr.toolbar.mouse_move)
384 #elif type == 'button_press':
385 # self.canvas.mpl_connect(type + '_event',
386 # self.figmgr.toolbar.press)
387 #elif type == 'button_release':
388 # self.canvas.mpl_connect(type + '_event',
389 # self.figmgr.toolbar.release)
[705]390
[1019]391 else:
392 self.events[type] = self.canvas.mpl_connect(type + '_event', func)
[705]393
394
395 def release(self):
[1019]396 """
397 Release buffered graphics.
398 """
399 self.buffering = False
400 self.show()
[705]401
402
[1095]403 def save(self, fname=None, orientation=None, dpi=None, papertype=None):
[1019]404 """
405 Save the plot to a file.
[705]406
[1019]407 fname is the name of the output file. The image format is determined
408 from the file suffix; 'png', 'ps', and 'eps' are recognized. If no
409 file name is specified 'yyyymmdd_hhmmss.png' is created in the current
410 directory.
411 """
[1095]412 from asap import rcParams
413 if papertype is None:
414 papertype = rcParams['plotter.papertype']
[1019]415 if fname is None:
416 from datetime import datetime
417 dstr = datetime.now().strftime('%Y%m%d_%H%M%S')
418 fname = 'asap'+dstr+'.png'
[705]419
[1019]420 d = ['png','.ps','eps']
[705]421
[1019]422 from os.path import expandvars
423 fname = expandvars(fname)
[705]424
[1019]425 if fname[-3:].lower() in d:
426 try:
[705]427 if fname[-3:].lower() == ".ps":
[1020]428 from matplotlib import __version__ as mv
[1603]429 w = self.figure.get_figwidth()
430 h = self.figure.get_figheight()
[1019]431
[705]432 if orientation is None:
[1147]433 # oriented
[705]434 if w > h:
435 orientation = 'landscape'
436 else:
437 orientation = 'portrait'
[1095]438 from matplotlib.backends.backend_ps import papersize
439 pw,ph = papersize[papertype.lower()]
[1025]440 ds = None
441 if orientation == 'landscape':
[1095]442 ds = min(ph/w, pw/h)
[1025]443 else:
[1095]444 ds = min(pw/w, ph/h)
[1025]445 ow = ds * w
446 oh = ds * h
[1603]447 self.figure.set_size_inches((ow, oh))
[1095]448 self.figure.savefig(fname, orientation=orientation,
449 papertype=papertype.lower())
[1603]450 self.figure.set_size_inches((w, h))
[705]451 print 'Written file %s' % (fname)
[1019]452 else:
[705]453 if dpi is None:
454 dpi =150
[1025]455 self.figure.savefig(fname,dpi=dpi)
[705]456 print 'Written file %s' % (fname)
[1019]457 except IOError, msg:
[1612]458 #print 'Failed to save %s: Error msg was\n\n%s' % (fname, err)
[1614]459 print_log()
[1676]460 asaplog.push('Failed to save %s: Error msg was\n\n%s' % (fname, str(msg)))
[1614]461 print_log( 'ERROR' )
[1019]462 return
463 else:
[1612]464 #print "Invalid image type. Valid types are:"
465 #print "'ps', 'eps', 'png'"
[1614]466 asaplog.push( "Invalid image type. Valid types are:" )
467 asaplog.push( "'ps', 'eps', 'png'" )
468 print_log('WARN')
[705]469
470
471 def set_axes(self, what=None, *args, **kwargs):
[1019]472 """
473 Set attributes for the axes by calling the relevant Axes.set_*()
474 method. Colour translation is done as described in the doctext
475 for palette().
476 """
[705]477
[1019]478 if what is None: return
479 if what[-6:] == 'colour': what = what[:-6] + 'color'
[705]480
[1153]481 key = "colour"
482 if kwargs.has_key(key):
483 val = kwargs.pop(key)
484 kwargs["color"] = val
[705]485
[1153]486 getattr(self.axes, "set_%s"%what)(*args, **kwargs)
[705]487
[1153]488 self.show(hardrefresh=False)
[705]489
[1019]490
[705]491 def set_figure(self, what=None, *args, **kwargs):
[1019]492 """
493 Set attributes for the figure by calling the relevant Figure.set_*()
494 method. Colour translation is done as described in the doctext
495 for palette().
496 """
[705]497
[1019]498 if what is None: return
499 if what[-6:] == 'colour': what = what[:-6] + 'color'
500 #if what[-5:] == 'color' and len(args):
501 # args = (get_colour(args[0]),)
[705]502
[1019]503 newargs = {}
504 for k, v in kwargs.iteritems():
505 k = k.lower()
506 if k == 'colour': k = 'color'
507 newargs[k] = v
[705]508
[1019]509 getattr(self.figure, "set_%s"%what)(*args, **newargs)
[1153]510 self.show(hardrefresh=False)
[705]511
512
513 def set_limits(self, xlim=None, ylim=None):
[1019]514 """
515 Set x-, and y-limits for each subplot.
[705]516
[1019]517 xlim = [xmin, xmax] as in axes.set_xlim().
518 ylim = [ymin, ymax] as in axes.set_ylim().
519 """
520 for s in self.subplots:
521 self.axes = s['axes']
522 self.lines = s['lines']
[705]523 oldxlim = list(self.axes.get_xlim())
524 oldylim = list(self.axes.get_ylim())
525 if xlim is not None:
526 for i in range(len(xlim)):
527 if xlim[i] is not None:
528 oldxlim[i] = xlim[i]
[1019]529 if ylim is not None:
[705]530 for i in range(len(ylim)):
531 if ylim[i] is not None:
532 oldylim[i] = ylim[i]
533 self.axes.set_xlim(oldxlim)
534 self.axes.set_ylim(oldylim)
535 return
536
537
538 def set_line(self, number=None, **kwargs):
[1019]539 """
540 Set attributes for the specified line, or else the next line(s)
541 to be plotted.
[705]542
[1019]543 number is the 0-relative number of a line that has already been
544 plotted. If no such line exists, attributes are recorded and used
545 for the next line(s) to be plotted.
[705]546
[1019]547 Keyword arguments specify Line2D attributes, e.g. color='r'. Do
[705]548
[1019]549 import matplotlib
550 help(matplotlib.lines)
[705]551
[1019]552 The set_* methods of class Line2D define the attribute names and
553 values. For non-US usage, "colour" is recognized as synonymous with
554 "color".
[705]555
[1019]556 Set the value to None to delete an attribute.
[705]557
[1019]558 Colour translation is done as described in the doctext for palette().
559 """
[705]560
[1019]561 redraw = False
562 for k, v in kwargs.iteritems():
563 k = k.lower()
564 if k == 'colour': k = 'color'
[705]565
[1019]566 if 0 <= number < len(self.lines):
567 if self.lines[number] is not None:
568 for line in self.lines[number]:
569 getattr(line, "set_%s"%k)(v)
570 redraw = True
571 else:
572 if v is None:
573 del self.attributes[k]
574 else:
575 self.attributes[k] = v
[705]576
[1153]577 if redraw: self.show(hardrefresh=False)
[705]578
579
[1723]580 #def set_panels(self, rows=1, cols=0, n=-1, nplots=-1, ganged=True):
581 def set_panels(self, rows=1, cols=0, n=-1, nplots=-1, layout=None,ganged=True):
[1019]582 """
583 Set the panel layout.
[705]584
[1019]585 rows and cols, if cols != 0, specify the number of rows and columns in
586 a regular layout. (Indexing of these panels in matplotlib is row-
587 major, i.e. column varies fastest.)
[705]588
[1019]589 cols == 0 is interpreted as a retangular layout that accomodates
590 'rows' panels, e.g. rows == 6, cols == 0 is equivalent to
591 rows == 2, cols == 3.
[705]592
[1019]593 0 <= n < rows*cols is interpreted as the 0-relative panel number in
594 the configuration specified by rows and cols to be added to the
595 current figure as its next 0-relative panel number (i). This allows
596 non-regular panel layouts to be constructed via multiple calls. Any
597 other value of n clears the plot and produces a rectangular array of
598 empty panels. The number of these may be limited by nplots.
599 """
600 if n < 0 and len(self.subplots):
601 self.figure.clear()
602 self.set_title()
[705]603
[1723]604 if layout:
605 lef, bot, rig, top, wsp, hsp = layout
606 self.figure.subplots_adjust(
607 left=lef,bottom=bot,right=rig,top=top,wspace=wsp,hspace=hsp)
608 del lef,bot,rig,top,wsp,hsp
609
[1019]610 if rows < 1: rows = 1
[705]611
[1019]612 if cols <= 0:
613 i = int(sqrt(rows))
614 if i*i < rows: i += 1
615 cols = i
[705]616
[1019]617 if i*(i-1) >= rows: i -= 1
618 rows = i
[705]619
[1019]620 if 0 <= n < rows*cols:
621 i = len(self.subplots)
[1723]622
[1019]623 self.subplots.append({})
[705]624
[1019]625 self.subplots[i]['axes'] = self.figure.add_subplot(rows,
626 cols, n+1)
627 self.subplots[i]['lines'] = []
[705]628
[1019]629 if i == 0: self.subplot(0)
[705]630
[1019]631 self.rows = 0
632 self.cols = 0
[705]633
[1019]634 else:
635 self.subplots = []
[705]636
[1019]637 if nplots < 1 or rows*cols < nplots:
638 nplots = rows*cols
[1025]639 if ganged:
640 hsp,wsp = None,None
641 if rows > 1: hsp = 0.0001
642 if cols > 1: wsp = 0.0001
643 self.figure.subplots_adjust(wspace=wsp,hspace=hsp)
[1019]644 for i in range(nplots):
645 self.subplots.append({})
[1153]646 self.subplots[i]['lines'] = []
647 if not ganged:
648 self.subplots[i]['axes'] = self.figure.add_subplot(rows,
[1019]649 cols, i+1)
[1757]650 if asaprcParams['plotter.axesformatting'] != 'mpl':
[1603]651 self.subplots[i]['axes'].xaxis.set_major_formatter(OldScalarFormatter())
[1153]652 else:
653 if i == 0:
654 self.subplots[i]['axes'] = self.figure.add_subplot(rows,
655 cols, i+1)
[1757]656 if asaprcParams['plotter.axesformatting'] != 'mpl':
[1603]657
658 self.subplots[i]['axes'].xaxis.set_major_formatter(OldScalarFormatter())
[1153]659 else:
660 self.subplots[i]['axes'] = self.figure.add_subplot(rows,
661 cols, i+1,
662 sharex=self.subplots[0]['axes'],
663 sharey=self.subplots[0]['axes'])
[1259]664
[705]665 # Suppress tick labelling for interior subplots.
666 if i <= (rows-1)*cols - 1:
667 if i+cols < nplots:
668 # Suppress x-labels for frames width
669 # adjacent frames
[1153]670 for tick in self.subplots[i]['axes'].xaxis.majorTicks:
671 tick.label1On = False
[1019]672 self.subplots[i]['axes'].xaxis.label.set_visible(False)
[705]673 if i%cols:
674 # Suppress y-labels for frames not in the left column.
675 for tick in self.subplots[i]['axes'].yaxis.majorTicks:
676 tick.label1On = False
677 self.subplots[i]['axes'].yaxis.label.set_visible(False)
[1025]678 # disable the first tick of [1:ncol-1] of the last row
[1153]679 #if i+1 < nplots:
680 # self.subplots[i]['axes'].xaxis.majorTicks[0].label1On = False
[1019]681 self.rows = rows
682 self.cols = cols
683 self.subplot(0)
[1723]684 del rows,cols,n,nplots,layout,ganged,i
[705]685
[1153]686 def tidy(self):
687 # this needs to be exceuted after the first "refresh"
688 nplots = len(self.subplots)
689 if nplots == 1: return
690 for i in xrange(nplots):
691 ax = self.subplots[i]['axes']
692 if i%self.cols:
693 ax.xaxis.majorTicks[0].label1On = False
694 else:
695 if i != 0:
696 ax.yaxis.majorTicks[-1].label1On = False
697
698
[705]699 def set_title(self, title=None):
[1019]700 """
701 Set the title of the plot window. Use the previous title if title is
702 omitted.
703 """
704 if title is not None:
705 self.title = title
[705]706
[1019]707 self.figure.text(0.5, 0.95, self.title, horizontalalignment='center')
[705]708
709
[1153]710 def show(self, hardrefresh=True):
[1019]711 """
712 Show graphics dependent on the current buffering state.
713 """
[1153]714 if not hardrefresh: return
[1019]715 if not self.buffering:
716 if self.loc is not None:
[1086]717 for sp in self.subplots:
[1019]718 lines = []
719 labels = []
720 i = 0
[1086]721 for line in sp['lines']:
[1019]722 i += 1
723 if line is not None:
724 lines.append(line[0])
725 lbl = line[0].get_label()
726 if lbl == '':
727 lbl = str(i)
728 labels.append(lbl)
[705]729
[1019]730 if len(lines):
[1147]731 fp = FP(size=rcParams['legend.fontsize'])
732 fsz = fp.get_size_in_points() - len(lines)
733 fp.set_size(max(fsz,6))
[1086]734 sp['axes'].legend(tuple(lines), tuple(labels),
[1147]735 self.loc, prop=fp)
[1019]736 else:
[1086]737 sp['axes'].legend((' '))
[705]738
[1086]739 from matplotlib.artist import setp
[1757]740 fpx = FP(size=rcParams['xtick.labelsize'])
741 xts = fpx.get_size_in_points()- (self.cols)/2
742 fpy = FP(size=rcParams['ytick.labelsize'])
743 yts = fpy.get_size_in_points() - (self.rows)/2
744 fpa = FP(size=rcParams['axes.labelsize'])
745 fpat = FP(size=rcParams['axes.titlesize'])
746 axsize = fpa.get_size_in_points()
747 tsize = fpat.get_size_in_points()-(self.cols)/2
[1086]748 for sp in self.subplots:
749 ax = sp['axes']
[1603]750 ax.title.set_size(tsize)
[1086]751 setp(ax.get_xticklabels(), fontsize=xts)
752 setp(ax.get_yticklabels(), fontsize=yts)
753 off = 0
754 if self.cols > 1: off = self.cols
[1757]755 ax.xaxis.label.set_size(axsize-off)
[1086]756 off = 0
757 if self.rows > 1: off = self.rows
[1757]758 ax.yaxis.label.set_size(axsize-off)
[705]759
760 def subplot(self, i=None, inc=None):
[1019]761 """
762 Set the subplot to the 0-relative panel number as defined by one or
763 more invokations of set_panels().
764 """
765 l = len(self.subplots)
766 if l:
767 if i is not None:
768 self.i = i
[705]769
[1019]770 if inc is not None:
771 self.i += inc
[705]772
[1019]773 self.i %= l
774 self.axes = self.subplots[self.i]['axes']
775 self.lines = self.subplots[self.i]['lines']
[705]776
777 def text(self, *args, **kwargs):
[1019]778 """
779 Add text to the figure.
780 """
781 self.figure.text(*args, **kwargs)
782 self.show()
[1147]783
784 def vline_with_label(self, x, y, label,
785 location='bottom', rotate=0.0, **kwargs):
786 """
787 Plot a vertical line with label.
788 It takes "world" values fo x and y.
789 """
790 ax = self.axes
791 # need this to suppress autoscaling during this function
792 self.axes.set_autoscale_on(False)
793 ymin = 0.0
794 ymax = 1.0
795 valign = 'center'
796 if location.lower() == 'top':
797 y = max(0.0, y)
798 elif location.lower() == 'bottom':
799 y = min(0.0, y)
800 lbloffset = 0.06
801 # a rough estimate for the bb of the text
802 if rotate > 0.0: lbloffset = 0.03*len(label)
803 peakoffset = 0.01
[1603]804 xy = None
805 xy0 = None
806 # matplotlib api change 0.98 is using transform now
807 if hasattr(ax.transData, "inverse_xy_tup"):
808 # get relative coords
809 xy0 = ax.transData.xy_tup((x,y))
810 xy = ax.transAxes.inverse_xy_tup(xy0)
811 else:
812 xy0 = ax.transData.transform((x,y))
813 # get relative coords
814 xy = ax.transAxes.inverted().transform(xy0)
[1147]815 if location.lower() == 'top':
816 ymax = 1.0-lbloffset
817 ymin = xy[1]+peakoffset
818 valign = 'bottom'
819 ylbl = ymax+0.01
820 elif location.lower() == 'bottom':
821 ymin = lbloffset
822 ymax = xy[1]-peakoffset
823 valign = 'top'
824 ylbl = ymin-0.01
[1603]825 trans = blended_transform_factory(ax.transData, ax.transAxes)
[1147]826 l = ax.axvline(x, ymin, ymax, color='black', **kwargs)
827 t = ax.text(x, ylbl ,label, verticalalignment=valign,
828 horizontalalignment='center',
829 rotation=rotate,transform = trans)
830 self.axes.set_autoscale_on(True)
Note: See TracBrowser for help on using the repository browser.