source: trunk/python/plotter2.py@ 2845

Last change on this file since 2845 was 2830, checked in by WataruKawasaki, 11 years ago

New Development: Yes

JIRA Issue: Yes (CAS-3620)

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: Yes

Module(s): sd

Description: a new simple plotter based on PGPLOT. the module name is sd.plotter2. only available on casapy (unavailable in the standalone ASAP)


File size: 25.6 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 of area to be displayed in a 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 of area to be displayed in a 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 of area to be displayed in a 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 display area of the specified viewport in
140 a shape 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 display area of the specified viewport in
152 a shape 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 2D range of display area of the specified viewport to be
164 automatic depending on the given data.
165 the x range will be the exact range of the x values of data,
166 whereas y range has a margin of 10% of the range of y values of data.
167
168 Parameter:
169 vpid: viewport id. when unset, operation is applied to
170 the viewport with the largest id.
171 """
172 if vpid is None: vpid = -1
173 self._plotter.set_autorange(vpid)
174
175 def set_xautorange(self, vpid=None):
176 """\
177 set x range of display area of the specified viewport to be
178 automatic depending on the given data.
179 the x range will be the exact range of the x values of data,
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 display area of the specified viewport to be
191 automatic depending on the given data.
192 the y range has a margin of 10% of the range of y values of data.
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.
350 dataid: dataset id. when not given, the last dataset used.
351 """
352 if width is None: width = 1
353 if style is None: style = "solid"
354 if vpid is None: vpid = -1
355 if dataid is None: dataid = -1
356
357 coloridx = self.get_colorindex(color)
358 styleidx = self.get_linestyleindex(style)
359 self._plotter.set_line(coloridx, width, styleidx, vpid, dataid)
360
361 def show_line(self, vpid=None, dataid=None):
362 """\
363 show line connecting the specified dataset.
364
365 Parameters:
366 vpid: viewport id. when not given, the last viewport
367 will be the target.
368 dataid: dataset id. when not given, the last dataset used.
369 """
370 if dataid is None:
371 if vpid is None: vpid = -1
372 dataid = -1
373
374 self._plotter.show_line(vpid, dataid)
375
376 def hide_line(self, vpid=None, dataid=None):
377 """\
378 hide line connecting the specified dataset.
379
380 Parameters:
381 vpid: viewport id. when not given, the last viewport
382 will be the target.
383 dataid: dataset id. when not given, the last dataset used.
384 """
385 if dataid is None:
386 if vpid is None: vpid = -1
387 dataid = -1
388
389 self._plotter.hide_line(vpid, dataid)
390
391 def set_point(self, type, size, color, vpid=None, dataid=None):
392 """\
393 change marker attributes for the specified dataset.
394
395 Parameters:
396 type: type of marker symbol. see PGPLOT manual for detail.
397 size: marker size.
398 color: color of marker. see output of list_colornames().
399 vpid: viewport id. when not given, the last viewport
400 will be the target.
401 dataid: dataset id. when not given, the last dataset used.
402 """
403 if dataid is None:
404 if vpid is None: vpid = -1
405 dataid = -1
406 coloridx = self.get_colorindex(color)
407 self._plotter.set_point(type, size, coloridx, vpid, dataid)
408
409 def show_point(self, vpid=None, dataid=None):
410 """\
411 show markers for the specified dataset.
412
413 Parameters:
414 vpid: viewport id. when not given, the last viewport
415 will be the target.
416 dataid: dataset id. when not given, the last dataset used.
417 """
418 if dataid is None:
419 if vpid is None: vpid = -1
420 dataid = -1
421
422 self._plotter.show_point(vpid, dataid)
423
424 def hide_point(self, vpid=None, dataid=None):
425 """\
426 hide markers for the specified dataset.
427
428 Parameters:
429 vpid: viewport id. when not given, the last viewport
430 will be the target.
431 dataid: dataset id. when not given, the last dataset used.
432 """
433 if dataid is None:
434 if vpid is None: vpid = -1
435 dataid = -1
436
437 self._plotter.hide_point(vpid, dataid)
438
439 def set_xmask(self, xmin, xmax, color=None, fstyle=None, width=None, hsep=None, vpid=None):
440 """\
441 add a rectangle which spans full y range.
442
443 Parameters:
444 xmin: the smaller end of mask region
445 xmax: the larger end of mask region
446 color: color of mask region. see output of list_colornames().
447 default is "lightgray".
448 fstyle: fill style. see output of list_fillstyles().
449 default is "solid".
450 width: width of outline of mask region. default is 1.
451 hsep: spacing of hatched lines. default is 1.0
452 vpid: viewport id. when not given, the last viewport
453 will be the target.
454 """
455 if color is None: color = "lightgray"
456 if fstyle is None: fstyle = "solid"
457 if width is None: width = 1
458 if hsep is None: hsep = 1.0
459 if vpid is None: vpid = -1
460 coloridx = self.get_colorindex(color)
461 fstyleidx = self.get_fillstyleindex(fstyle)
462 self._plotter.set_mask_x(xmin, xmax, coloridx, fstyleidx, width, hsep, vpid)
463
464 def set_xlabel(self, label, style=None, size=None, posx=None, posy=None, vpid=None):
465 """\
466 set label string along x axis. when the position of label is
467 specified explicitly by posx and posy, the label appears so
468 that its center placed on the specified position.
469
470 Parameters:
471 label: label string.
472 style: font style. "normal", "roman", "italic" and "script"
473 are available. default is "normal".
474 size: font size. default is 1.1 (10% larger than that
475 of number labels)
476 posx: x position of label string in window coordinate.
477 default is the center of x axis.
478 posy: y position of label string.
479 vpid: viewport id. when not given, the last viewport
480 will be the target.
481 """
482 if style is None: style = ""
483 if size is None: size = 1.1
484 if posx is None: posx = -1.0
485 if posy is None: posy = -1.0
486 if vpid is None: vpid = -1
487
488 self._plotter.set_label_x(label, posx, posy, size, style, 1, 0, vpid)
489
490 def set_ylabel(self, label, style=None, size=None, posx=None, posy=None, vpid=None):
491 """\
492 set label string along y axis. when the position of label is
493 specified explicitly by posx and posy, the label appears so
494 that its center placed on the specified position.
495
496 Parameters:
497 label: label string.
498 style: font style. "normal", "roman", "italic" and "script"
499 are available. default is "normal".
500 size: font size. default is 1.1 (10% larger than that
501 of number labels)
502 posx: x position of label string in window coordinate.
503 posy: y position of label string.
504 default is the center of y axis.
505 vpid: viewport id. when not given, the last viewport
506 will be the target.
507 """
508 if style is None: style = ""
509 if size is None: size = 1.1
510 if posx is None: posx = -1.0
511 if posy is None: posy = -1.0
512 if vpid is None: vpid = -1
513
514 self._plotter.set_label_y(label, posx, posy, size, style, 1, 0, vpid)
515
516 def set_title(self, title, style=None, size=None, posx=None, posy=None, vpid=None):
517 """\
518 set title string over the top of the specified viewport.
519 when the position of title is specified explicitly by posx
520 and posy, the title appears so that its center placed on
521 the specified position.
522
523 Parameters:
524 title: title string.
525 style: font style. "normal", "roman", "italic" and "script"
526 are available. default is "normal".
527 size: font size. default is 1.5 (50% larger than that
528 of number titles)
529 posx: x position of title string in window coordinate.
530 posy: y position of title string.
531 default is the center of y axis.
532 vpid: viewport id. when not given, the last viewport
533 will be the target.
534 """
535 if style is None: style = ""
536 if size is None: size = 1.5
537 if posx is None: posx = -1.0
538 if posy is None: posy = -1.0
539 if vpid is None: vpid = -1
540
541 self._plotter.set_title(title, posx, posy, size, style, 1, 0, vpid)
542
543 def set_vpbgcolor(self, bgcolor, vpid=None):
544 """\
545 change the background color of the specified viewport.
546 default is transparent.
547
548 Parameters:
549 bgcolor: background color. see output of list_colornames().
550 default is "" (transparent).
551 vpid: viewport id. when not given, the last viewport
552 will be the target.
553 """
554 if vpid is None: vpid = -1
555
556 if bgcolor.strip() == "":
557 coloridx = -1
558 else:
559 coloridx = self.get_colorindex(bgcolor)
560
561 self._plotter.set_vpbgcolor(coloridx, vpid)
562
563 def plot(self):
564 """\
565 execute actual plotting.
566 """
567 self._plotter.plot()
568
569 def save(self, filename):
570 """\
571 save the figure in a file with specified name.
572 the filename and device previously set by set_output() is
573 not affected by this command.
574
575 Parameters:
576 filename: output file name.
577 """
578 prev_filename = self._plotter.get_filename()
579 prev_dev = self._plotter.get_device()
580
581 self.set_output(filename)
582 self.plot()
583
584 self.set_output(prev_filename, prev_dev)
585
586 def get_vinfo(self):
587 self._plotter.get_vinfo()
588
589 def get_colorindex(self, colorname):
590 """\
591 (for internal use)
592 convert the given color name into color index used in PGPLOT.
593 """
594 name = colorname.strip().lower()
595
596 if name == "white": idx = 0 # our definition of bgcolor
597 if name == "black": idx = 1 # our definition of fgcolor
598 if name == "red": idx = 2
599 if name == "green": idx = 3
600 if name == "blue": idx = 4
601 if name == "cyan": idx = 5
602 if name == "magenta": idx = 6
603 if name == "yellow": idx = 7
604 if name == "orange": idx = 8
605 if name == "yellowgreen": idx = 9
606 if name == "emerald": idx = 10
607 if name == "skyblue": idx = 11
608 if name == "purple": idx = 12
609 if name == "pink": idx = 13
610 if name == "gray": idx = 14
611 if name == "lightgray": idx = 15
612
613 return idx
614
615 def list_colornames(self):
616 """\
617 list the available color names.
618 """
619 print "plotter2: default color list ----"
620 print " (0) white (background)"
621 print " (1) black (foreground)"
622 print " (2) red"
623 print " (3) green"
624 print " (4) blue"
625 print " (5) cyan"
626 print " (6) magenta"
627 print " (7) yellow"
628 print " (8) orange"
629 print " (9) yellowgreen"
630 print " (10) emerald"
631 print " (11) skyblue"
632 print " (12) purple"
633 print " (13) pink"
634 print " (14) gray"
635 print " (15) lightgray"
636 print "---------------------------------"
637
638 def get_linestyleindex(self, fstyle):
639 """\
640 (for internal use)
641 convert the given line style into style index used in PGPLOT.
642 """
643 style = fstyle.strip().lower()
644 if style == "solid": idx = 1
645 if style == "outline": idx = 2
646 if style == "hatched": idx = 3
647 if style == "crosshatched": idx = 4
648
649 return idx
650
651 def list_linestyles(self):
652 """\
653 list the available line styles.
654 """
655 print "plotter2: fill style list ----"
656 print " (1) solid"
657 print " (2) dashed"
658 print " (3) dash-dotted"
659 print " (4) dotted"
660 print " (5) dash-dot-dot-dotted"
661 print "------------------------------"
662
663 def get_fillstyleindex(self, fstyle):
664 """\
665 (for internal use)
666 convert the given fill style into style index used in PGPLOT.
667 """
668 style = fstyle.strip().lower()
669 if style == "solid": idx = 1
670 if style == "outline": idx = 2
671 if style == "hatched": idx = 3
672 if style == "crosshatched": idx = 4
673
674 return idx
675
676 def list_fillstyles(self):
677 """\
678 list the available fill styles.
679 """
680 print "plotter2: fill style list ----"
681 print " (1) solid"
682 print " (2) outline"
683 print " (3) hatched"
684 print " (4) crosshatched"
685 print "------------------------------"
686
687"""
688 def set_annotation(self, label, posx=None, posy=None, angle=None, fjust=None, size=None, style=None, color=None, bgcolor=None, vpid=None):
689 if posx is None: posx = -1.0
690 if posy is None: posy = -1.0
691 if angle is None: angle = 0.0
692 if fjust is None: fjust = 0.5
693 if size is None: size = 2.0
694 if style is None: style = ""
695 if color is None: color = 1 #default foreground colour (b)
696 if bgcolor is None: bgcolor = 0 #default backgound colour (w)
697 if vpid is None: vpid = -1
698
699 coloridx = self.get_colorindex(color)
700 bgcoloridx = self.get_colorindex(bgcolor)
701 self._plotter.set_annotation(label, posx, posy, angle, fjust, size, style, coloridx, bgcoloridx, vpid)
702"""
Note: See TracBrowser for help on using the repository browser.