forked from emmt/ylib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.i
More file actions
2689 lines (2520 loc) · 99 KB
/
plot.i
File metadata and controls
2689 lines (2520 loc) · 99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* plot.i --
*
* Additional routines for plotting in Yorick.
*
* ---------------------------------------------------------------------------
*
* This file is part of YLib available at <https://github.com/emmt/ylib> and
* licensed under the MIT "Expat" License.
*
* Copyright (C) 2000-2017, Éric Thiébaut.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* ----------------------------------------------------------------------------
*
* Routines:
* color_bar: add a color bar to a plot;
* pl3dj: plot disjoint lines in 3D space;
* pl3s: plot 3D surface;
* pl3t: plot text in 3D space;
* pl_get_axis_flags: parse axis settings;
* pl_get_color: parse color value/name;
* pl_get_font: parse font value/name;
* pl_get_symbol: parse symbol value/name;
* pl_map: apply a function to all the elements of an array;
* pla: plot several curves at the same time;
* plhline: plot horizontal line across viewport;
* pls: plot surface as a filled mesh with contours;
* plvline: plot vertical line across viewport;
* ps2png, ps2jpeg: convert PostScript file into bitmap image;
* win2png, win2jpeg: dump graphical window into bitmap image;
* win_copy_lim: copy plot limits between graphic windows;
* xbtn_plot: plot pseudo-GUI buttons;
* xbtn_which: manage pseudo-GUI buttons;
* xmouse: extended mouse interface;
* xmouse_box: interactively select and draw a rectangular region;
* xmouse_demo: simple demonstration of xmouse capabilities;
* xmouse_length: interactively measure a distance;
* xmouse_line: interactively select and draw a line;
* xmouse_point: interactively select and draw point(s);
* xwindow: setup viewport and graphic style of graphic windows;
*
* ----------------------------------------------------------------------------
*/
require, "utils.i";
require, "xplot.i";
func pl_arrow(x0, y0, x1, y1, head=, size=, width=, color=, angle=)
/* DOCUMENT pl_arrow, x0, y0, x1, y1;
Plot an arrow starting at (X0,Y0), ending at (X1,Y1).
RESTRICTIONS
Works better with square limits.
SEE ALSO:
*/
{
color = pl_get_color(color);
pldj, x0,y0,x1,y1, width=width, color=color, type=type;
if (head) {
if (is_void(angle)) angle = 50.0;
if (is_void(size)) size = 1.0;
u = x1 - x0;
v = y1 - y0;
s = size*0.01/abs(u, v);
alpha = angle*(pi/360.0); // half angle with respect to direction
sn = s*sin(alpha);
cs = s*cos(alpha);
xm = [cs*u - sn*v, 0.0, cs*u + sn*v, 0.0];
ym = [cs*v + sn*u, 0.0, cs*v - sn*u, 0.0];
m = numberof(xm);
n = array(1, 1 + numberof(y1));
n(1) = m;
if ((head&1) != 0) {
plfp, z, grow(ym,y0(*)), grow(xm,x0(*)), n,
edges=1, ewidth=width, ecolor=color;
}
if ((head&2) != 0) {
plfp, z, grow(-ym,y1(*)), grow(-xm,x1(*)), n,
edges=1, ewidth=width, ecolor=color;
}
}
}
func pl_fc(z, y, x, ireg, levs=, legend=, hide=, type=, width=, color=,
colors=, smooth=, marks=, marker=, mspace=, mphase=,
triangle=, region=)
{
d = dimsof(z);
if (d(1) != 2)
error, "expecting a 2D array for Z";
nx = d(2);
ny = d(3);
if (is_void(x))
x = span(1, nx, nx)(,-:1:ny);
else if (dimsof(x)(1) == 1)
x = x(,-:1:ny);
if (is_void(y))
y = span(1, ny, ny)(-:1:nx,);
else if (dimsof(y)(1) == 1)
y = y(-:1:nx,);
if (is_void(levs)) {
zmin = min(z);
zmax = max(z);
if (zmin >= zmax) levs = zmin;
else levs = zmin+indgen(9)*0.1*(zmax-zmin);
}
plfc, z, y, x, ireg, levs=levs, colors=colors,
triangle=triangle, region=region;
plc, z, y, x, ireg, levs=levs, legend=legend, hide=hide, type=type,
width=width, color=color, smooth=smooth, marks=marks, marker=marker,
mspace=mspace, mphase=mphase, triangle=triangle, region=region;
}
/*---------------------------------------------------------------------------*/
func color_bar(levs, colors, vert=, labs=, adjust=, color=, width=,
height=, ticklen=, vport=, format=, font=)
/* DOCUMENT color_bar;
or color_bar, levs, colors;
Remarks: This routine supersedes `color_bar` in `graph.i` (more keywords
are available to specify the viewport, the text font, color,
etc.). For most purposes, `pl_cbar` (which see) is probably
more suitable.
Draw a color bar below the current coordinate system. If LEVS is not
specified uses plfc_levs (set by previous call to plfc). If COLORS is
specified, it should have one more value than LEVS, otherwise equally
spaced colors are chosen, or plfc_colors if plfc_levs was used. With the
VERT=1 keyword the color bar appears to the left of the current coordinate
system (vert=0 is default). By default, color_bar will attempt to label
some of the color interfaces. With the LABS keyword, you can force the
labelling algorithm as follows: LABS=0 supresses all labels, LABS=n forces
a label at every n-th interface, LABS=[i,n] forces a label every n-th
interface starting at i-th interface (0<=i<=numberof(LEVS)).
You can specify the viewport coordinates by keyword
VPORT=[xmin,xmax,ymin,ymax]; by default the colorbar is drawn next to the
current viewport. You can use the ADJUST keyword to move the bar closer
to (adjust<0) or further from (adjust>0) the viewport.
You can specify the string format for labels with keyword FORMAT (default
"%g"), the font type with keyword FONT (default "helvetica") and the font
height with keyword HEIGHT (default 14 points).
Keyword COLOR can be used to specify the color of the labels, the ticks
and the frame of the colorbar. Default is foreground color.
Keyword WIDTH can be used to set the width of the lines used to draw the
frame and the ticks of the colorbar.
Keyword TICKLEN can be used to set the length (in NDC units) of the ticks.
Default is 0.005 NDC.
SEE ALSO: pl_cbar, plfc. */
{
nil = string(0);
if (is_void(levs)) {
if (is_void(plfc_levs)) error, "no levels specified";
levs = plfc_levs;
n = numberof(levs)+1;
if (is_void(colors)) colors = plfc_colors;
} else {
n = numberof(levs) + 1;
if (is_void(colors)) colors = bytscl(span(1,n,n),cmin=0.5,cmax=n+0.5);
}
if (n != numberof(colors))
error, "numberof(colors) must be one more than numberof(levs)";
if (is_void(vport)) vport = _p_builtin_viewport();
if (is_void(adjust)) adjust = 0.0;
if (is_void(ticklen)) ticklen = 0.005;
dx = dy = 0.0;
if (vert) {
x = (vport(2)+adjust+[0.022,0.042])(-:1:n+1,);
dx = ticklen;
y = span(vport(3),vport(4),n+1)(,-:1:2);
} else {
y = (vport(3)-adjust-[0.045,0.065])(-:1:n+1,);
dy = -ticklen;
x = span(vport(1),vport(2),n+1)(,-:1:2);
}
sys = plsys(0);
plf,[colors],y,x,edges=1,ecolor=color,legend=nil;
plsys, sys;
if (is_void(labs) || labs(0) > 0) {
if (numberof(levs) > 1) {
dz = levs(dif);
if (numberof(dz) != numberof(levs) - 1 ||
anyof((dz > 0.0) != (dz(1) > 0.0)) || !dz(1))
error, "levs must be monotone 1D";
levs = levs(1:0);
levs = grow([2*levs(1)-levs(2)],levs,[2*levs(0)-levs(-1)]);
} else {
levs = double(levs(1));
if (!levs) levs = [-1.0,levs,1.0];
else levs = [0.0,levs,2*levs];
}
if (numberof(labs)<2) {
if (is_void(labs)) labs = (n-1)/4 + 1;
orig = where(levs<1.0e-9*max(levs(dif)));
if (numberof(orig)==1) labs = [orig(1)%labs,labs];
else labs = [(n%labs)/2,labs];
}
list = where(indgen(0:n)%labs(2)==labs(1));
x = x(list,);
y = y(list,);
if (is_void(format)) format = "%g";
labs = swrite(format=format,levs(list));
plsys, 0;
pldj, x(,2),y(,2),x(,2)+dx,y(,2)+dy, legend=nil, color=color, width=width;
plsys, sys;
if (is_void(font)) font = "helvetica";
plt1, labs,x(,2)+2*dx,y(,2)+2*dy, justify=(vert?"LH":"CT"),
height=height, font=font, color=color;
}
}
/*---------------------------------------------------------------------------*/
func pla(y, x, every=, legend=, hide=, type=, width=, color=, closed=, smooth=,
marks=, marker=, mspace=, mphase=, rays=, arrowl=, arroww=, rspace=,
rphase=)
/* DOCUMENT pla, y, x
or pla, y
Plot the buddle of curves Y versus X labelled by their last dimension.
Y must be 2-dimensional, and X may be 2-dimensional, 1-dimensional or
omitted. If X is 2-dimensional, it must have the same dimensions as Y
and Y(,i) versus X(,i) is plotted for each last indice i. If X is
1-dimensional, it must have the same length as the 1st dimension of Y
and Y(,i) versus X is plotted for each last indice i. If X is
omitted, it defaults to [1, 2, ..., numberof(Y(,1))].
The plotting keywords of plg are accepted plus the optional keyword
EVERY=N which can be used to plot every N curves in the bundle
(default N=1).
EXAMPLE
x = span(0,1,25)(,-:1:25);
pla, x*transpose(x), marks=0, every=3;
SEE ALSO
plg, plp.
*/
{
if (is_void(every)) {
n = 1;
} else if (! is_array(every) || dimsof(every)(1) || (n = long(every)) <= 0) {
error, "EVERY must be a scalar >= 1";
}
if (! is_array(y) || dimsof(y)(1) != 2) {
error, "Y must be 2-dimensional";
}
imax = dimsof(y)(3);
if (is_void(x)) {
x2d = 0N;
} else {
x2d = dimsof(x)(1) >= 2;
}
for(i = (n+1)/2; i <= imax; i += n) {
px = (x2d ? &x(,i) : &x);
plg, y(, i), *px, legend=legend, hide=hide, type=type, width=width,
color=color, closed=closed, smooth=smooth, marks=marks, marker=marker,
mspace=mspace, mphase=mphase, rays=rays, arrowl=arrowl, arroww=arroww,
rspace=rspace, rphase=rphase;
}
}
/*---------------------------------------------------------------------------*/
func pls_mesh(&x, &xx, d, which=, inhibit=)
/* DOCUMENT err_msg = pls_mesh(x, xx, dimsof(z), which=1/2, inhibit=1/2)
build X and/or XX arrays of coordinates (abscissa if last argument is
0/nil; otherwise ordinate) for 2-D array Z. Normally, the returned
value is string(0) otherwise it is an error message.
X is input and output, it will have the same shape as Z and will be
suitable for contour plots. XX is purely output, it will have 1 more
element than Z in each dimension and will be suitable for mesh plots.
In other words, X(i,j) will furnish the coordinate of the centre of
cell Z(i,j) whereas XX(i,j), XX(i,j+1), XX(i+1,j) and XX(i+1,j+1)
will give the coordinates of the corners of cell Z(i,j).
Assuming the length of Z along the considered dimension is N
(N must be >= 2) there are 3 possibilities:
(1) if X is a vector with N elements or has the same shape as Z,
then X is considered to give the coordinates at the centre of Z
cells: X is unchanged and output XX is build by interpolating
(and extrapolating at the edges) X ;
(2) if X is a vector with N+1 elements or has 1 more element than Z
in each dimension, then X is considered to give the coordinates
at the corners of Z cells: output XX is set to input X and
output X is build by interpolating output XX;
(3) if X is nil, it defaults to [0.5, 1.5, ..., N-0.5] and XX
defaults to [0, 1, ..., N] along the considered dimension.
Finally, if X is 1-D, it is expanded in the other direction.
If keyword WHICH is 1 (the default), abscissa is the dimension of
interest; otherwise WHICH must be 2 and ordinate is the dimension
of interest.
If keyword INHIBIT is 1, then only X output is computed; if INHIBIT
is 2 then only XX output is computed.
SEE ALSO: pls, pl3s, plmesh.
*/
{
xx = [];
if (is_void(which))
which = 1;
do_x = inhibit != 1;
do_xx = inhibit != 2;
expand=1;
if (d(1) != 2 || anyof(d < 2))
return "Z must be 2-dimensional and have at least 2-by-2 elements";
n1 = d(2);
n2 = d(3);
n = d(which+1);
if (is_void((dx = dimsof(x)))) {
if (do_x)
x = span(0.5, n-0.5, n);
if (do_xx)
xx = span(0, n, n+1);
} else if (dx(1) == 1) {
if (dx(2) == n) {
if (do_xx) {
xx = x(pcen);
xx(1) = 2.0 * x(1) - x(2);
xx(0) = 2.0 * x(0) - x(-1);
}
} else if (dx(2) == n+1) {
xx = x;
x = do_x ? xx(zcen) : [];
}
} else if (dx(1) == 2) {
expand = 0;
if (allof(dx == d)) {
if (do_xx) {
t = x(pcen,);
t(1,) = 2.0 * x(1,) - x(2,);
t(0,) = 2.0 * x(0,) - x(-1,);
xx = t(,pcen);
xx(,1) = 2.0 * t(,1) - t(,2);
xx(,0) = 2.0 * t(,0) - t(,-1);
t = [];
}
} else if (allof(dx == d + [0,1,1])) {
xx = x;
x = do_x ? xx(zcen,zcen) : [];
}
}
if (is_void(xx) && is_void(x)) {
return "X, Y and Z are not compatible";
}
if (expand) {
if (which == 1) {
if (do_x)
x = x(,-:1:n2);
if (do_xx)
xx = xx(,-:1:n2+1);
} else {
if (do_x)
x = x(-:1:n1,);
if (do_xx)
xx = xx(-:1:n1+1,);
}
}
return string(0);
}
func pls(z, y, x, cbar=, viewport=, title=, xtitle=, ytitle=,
legend=, hide=, top=, cmin=, cmax=, edges=, ecolor=, ewidth=,
height=, font=, levs=, nlevs=, type=, width=, color=,
marks=, marker=, mspace=, mphase=, smooth=)
/* DOCUMENT pls, z, y, x
or pls, z
draws surface plot of Z versus (X,Y) as a filled mesh with
optional contours. The Z array must be a 2-dimensional array,
see documentation of pls_mesh for the meaning of X and Y.
If keyword CBAR is set to non-zero, a color bar is drawn on the
right of the plot. The current viewport (in NDC) may be
specified with keyword VIEWPORT, default is:
[0.19, 0.60, 0.44, 0.85].
The appearance of the filled mesh can be modified by means of
keywords: LEGEND, HIDE, TOP, CMIN, CMAX, EDGES, ECOLOR and EWIDTH
(see plf documentation).
Optional contour plot of Z may be superimposed by either keyword
NLEVS to set the number of contours or by with keyword LEVS to
specify the level values. The appearance of the contour plot can
be modified by means of keywords: LEGEND, HIDE, TYPE, WIDTH,
COLOR, MARKS, MARKER, MSPACE, MPHASE and SMOOTH (see plc
documentation).
SEE ALSO: pls_mesh, pl3s, plc, plf, plmesh.
*/
{
local r, g, b; // these variables are used to query colors
local xx, yy;
/*
* Set some defaults.
*/
if (is_void(edges))
edges = 0;
if (is_void(height)) {
height = 12;
small=10;
} else {
s = [8,10,12,14,18,24];
i = where(height == s);
if (numberof(i) != 1)
error, "bad font HEIGHT";
i = i(1);
small = i > 1 ? s(i-1) : height;
}
if (numberof(levs)) {
nlevs = numberof(levs);
} else if (is_void(nlevs)) {
nlevs = 8;
}
/*
* Compute mesh coordinates.
*/
i = nlevs >= 1 ? 0 : 1;
if ((msg = pls_mesh(x, xx, dimsof(z), which=1, inhibit=i)) != string(0) ||
(msg = pls_mesh(y, yy, dimsof(z), which=2, inhibit=i)) != string(0))
error, msg;
/*
* Plot color bar and titles.
*/
vpmax = [0.127, 0.672, 0.363, 0.908];
if (numberof(viewport) != 4)
viewport = [0.19, 0.60, 0.44, 0.85]; // standard viewport
if (cbar) {
local r, g, b;
plsys, 0;
margin = vpmax(2)-viewport(2);
x0 = viewport(2) + 0.7 * margin;
x1 = viewport(2) + 0.9 * margin;
y0 = viewport(3);
y1 = viewport(4);
palette, r, g, b, query=1;
n = numberof(r);
r = g = b = [];
pli, char(indgen(n)-1)(-,), legend=string(0), x0, y0, x1, y1;
plg, [y0,y0,y1,y1,y0], [x0,x1,x1,x0,x0], legend=string(0), marks=0,
width=1;
plsys, 1;
}
xc = 0.5*(viewport(1)+viewport(2));
yc = 0.5*(viewport(3)+viewport(4));
if (!is_void(title)) {
plt, title, xc, viewport(4) + 0.9 * (vpmax(4) - viewport(4)), tosys=0,
legend=string(0), justify="CT", path=0,
font=font, height=height, opaque=opaque;
}
if (!is_void(xtitle)) {
plt, xtitle, xc, vpmax(3) + 0.05 * (viewport(3) - vpmax(3)), tosys=0,
legend=string(0), justify="CB", path=0,
font=font, height=small, opaque=opaque;
}
if (!is_void(ytitle)) {
plt, ytitle, vpmax(1) + 0.05 * (viewport(1) - vpmax(1)), yc, tosys=0,
legend=string(0), justify="LH", path=1,
font=font, height=small, opaque=opaque;
}
/*
* Plot filled mesh.
*/
plf, z, yy, xx, legend=legend, hide=hide,
top=top, cmin=cmin, cmax=cmax, edges=edges, ecolor=ecolor, ewidth=ewidth;
xx = yy = [];
/*
* Plot contours.
*/
if (nlevs) {
if (is_void(levs)) {
zmax = double(max(z));
zmin = double(min(z));
levs = zmin + (zmax-zmin) / double(nlevs+1) * indgen(nlevs);
}
plc, z, y, x, levs=levs, legend=legend, hide=hide, type=type, width=width,
color=color, marks=marks, marker=marker, mspace=mspace, mphase=mphase,
smooth=smooth;
}
}
/*---------------------------------------------------------------------------*/
func plfg(y, x, base=, vert=, color=, edges=, ecolor=, ewidth=, etype=,
legend=, hide=)
/* DOCUMENT plfg, y;
or plfg, y, x;
This subroutine plots a filled curve of Y versus X. Y and X must be 1-D
arrays of equal length; if X is omitted, it defaults to [1, 2, ...,
numberof(Y)]. The filled area is delimited by the curve and a baseline
which is horizontal unless keyword VERT is set true, to indicate that the
baseline is vertical. Keyword BASE can be used to specify the coordinate
of the baseline (BASE = 0.0 by default). The fill color (default
foreground) can be specified with keyword COLOR.
If keyword EDGES is true, the curve is also drawn. The color, type and
width of the line used to draw the curve can be specified by keywords
ECOLOR, ETYPE and EWIDTH. If keyword EDGES is set with the special value
-1, the full outline of the filled area is drawn; otherwise only the
curve itself is drawn. By default, the curve is not drawn unless at
least one of the keywords ECOLOR, ETYPE or EWIDTHis non nil.
SEE ALSO: plg, plfp, pl_get_color.
*/
{
n = numberof(y);
if (is_void(base)) base = 0.0;
if (is_void(x)) x = double(indgen(n));
if (is_void(color)) {
z = []; // will draw with background color
} else {
z = pl_get_color(color)(,-);
}
if (! is_void(ecolor)) {
ecolor = pl_get_color(ecolor);
}
if (is_void(edges)) {
edges = ! (is_void(ecolor) && is_void(ewidth) && is_void(etype));
}
local ex, ey;
if (! vert) {
x1 = x(0);
x2 = x3 = x(1);
y1 = y2 = base;
y3 = y(1);
} else {
x1 = x2 = base;
x3 = x(1);
y1 = y(0);
y2 = y3 = y(1);
}
ey = grow(y, y1, y2, y3);
ex = grow(x, x1, x2, x3);
plfp, z, ey, ex, n + 3, edges=0, legend=legend, hide=hide;
if (edges) {
if (edges != -1) {
eq_nocopy, ex, x;
eq_nocopy, ey, y;
}
plg, ey, ex, color=ecolor, width=ewidth, type=etype,
legend=legend, hide=hide, marks=0;
}
}
/*---------------------------------------------------------------------------*/
plvline = pl_vline;
plhline = pl_hline;
/*---------------------------------------------------------------------------*/
/* PARSING OF PLOTTING KEYWORDS */
local PL_BG, PL_FG, PL_BLACK, PL_WHITE, PL_RED, PL_GREEN, PL_BLUE, PL_CYAN;
local PL_MAGENTA, PL_YELLOW, PL_GRAYD, PL_GRAYC, PL_GRAYB, PL_GRAYA;
local PL_XOR, PL_EXTRA;
local _PL_COLOR_NAMES, _PL_COLOR_RGB, _PL_COLOR_PACKED;
func pl_get_color(color, flags)
/* DOCUMENT pl_get_color(color);
* -or- pl_get_color(color, flags);
*
* The function pl_get_color parses its argument and returns the
* corresponding numerical color(s). Input COLOR can be specified by
* color name, color index, packed RGB triplet or non-packed [R,G,B]
* triplet. In any case, the output is an array of char's and either
* indexed color(s) or [R,G,B] triplet(s).
*
* The default is to accept only a single input color and to return
* the corresponding color index or [R,G,B] triplet. Optional argument
* FLAGS can be specified to change this default behaviour:
* FLAGS = 1 - to force to RGB triplet
* FLAGS = 2 - force to indexed color
* FLAGS |= 4 - to accept multiple colors (can be OR'ed with one of the
* other values)
* hence:
* 0 - (default) means: only scalar color accepted, result is either
* an index or a triplet;
* 1 - means: only scalar color accepted, result is always a triplet;
* 2 - means: only scalar color accepted, result is always an index;
* 4 - means: multiple colors accepted, result is either an index or
* a triplet;
* 5 - means: multiple colors accepted, result is always a triplet;
* 6 - means: multiple colors accepted, result is always an index;
*
* If COLOR is nil, the returned value is PL_FG (index for "fg" color).
*
* The following predefined color names, vales and constants (as Yorick
* global variables) are supported:
*
* NAME INTEGER CONSTANT NAME INTEGER CONSTANT
* --------------------------- -------------------------------
* "bg" 255 -1 PL_BG "magenta" 247 -9 PL_MAGENTA
* "fg" 254 -2 PL_FG "yellow" 246 -10 PL_YELLOW
* "black" 253 -3 PL_BLACK "grayd" 245 -11 PL_GRAYD
* "white" 252 -4 PL_WHITE "grayc" 244 -12 PL_GRAYC
* "red" 251 -5 PL_RED "grayb" 243 -13 PL_GRAYB
* "green" 250 -6 PL_GREEN "graya" 242 -14 PL_GRAYA
* "blue" 249 -7 PL_BLUE "xor" 241 -15 PL_XOR
* "cyan" 248 -8 PL_CYAN "extra" 240 -16 PL_EXTRA
* --------------------------- -------------------------------
*
* Color names are case insensitive and a most X11 color names are
* available (thanks to a builtin database). For instance
* "darkslateblue" and "DarkSlateBlue" are valid color names.
*
*
* SEE ALSO:
* color, pl_get_symbol, pl_get_font, pl_get_palette.
*/
{
/* Parse flags (default is to get a single color in a numerical form
which is usable as the value of the COLOR keyword in Yorick plotting
functions). */
if (is_void(flags)) {
mode = 0; /* any */
single = 1n;
} else {
mode = (flags & 3);
single = ! (flags & 4);
}
/* Parse color value(s). */
/**/extern _PL_COLOR_NAMES, _PL_COLOR_RGB;
if (is_void(color)) {
return (mode == 1 ? _PL_COLOR_RGB(,2) : PL_FG);
}
if ((s = structof(color)) == string) {
dims = dimsof(color);
if (single && dims(1)) error, "color name must be a scalar";
ndx = array(long, dims);
len = strlen(color);
n = numberof(color);
for (k = 1; k <= n; ++k) {
/* Timing Issues: It is slightly faster to use strfind (no string
duplication). Searching the color database, takes ~ 10
microseconds per color on my 2GHz Centrino. */
sel = strfind(color(k), _PL_COLOR_NAMES, case=0);
if (is_array((i = where(! sel(1,..)))) &&
is_array((j = where(sel(2, i) == len(k))))) {
ndx(k) = i(j(1));
} else {
error, ("unrecognized color name: \"" + color(k) + "\"");
}
}
if (! mode) {
return (max(ndx) <= 16 ? char(256 - ndx) : _PL_COLOR_RGB(, ndx));
} else if (mode == 1) {
return _PL_COLOR_RGB(, ndx);
} else {
if (max(ndx) > 16) {
error, "named color is not an indexed one";
}
return char(256 - ndx);
}
}
if (s == char) {
/* Can be an indexed color or a triplet. */
dims = dimsof(color);
n = dims(1);
is_rgb = (n >= 1 && dims(2) == 3);
if (single && n != is_rgb) {
error, "expecting a single color";
}
if (is_rgb) {
if (mode == 2) {
/* FIXME: cannot force triplet to index */
error, "unsupported conversion of [R,G,B] to indexed color";
}
} else if (mode == 1) {
return pl_get_palette()(, 1 + color);
}
return color;
}
if (s == long || s == short || s == int) {
dims = dimsof(color);
if (single && dims(1)) {
error, "expecting a single color";
}
if ((cmin = min(color)) < 0) {
if (cmin < -16) {
error, "out of range indexed color";
}
color = long(color); /* force copy/conversion */
color(where(color < 0)) += 256;
cmin = min(color);
}
cmax = max(color);
if (cmax <= 255) {
/* Assume all colors are indexed ones. */
if (mode == 1) {
return pl_get_palette()(, 1 + color);
}
return char(color);
}
if (mode == 2) {
/* FIXME: cannot force triplet to index */
error, "unsupported conversion of [R,G,B] to indexed color";
}
rgb = array(char, 3, dims);
if (cmin >= 256) {
/* Assume all colors are packed RGB triplets. */
rgb(1, ..) = (color & 0xff);
rgb(2, ..) = ((color >> 8) & 0xff);
rgb(3, ..) = ((color >> 16) & 0xff);
return rgb;
} else {
/* There is a mixture of indexed and packed RGB colors. */
lut = pl_get_palette();
ndx = (color <= 255);
if (is_array((i = where(nxd)))) {
rgb(, i) = lut(, 1 + color(i));
}
if (is_array(( i = where(! nxd)))) {
color = color(i);
rgb(1, i) = (color & 0xff);
rgb(2, i) = ((color >> 8) & 0xff);
rgb(3, i) = ((color >> 16) & 0xff);
}
}
return rgb;
}
error, "bad data type for color";
}
/* Color codes as defined in 'play.h': */
PL_BG = char(255);
PL_FG = char(254);
PL_BLACK = char(253);
PL_WHITE = char(252);
PL_RED = char(251);
PL_GREEN = char(250);
PL_BLUE = char(249);
PL_CYAN = char(248);
PL_MAGENTA = char(247);
PL_YELLOW = char(246);
PL_GRAYD = char(245);
PL_GRAYC = char(244);
PL_GRAYB = char(243);
PL_GRAYA = char(242);
PL_XOR = char(241);
PL_EXTRA = char(240);
local PL_RGB_BG, PL_RGB_FG, PL_RGB_XOR, PL_RGB_EXTRA;
local PL_RGB_BLACK, PL_RGB_WHITE, PL_RGB_RED, PL_RGB_GREEN, PL_RGB_BLUE;
local PL_RGB_CYAN, PL_RGB_MAGENTA, PL_RGB_YELLOW;
local PL_RGB_GRAYD, PL_RGB_GRAYC, PL_RGB_GRAYB, PL_RGB_GRAYA;
func pl_get_palette(win)
/* DOCUMENT rgb = pl_get_palette();
* -or- rgb = pl_get_palette(win);
*
* Returns color table for current graphics window or for winddow WIN if
* it is specified. The color table is a 3-by-256 array of char's such
* that: RGB(1,i), RGB(2,i) and RGB(3,i) are the red, green and blue
* levels for color index i respectively.
*
* Note that some color index cannot be obtained ("bf", "fg", "xor" and
* "extra") and are left as black/white in the returned color table. It
* is possible to change the predefined colors by setting the global
* variables such as PL_RGB_BG with a given [r,g,b] triplet.
*
* SEE ALSO: palette, pl_get_color.
*/
{
local r, g, b, old_win;
if (! is_void(win)) {
old_win = current_window();
window, win;
}
palette, r, g, b, query=1;
lut = array(char, 3, 256);
if (is_array(r)) {
lut(1, 1:numberof(r)) = r;
lut(2, 1:numberof(g)) = g;
lut(3, 1:numberof(b)) = b;
} else {
/* Default is a gray colormap with K=240 colors and there are M=256
* levels. The formula is:
*
* y = (2*M*x + M)/(2*K)
*
* where integer arithmetic must be used and x = 0, ..., K - 1 is the
* 0-based color index and y = 0, ..., M - 1 is the color level.
* After simplification (with K=240 and M=256):
*/
lut( , 1:240) = (indgen(8:3832:16)/15)(-, );
}
lut(, 1 + PL_BG) = PL_RGB_BG;
lut(, 1 + PL_FG) = PL_RGB_FG;
lut(, 1 + PL_BLACK) = PL_RGB_BLACK;
lut(, 1 + PL_WHITE) = PL_RGB_WHITE;
lut(, 1 + PL_RED) = PL_RGB_RED;
lut(, 1 + PL_GREEN) = PL_RGB_GREEN;
lut(, 1 + PL_BLUE) = PL_RGB_BLUE;
lut(, 1 + PL_CYAN) = PL_RGB_CYAN;
lut(, 1 + PL_MAGENTA) = PL_RGB_MAGENTA;
lut(, 1 + PL_YELLOW) = PL_RGB_YELLOW;
lut(, 1 + PL_GRAYD) = PL_RGB_GRAYD;
lut(, 1 + PL_GRAYC) = PL_RGB_GRAYC;
lut(, 1 + PL_GRAYB) = PL_RGB_GRAYB;
lut(, 1 + PL_GRAYA) = PL_RGB_GRAYA;
lut(, 1 + PL_XOR) = PL_RGB_XOR;
lut(, 1 + PL_EXTRA) = PL_RGB_EXTRA;
if (win != old_win) {
window, old_win;
}
return lut;
}
PL_RGB_BG = char([214, 214, 214]);
PL_RGB_FG = char([ 0, 0, 0]);
PL_RGB_BLACK = char([ 0, 0, 0]);
PL_RGB_WHITE = char([255, 255, 255]);
PL_RGB_RED = char([255, 0, 0]);
PL_RGB_GREEN = char([ 0, 255, 0]);
PL_RGB_BLUE = char([ 0, 0, 255]);
PL_RGB_CYAN = char([ 0, 255, 255]);
PL_RGB_MAGENTA = char([255, 0, 255]);
PL_RGB_YELLOW = char([255, 255, 0]);
PL_RGB_GRAYD = char([100, 100, 100]);
PL_RGB_GRAYC = char([150, 150, 150]);
PL_RGB_GRAYB = char([190, 190, 190]);
PL_RGB_GRAYA = char([214, 214, 214]);
PL_RGB_XOR = char([ 0, 0, 0]);
PL_RGB_EXTRA = char([ 0, 0, 0]);
local PL_COURIER, PL_TIMES, PL_HELVETICA, PL_SYMBOL, PL_NEWCENTURY;
local PL_GUI_FONT, PL_BOLD, PL_ITALIC, PL_OPAQUE;
local _PL_FONT_TABLE;
func pl_get_font(value, default)
/* DOCUMENT pl_get_font(value, default);
* Parse font VALUE which can have any value recognized by Yorick
* for the "font" keyword in builtin plotting functions and return the
* corresponding integer value. In addition, if VALUE is void, DEFAULT
* is returned.
*
* SEE ALSO:
* font, pl_get_color, , pl_get_symbol, xwindow.
*/
{
extern _PL_FONT_TABLE;
if (is_void(value)) return default;
if (is_array(value) && ! dimsof(value)(1)) {
if ((s = structof(value)) == long) return value;
if (s == string) {
n = strmatch(value, "B") | 2*strmatch(value, "I");
fn = (n==3 ? strpart(value, 1:-2) : (n ? strpart(value, 1:-1) : value));
if (is_func(h_new)) {
if (! is_hash(_PL_FONT_TABLE)) {
_PL_FONT_TABLE = h_new("courier",1, "times",2, "helvetica",3,
"symbol",4,"schoolbook",5);
}
index = _PL_FONT_TABLE(fn);
if (index) return n + 4*(index - 1);
} else {
if (fn == "courier") return n;
if (fn == "times") return n+4;
if (fn == "helvetica") return n+8;
if (fn == "symbol") return n+12;
if (fn == "schoolbook") return n+16;
}
error, "bad font name \""+value+"\"";
}
if (s == char || s == short || s == int) return long(value);
}
error, "bad font value";
}
/* Font codes/flags as defined in 'play.h': */
PL_COURIER = 0;
PL_TIMES = 4;
PL_HELVETICA = 8;
PL_SYMBOL = 12;
PL_NEWCENTURY = 16;
PL_GUI_FONT = 20;
PL_BOLD = 1;
PL_ITALIC = 2;
PL_OPAQUE = 32;
local PL_SQUARE, PL_PLUS, PL_TRIANGLE, PL_UP_TRIANGLE, PL_CIRCLE;
local PL_DIAMOND, PL_CROSS, PL_DOWN_TRIANGLE, PL_STAR;
local _PL_SYMBOL_TABLE;
func pl_get_symbol(symbol)
/* DOCUMENT pl_get_symbol(symbol);
*
* Get symbol value as an integer, SYMBOL must be a scalar and may be either
* an integer, a character or a string:
*
* CHAR STRING DESCRIPTION
* ------------------------------------------------------------------
* nothing (just draw error bars if any)
* # "square" a square
* + "plus" a plus sign
* ^ "triangle" "uptriangle" a triangle
* o "circle" a circle (actually an hexagon)
* @ "diamond" a square rotated by 45 degrees
* x "cross" an X-cross <- this is the default
* v "downtriangle" an upside down triangle
* * "star" a star
* ----------------------------------------------------------------------
*
* The one-character symbol may given as lower/upper case and as a string
* or a char; e.g. 'v', 'V', "v" and "V" all stand for an upside down
* triangle.
*
* For convenience, global variables PL_SQUARE, PL_PLUS, PL_TRIANGLE,
* PL_UP_TRIANGLE, PL_CIRCLE; local PL_DIAMOND, PL_CROSS,
* PL_DOWN_TRIANGLE and PL_STAR are defined with the corresponding symbol
* code.
*
* SEE ALSO: plp, pl_get_color.
*/
{
if (is_void(symbol)) {
return 6;
}
if (! is_array(symbol) || dimsof(symbol)(1)) {
error, "symbol must be a scalar";
}
s = structof(symbol);
if (s == string || s == char) {
if (is_func(h_new)) {
/* Use Yeti hash-table to speed-up symbol identification. */
extern _PL_SYMBOL_TABLE;
if (! is_hash(_PL_SYMBOL_TABLE)) {
_PL_SYMBOL_TABLE = h_new("square",P_SQUARE, "#",P_SQUARE,
"plus",P_PLUS, "+",P_PLUS,
"triangle",P_TRIANGLE,
"uptriangle",P_TRIANGLE_UP,
"^",,P_TRIANGLE_UP,
"circle",P_CIRCLE,
"o",P_CIRCLE, "O",P_CIRCLE,
"diamond",P_DIAMOND, "@",P_DIAMOND,
"cross",P_CROSS, "x",P_CROSS, "X",P_CROSS,
"downtriangle",P_TRIANGLE_DOWN,
"v",P_TRIANGLE_DOWN, "V",P_TRIANGLE_DOWN,
"star",P_STAR, "*",P_STAR);
}
if (s == char) {
symbol = strchar(symbol);
}
symbol = _PL_SYMBOL_TABLE(symbol);
if (symbol) {
return symbol;
}
} else {
/* Use vanilla Yorick. */
local c;
if (s == char) {
len = 1;
eq_nocopy, c, symbol;
} else {
len = strlen(symbol);
c = strchar(symbol)(1);
}
if (len == 1) {
if (c=='#') return P_SQUARE;
if (c=='+') return P_PLUS;
if (c=='^') return P_TRIANGLE_UP;
if (c=='o' || c =='O') return P_CIRCLE;
if (c=='@') return P_DIAMOND;
if (c=='x' || c=='X') return P_CROSS;
if (c=='v' || c=='V') return P_TRIANGLE_DOWN;