SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 2415|回复: 1
打印 上一主题 下一主题

用 IML 和 Heatmap 画圣诞树

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2013-12-23 10:16:40 | 只看该作者

用 IML 和 Heatmap 画圣诞树

[code:2oen02v3]proc iml;
/* define helper functions ROW and COL */
start row(x);       /* return matrix m such that m[i,j] = i */
   return( repeat( T(1:nrow(x)), 1, ncol(x) ));
finish;
start col(x);       /* return matrix m such that m[i,j] = j */
   return( repeat(1:ncol(x), nrow(x)) );
finish;

/* parameters for the tree dimensions */
h = 100; w = h+1; b = int(h/10);
M = j(w, h, .);         /* initialize h x w matrix to missing */
x = col(M);             /* column numbers */
y = w + 1 - row(M);     /* reverse Y axis */

/* define the leafy portion of the tree */
TreeIdx = loc(y>b & y<=2*x & y<=-2*x+2*h); /* a triangle */
M[TreeIdx] = 0;

/* place lights randomly within tree */
N = int(0.12*ncol(TreeIdx)); /* use 12% of tree area */
call randseed(1225);
do i = 1 to 3;               /* 3 colors */
   idx = sample(TreeIdx, N, "WOR"); /* sample without replacement */
   M[idx] = i;               
end;

/* define the trunk */
width = int(b/2);
TrunkIdx = loc( y<= b & abs(x-nrow(M)/2)<width );
M[TrunkIdx] = 4;

/* write matrix in "long form" to SAS data set */
Row = row(M);                    /* row index */
Col = col(M);                    /* col index */
create Tree var {"Row" "Col" "M"};  append;  close Tree;
quit;

proc template;
define statgraph HeatmapTree;
dynamic _X _Y _Z;
begingraph;
   discreteattrmap name="christmastree";
      value '.' / fillattrs=(color=WHITE);    /* background color */
      value '0' / fillattrs=(color=GREEN);    /* tree color       */
      value '1' / fillattrs=(color=RED);      /* ornament color 1 */
      value '2' / fillattrs=(color=BLUE);     /* ornament color 2 */
      value '3' / fillattrs=(color=YELLOW);   /* ornament color 3 */
      value '4' / fillattrs=(color=BROWN);    /* tree trunk color */
   enddiscreteattrmap;
   discreteattrvar attrvar=Alias var=_Z attrmap="christmastree";
   layout overlay / xaxisopts=(type=discrete display=none)
                    yaxisopts=(type=discrete display=none reverse=true);
      heatmapparm x=_X y=_Y colorgroup=Alias /
                    xbinaxis=false ybinaxis=false primary=true;
   endlayout;
endgraph;
end;
run;

ods graphics / width=500 height=800;
proc sgrender data=Tree template=HeatmapTree;
   dynamic _X="Col" _Y="Row" _Z="M";
run;[/code:2oen02v3]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2014-1-4 20:08:32 | 只看该作者

Re: 用 IML 和 Heatmap 画圣诞树

"A Christmas tree matrix" by Rick Wicklin
[url:r2sb7gnz]http://blogs.sas.com/content/iml/2013/12/18/christmas-tree-matrix/[/url:r2sb7gnz]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2024-4-20 07:26 , Processed in 0.213199 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表