source: trunk/python/plotter2.py@ 2886

Last change on this file since 2886 was 2861, checked in by WataruKawasaki, 11 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: Yes

Module(s): sd

Description: (1) bugfix in setting masks

(2) finished implementation of get_vinfo()
(3) change get_*index() to class methods
(4) modify get_*index() to raise exception when invalid value given


File size: 26.3 KB
Line 
1from os.path import expanduser, expandvars
2from asap._asap import Plotter2
3
4class plotter2:
5 def __init__(self):
6 self._plotter = Plotter2()
7
8 def set_output(self, filename=None, dev=None):
9 """\
10 set output filename and/or output device.
11 when only filename given, filename is splitted with '.' then
12 the last part (filename extension) will be used as output device.
13 Parameters:
14 filename: output file name (default is '')
15 device: output device. default is 'xwindow'. 'png', 'ps'
16 and 'vps'(PostScript in portrait shape) are
17 available also.
18
19 Example:
20 set_output() -- display in X Window (default)
21 set_output('foo.png') -- generate foo.png in PNG format.
22 set_output('bar.ps', 'vps') -- generate bar.ps in portrait shape.
23 """
24 if filename is None:
25 filename = ''
26 dev = 'xwindow'
27 else:
28 filename = filename.strip()
29 if filename == '':
30 dev = 'xwindow'
31 else:
32 if (dev is None) or (dev.strip() == ''):
33 fnamelem = filename.lower().split('.')
34 dev = fnamelem[len(fnamelem)-1].strip()
35 if dev == 'ps': dev = 'cps'
36
37 self._plotter.set_filename(expanduser(expandvars(filename)))
38 self._plotter.set_device(dev.strip())
39
40 def set_vp(self, xmin, xmax, ymin, ymax, id=None):
41 """\
42 change the position/shape of viewport in which data are to be plotted.
43 positions should be given in window coordinate (0<={x,y}<=1), but,
44 unlike the usual definition of window coordinates, with the origin at
45 the bottom left corner.
46 when you create plotter2 instance, it already has a single viewport
47 with xmin=ymin=0.1 and xmax=ymax=0.9 (id=0). in case you don't want to
48 change the size/position of the viewport, you never need to run this
49 function.
50 if you want to have two or more viewports, run set_vp() first to
51 move/resize the first viewport, then you will be able to add a new
52 viewport by executing set_vp without id (the id of the new viewport
53 is returned in this case). the location and shape of any viewport
54 can be modified later by specifying its viewport id.
55
56 Parameters:
57 xmin: the x position of the left side.
58 xmax: the x position of the rightt side.
59 ymin: the y position of the top of viewport.
60 ymax: the y position of the bottom of viewport.
61 id: viewport id
62 """
63 if id is None:
64 if self._plotter.get_hasdefaultvp():
65 self._plotter.set_viewport(xmin, xmax, ymin, ymax, 0)
66 else:
67 return self._plotter.add_viewport(xmin, xmax, ymin, ymax)
68 else:
69 self._plotter.set_viewport(xmin, xmax, ymin, ymax, id)
70
71 def show_vp(self, vpid=None):
72 """\
73 show viewport specified with its id.
74
75 Parameter:
76 vpid: viewport id. when unset, operation is applied to
77 the viewport with the largest id.
78 """
79 if vpid is None: vpid = -1
80 self._plotter.show_viewport(vpid)
81
82 def hide_vp(self, vpid=None):
83 """\
84 hide viewport specified with its id.
85
86 Parameter:
87 vpid: viewport id. when unset, operation is applied to
88 the viewport with the largest id.
89 """
90 if vpid is None: vpid = -1
91 self._plotter.hide_viewport(vpid)
92
93 def set_range(self, xmin, xmax, ymin, ymax, vpid=None):
94 """\
95 set 2D range to be displayed in the specified viewport.
96 by default, the display range is set automatically.
97
98 Parameter:
99 xmin: the minimum of x range.
100 xmax: the maximum of x range.
101 ymin: the minimum of y range.
102 ymax: the maximum of y range.
103 vpid: viewport id. when unset, operation is applied to
104 the viewport with the largest id.
105 """
106 if vpid is None: vpid = -1
107 self._plotter.set_range(xmin, xmax, ymin, ymax, vpid)
108
109 def set_xrange(self, xmin, xmax, vpid=None):
110 """\
111 set x range to be displayed in the specified viewport.
112 by default, the display range is set automatically.
113
114 Parameter:
115 xmin: the minimum of x range.
116 xmax: the maximum of x range.
117 vpid: viewport id. when unset, operation is applied to
118 the viewport with the largest id.
119 """
120 if vpid is None: vpid = -1
121 self._plotter.set_range_x(xmin, xmax, vpid)
122
123 def set_yrange(self, ymin, ymax, vpid=None):
124 """\
125 set y range to be displayed in the specified viewport.
126 by default, the display range is set automatically.
127
128 Parameter:
129 ymin: the minimum of y range.
130 ymax: the maximum of y range.
131 vpid: viewport id. when unset, operation is applied to
132 the viewport with the largest id.
133 """
134 if vpid is None: vpid = -1
135 self._plotter.set_range_y(ymin, ymax, vpid)
136
137 def get_xrange(self, vpid=None):
138 """\
139 returns x range of displayed region of the specified viewport
140 as a list of [xmin, xmax].
141
142 Parameter:
143 vpid: viewport id. when unset, operation is applied to
144 the viewport with the largest id.
145 """
146 if vpid is None: vpid = -1
147 return self._plotter.get_range_x(vpid)
148
149 def get_yrange(self, vpid=None):
150 """\
151 returns y range of displayed region in the specified viewport
152 as a list of [ymin, ymax].
153
154 Parameter:
155 vpid: viewport id. when unset, operation is applied to
156 the viewport with the largest id.
157 """
158 if vpid is None: vpid = -1
159 return self._plotter.get_range_y(vpid)
160
161 def set_autorange(self, vpid=None):
162 """\
163 set the 2-Dimensional range of displayed area of the specified
164 viewport to automatically enclose the given data.
165 the x range will be from the minimum up to the maximum value
166 of the given x values, whereas y range has margins of 10% of
167 the y value range both at the top and the bottom sides.
168
169 Parameter:
170 vpid: viewport id. when unset, operation is applied to
171 the viewport with the largest id.
172 """
173 if vpid is None: vpid = -1
174 self._plotter.set_autorange(vpid)
175
176 def set_xautorange(self, vpid=None):
177 """\
178 set x range of displayed area of the specified viewport to
179 automatically enclose the given data with no margins.
180
181 Parameter:
182 vpid: viewport id. when unset, operation is applied to
183 the viewport with the largest id.
184 """
185 if vpid is None: vpid = -1
186 self._plotter.set_autorange_x(vpid)
187
188 def set_yautorange(self, vpid=None):
189 """\
190 set y range of displayed area of the specified viewport to
191 automatically enclose the given data with margins of 10% of
192 the y value range both at the top and the bottom sides.
193
194 Parameter:
195 vpid: viewport id. when unset, operation is applied to
196 the viewport with the largest id.
197 """
198 if vpid is None: vpid = -1
199 self._plotter.set_autorange_y(vpid)
200
201 def set_fontsize(self, size, vpid=None):
202 """\
203 set the font size that is used for number labels etc.
204
205 Parameter:
206 size: font size (default is 1)
207 vpid: viewport id. when unset, operation is applied to
208 the viewport with the largest id.
209 """
210 if vpid is None: vpid = -1
211 self._plotter.set_fontsize(size, vpid)
212
213 def set_xtics(self, interval_major, num_minor=None, vpid=None):
214 """\
215 set the interval of ticks along x axis.
216
217 Parameter:
218 interval_major: interval of major ticks
219 num_minor: major tick interval / minor tick interval.
220 default is 5.
221 vpid: viewport id. when unset, operation is
222 applied to the viewport with the largest id.
223 """
224 if vpid is None: vpid = -1
225 if num_minor is None: num_minor = 5
226 self._plotter.set_tics_x(interval_major, num_minor, vpid)
227
228 def set_ytics(self, interval_major, num_minor=None, vpid=None):
229 """\
230 set the interval of ticks along y axis.
231
232 Parameter:
233 interval_major: interval of major ticks
234 num_minor: major tick interval / minor tick interval.
235 default is 5.
236 vpid: viewport id. when unset, operation is
237 applied to the viewport with the largest id.
238 """
239 if vpid is None: vpid = -1
240 if num_minor is None: num_minor = 5
241 self._plotter.set_tics_y(interval_major, num_minor, vpid)
242
243 def set_xautotics(self, vpid=None):
244 """\
245 set the interval of ticks and number labels along x axis
246 given automatically.
247
248 Parameter:
249 vpid: viewport id. when unset, operation is applied to
250 the viewport with the largest id.
251 """
252 if vpid is None: vpid = -1
253 self._plotter.set_autotics_x(vpid)
254
255 def set_yautotics(self, vpid=None):
256 """\
257 set the interval of ticks and number labels along y axis
258 given automatically.
259
260 Parameter:
261 vpid: viewport id. when unset, operation is applied to
262 the viewport with the largest id.
263 """
264 if vpid is None: vpid = -1
265 self._plotter.set_autotics_y(vpid)
266
267 def set_xnuminterval(self, interval, vpid=None):
268 """\
269 set the interval of number labels along x axis.
270
271 Parameter:
272 interval: interval of number labels
273 vpid: viewport id. when unset, operation is
274 applied to the viewport with the largest id.
275 """
276 if vpid is None: vpid = -1
277 self._plotter.set_ninterval_x(interval, vpid)
278
279 def set_ynuminterval(self, interval, vpid=None):
280 """\
281 set the interval of number labels along y axis.
282
283 Parameter:
284 interval: interval of number labels
285 vpid: viewport id. when unset, operation is
286 applied to the viewport with the largest id.
287 """
288 if vpid is None: vpid = -1
289 self._plotter.set_ninterval_y(interval, vpid)
290
291 def set_xnumlocation(self, location=None, vpid=None):
292 """\
293 set the location of number labels along x axis.
294
295 Parameters:
296 location: 'l' (left side) or 'r' (right side). default is 'l'.
297 vpid: viewport id. when unset, operation is
298 applied to the viewport with the largest id.
299 """
300 if vpid is None: vpid = -1
301 if location is None: location = "l"
302 self._plotter.set_nlocation_x(location.lower(), vpid)
303
304 def set_ynumlocation(self, location=None, vpid=None):
305 """\
306 set the location of number labels along y axis.
307
308 Parameters:
309 location: 'b' (bottom) or 't' (top). default is 'b'.
310 vpid: viewport id. when unset, operation is
311 applied to the viewport with the largest id.
312 """
313 if vpid is None: vpid = -1
314 if location is None: location = "b"
315 self._plotter.set_nlocation_y(location.lower(), vpid)
316
317 def set_data(self, xdata, ydata, vpid=None, dataid=None):
318 """\
319 append or change dataset to be plotted.
320
321 Parameters:
322 xdata: a list of x positions of the input data.
323 ydata: a list of y positions of the input data.
324 vpid: viewport id. when not given, the last viewport
325 will be the target.
326 dataid: dataset id. a integer starting from 0. when dataid
327 is given, the corresponding dataset of the specified
328 viewport is replaced by the given xdata and ydata.
329 when not given, a new dataset of xdata and ydata is
330 appended.
331 """
332 if dataid is None:
333 if vpid is None: vpid = -1
334 dataid = -1
335
336 self._plotter.set_data(xdata, ydata, vpid, dataid)
337
338 def set_line(self, color, width=None, style=None, vpid=None, dataid=None):
339 """\
340 change line attributes.
341
342 Parameters:
343 color: line color specified by color name. available colors
344 can be listed via list_colornames().
345 width: line width. default is 1.
346 style: line style. available styles can be listed via
347 list_linestyles().
348 vpid: viewport id. when not given, the last viewport
349 will be the target of operation.
350 dataid: dataset id. when not given, the last dataset for the
351 specified viewport is the target.
352 """
353 if width is None: width = 1
354 if style is None: style = "solid"
355 if vpid is None: vpid = -1
356 if dataid is None: dataid = -1
357
358 coloridx = self.get_colorindex(color)
359 styleidx = self.get_linestyleindex(style)
360 self._plotter.set_line(coloridx, width, styleidx, vpid, dataid)
361
362 def show_line(self, vpid=None, dataid=None):
363 """\
364 show line connecting the specified dataset.
365
366 Parameters:
367 vpid: viewport id. when not given, the last viewport
368 will be the target.
369 dataid: dataset id. when not given, the last dataset used.
370 """
371 if dataid is None:
372 if vpid is None: vpid = -1
373 dataid = -1
374
375 self._plotter.show_line(vpid, dataid)
376
377 def hide_line(self, vpid=None, dataid=None):
378 """\
379 hide line connecting the specified dataset.
380
381 Parameters:
382 vpid: viewport id. when not given, the last viewport
383 will be the target.
384 dataid: dataset id. when not given, the last dataset used.
385 """
386 if dataid is None:
387 if vpid is None: vpid = -1
388 dataid = -1
389
390 self._plotter.hide_line(vpid, dataid)
391
392 def set_point(self, type, size, color, vpid=None, dataid=None):
393 """\
394 change marker attributes for the specified dataset.
395
396 Parameters:
397 type: type of marker symbol. see PGPLOT manual for detail.
398 size: marker size.
399 color: color of marker. see output of list_colornames().
400 vpid: viewport id. when not given, the last viewport
401 will be the target.
402 dataid: dataset id. when not given, the last dataset used.
403 """
404 if dataid is None:
405 if vpid is None: vpid = -1
406 dataid = -1
407 coloridx = self.get_colorindex(color)
408 self._plotter.set_point(type, size, coloridx, vpid, dataid)
409
410 def show_point(self, vpid=None, dataid=None):
411 """\
412 show markers for the specified dataset.
413
414 Parameters:
415 vpid: viewport id. when not given, the last viewport
416 will be the target.
417 dataid: dataset id. when not given, the last dataset used.
418 """
419 if dataid is None:
420 if vpid is None: vpid = -1
421 dataid = -1
422
423 self._plotter.show_point(vpid, dataid)
424
425 def hide_point(self, vpid=None, dataid=None):
426 """\
427 hide markers for the specified dataset.
428
429 Parameters:
430 vpid: viewport id. when not given, the last viewport
431 will be the target.
432 dataid: dataset id. when not given, the last dataset used.
433 """
434 if dataid is None:
435 if vpid is None: vpid = -1
436 dataid = -1
437
438 self._plotter.hide_point(vpid, dataid)
439
440 def set_xmask(self, xmin, xmax, color=None, fstyle=None, width=None, hsep=None, vpid=None):
441 """\
442 add a rectangle which spans full y range.
443
444 Parameters:
445 xmin: the smaller end of mask region
446 xmax: the larger end of mask region
447 color: color of mask region. see output of list_colornames().
448 default is "lightgray".
449 fstyle: fill style. see output of list_fillstyles().
450 default is "solid".
451 width: width of outline of mask region. default is 1.
452 hsep: spacing of hatched lines. default is 1.0
453 vpid: viewport id. when not given, the last viewport
454 will be the target.
455 """
456 if color is None: color = "lightgray"
457 if fstyle is None: fstyle = "solid"
458 if width is None: width = 1
459 if hsep is None: hsep = 1.0
460 if vpid is None: vpid = -1
461 coloridx = self.get_colorindex(color)
462 fstyleidx = self.get_fillstyleindex(fstyle)
463 self._plotter.set_mask_x(xmin, xmax, coloridx, fstyleidx, width, hsep, vpid)
464
465 def set_xlabel(self, label, style=None, size=None, posx=None, posy=None, vpid=None):
466 """\
467 set label string along x axis. when the position of label is
468 specified explicitly by posx and posy, the label appears so
469 that its center placed on the specified position.
470
471 Parameters:
472 label: label string.
473 style: font style. "normal", "roman", "italic" and "script"
474 are available. default is "normal".
475 size: font size. default is 1.1 (10% larger than that
476 of number labels)
477 posx: x position of label string in window coordinate.
478 default is the center of x axis.
479 posy: y position of label string.
480 vpid: viewport id. when not given, the last viewport
481 will be the target.
482 """
483 if style is None: style = ""
484 if size is None: size = 1.1
485 if posx is None: posx = -1.0
486 if posy is None: posy = -1.0
487 if vpid is None: vpid = -1
488
489 self._plotter.set_label_x(label, posx, posy, size, style, 1, 0, vpid)
490
491 def set_ylabel(self, label, style=None, size=None, posx=None, posy=None, vpid=None):
492 """\
493 set label string along y axis. when the position of label is
494 specified explicitly by posx and posy, the label appears so
495 that its center placed on the specified position.
496
497 Parameters:
498 label: label string.
499 style: font style. "normal", "roman", "italic" and "script"
500 are available. default is "normal".
501 size: font size. default is 1.1 (10% larger than that
502 of number labels)
503 posx: x position of label string in window coordinate.
504 posy: y position of label string.
505 default is the center of y axis.
506 vpid: viewport id. when not given, the last viewport
507 will be the target.
508 """
509 if style is None: style = ""
510 if size is None: size = 1.1
511 if posx is None: posx = -1.0
512 if posy is None: posy = -1.0
513 if vpid is None: vpid = -1
514
515 self._plotter.set_label_y(label, posx, posy, size, style, 1, 0, vpid)
516
517 def set_title(self, title, style=None, size=None, posx=None, posy=None, vpid=None):
518 """\
519 set title string over the top of the specified viewport.
520 when the position of title is specified explicitly by posx
521 and posy, the title appears so that its center placed on
522 the specified position.
523
524 Parameters:
525 title: title string.
526 style: font style. "normal", "roman", "italic" and "script"
527 are available. default is "normal".
528 size: font size. default is 1.5 (50% larger than that
529 of number titles)
530 posx: x position of title string in window coordinate.
531 posy: y position of title string.
532 default is the center of y axis.
533 vpid: viewport id. when not given, the last viewport
534 will be the target.
535 """
536 if style is None: style = ""
537 if size is None: size = 1.5
538 if posx is None: posx = -1.0
539 if posy is None: posy = -1.0
540 if vpid is None: vpid = -1
541
542 self._plotter.set_title(title, posx, posy, size, style, 1, 0, vpid)
543
544 def set_vpbgcolor(self, bgcolor, vpid=None):
545 """\
546 change the background color of the specified viewport.
547 default is transparent.
548
549 Parameters:
550 bgcolor: background color. see output of list_colornames().
551 default is "" (transparent).
552 vpid: viewport id. when not given, the last viewport
553 will be the target.
554 """
555 if vpid is None: vpid = -1
556
557 if bgcolor.strip() == "":
558 coloridx = -1
559 else:
560 coloridx = self.get_colorindex(bgcolor)
561
562 self._plotter.set_vpbgcolor(coloridx, vpid)
563
564 def plot(self):
565 """\
566 execute actual plotting.
567 """
568 self._plotter.plot()
569
570 def save(self, filename):
571 """\
572 save the figure in a file with specified name.
573 the filename and device previously set by set_output() is
574 not affected by this command.
575
576 Parameters:
577 filename: output file name.
578 """
579 prev_filename = self._plotter.get_filename()
580 prev_dev = self._plotter.get_device()
581
582 self.set_output(filename)
583 self.plot()
584
585 self.set_output(prev_filename, prev_dev)
586
587 def get_vinfo(self):
588 self._plotter.get_vinfo()
589
590 @classmethod
591 def get_colorindex(cls, colorname):
592 """\
593 convert the given color name into color index used in PGPLOT.
594 """
595 name = colorname.strip().lower()
596 available_color = True
597
598 if name == "white": idx = 0 # our definition of bgcolor
599 elif name == "black": idx = 1 # our definition of fgcolor
600 elif name == "red": idx = 2
601 elif name == "green": idx = 3
602 elif name == "blue": idx = 4
603 elif name == "cyan": idx = 5
604 elif name == "magenta": idx = 6
605 elif name == "yellow": idx = 7
606 elif name == "orange": idx = 8
607 elif name == "yellowgreen": idx = 9
608 elif name == "emerald": idx = 10
609 elif name == "skyblue": idx = 11
610 elif name == "purple": idx = 12
611 elif name == "pink": idx = 13
612 elif name == "gray": idx = 14
613 elif name == "lightgray": idx = 15
614 else: available_color = False
615
616 if (available_color):
617 return idx
618 else:
619 raise ValueError("Unavailable colour name.")
620
621 @classmethod
622 def list_colornames(cls):
623 """\
624 list the available color names.
625 """
626 print "plotter2: default color list ----"
627 print " (0) white (background)"
628 print " (1) black (foreground)"
629 print " (2) red"
630 print " (3) green"
631 print " (4) blue"
632 print " (5) cyan"
633 print " (6) magenta"
634 print " (7) yellow"
635 print " (8) orange"
636 print " (9) yellowgreen"
637 print " (10) emerald"
638 print " (11) skyblue"
639 print " (12) purple"
640 print " (13) pink"
641 print " (14) gray"
642 print " (15) lightgray"
643 print "---------------------------------"
644
645 @classmethod
646 def get_linestyleindex(cls, fstyle):
647 """\
648 convert the given line style into style index used in PGPLOT.
649 """
650 style = fstyle.strip().lower()
651 available_style = True
652
653 if style == "solid": idx = 1
654 elif style == "dashed": idx = 2
655 elif style == "dash-dotted": idx = 3
656 elif style == "dotted": idx = 4
657 elif style == "dash-dot-dot-dotted": idx = 5
658 else: available_style = False
659
660 if (available_style):
661 return idx
662 else:
663 raise ValueError("Unavailable line style.")
664
665 @classmethod
666 def list_linestyles(cls):
667 """\
668 list the available line styles.
669 """
670 print "plotter2: fill style list ----"
671 print " (1) solid"
672 print " (2) dashed"
673 print " (3) dash-dotted"
674 print " (4) dotted"
675 print " (5) dash-dot-dot-dotted"
676 print "------------------------------"
677
678 @classmethod
679 def get_fillstyleindex(cls, fstyle):
680 """\
681 convert the given fill style into style index used in PGPLOT.
682 """
683 style = fstyle.strip().lower()
684 available_style = True
685
686 if style == "solid": idx = 1
687 elif style == "outline": idx = 2
688 elif style == "hatched": idx = 3
689 elif style == "crosshatched": idx = 4
690 else: available_style = False
691
692 if (available_style):
693 return idx
694 else:
695 raise ValueError("Unavailable fill style.")
696
697 @classmethod
698 def list_fillstyles(cls):
699 """\
700 list the available fill styles.
701 """
702 print "plotter2: fill style list ----"
703 print " (1) solid"
704 print " (2) outline"
705 print " (3) hatched"
706 print " (4) crosshatched"
707 print "------------------------------"
708
709"""
710 def set_annotation(self, label, posx=None, posy=None, angle=None, fjust=None, size=None, style=None, color=None, bgcolor=None, vpid=None):
711 if posx is None: posx = -1.0
712 if posy is None: posy = -1.0
713 if angle is None: angle = 0.0
714 if fjust is None: fjust = 0.5
715 if size is None: size = 2.0
716 if style is None: style = ""
717 if color is None: color = 1 #default foreground colour (b)
718 if bgcolor is None: bgcolor = 0 #default backgound colour (w)
719 if vpid is None: vpid = -1
720
721 coloridx = self.get_colorindex(color)
722 bgcoloridx = self.get_colorindex(bgcolor)
723 self._plotter.set_annotation(label, posx, posy, angle, fjust, size, style, coloridx, bgcoloridx, vpid)
724"""
Note: See TracBrowser for help on using the repository browser.