Stats

Popular Posts

Followers

Mathematica 教學:Color Bar

戴忠淵 於 2012年4月17日星期二 下午10:47 發表


colorbarplot[fun_,xrange_List,yrange_List,color_String]:=
Block[{tt,myvalue,mycolor,colorbar,myticks,mycolorbar},

(* ContourPlot *)
tt=ContourPlot[fun,{x,xrange[[1]],xrange[[2]]},{y,yrange[[1]],yrange[[2]]},
ColorFunction->color];

(* 取出ContoourPlot所有等高線的值 *)
myvalue=Cases[tt,Tooltip[pts__,v_]:>v,Infinity];

(* 取出ContoourPlot所有等高線的顏色 *)
mycolor=Cases[tt,RGBColor[a_,b_,c_],Infinity];

(* 建立color bar *)
colorbar=Flatten[{mycolor[[#+1]],Rectangle[{0,#},{1,#+1}]}&/@
Range[0,Length@myvalue]];
myticks=Transpose[{Reverse@Range[0,Length@myvalue+1],
Flatten@{myvalue[[1]]+myvalue[[1]]-myvalue[[2]], myvalue,
myvalue[[-1]]-myvalue[[1]]+myvalue[[2]]}}];
mycolorbar=Plot[0,{x,0,1},
Axes->None,
Frame->{{None,True},{None,None}},
FrameTicks->{{None,myticks},{None,None}},
Epilog->colorbar,
PlotRange->{{0,1},{0,Length@myvalue+1}},
AspectRatio->Length@myvalue+1];

(* 將ContoourPlot以及color bar合併 *)
Labeled[tt,mycolorbar,{Right}]
]

(* Example *) 

colorbarplot[x^2+y^2,{-2Pi,2Pi},{-2Pi,2Pi},"Rainbow"]



Tags: , ,

讀者回應 ( 0 意見 )

發佈留言

Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.

如果這篇文章對你有幫助,那請留個訊息給我~