Stats

Popular Posts

Followers

Mathematica 教學 Legend for Plot without PlotLegend

戴忠淵 於 2011年7月4日星期一 下午7:27 發表



Mathematica Legend for Plot without PlotLegend

mylegend[plot_Graphics, legend_List] :=Block[{p = plot, l = legend, color, temp},
(* 擷取色彩 *)
color = Cases[p, Hue[a_, b_, c_] :> Hue[a, b, c], Infinity];
(* 配對圖例及色彩 *)
temp = {color[[#]], l[[#]]} & /@ Range[Length@color];
(* 建立圖例 *)
Labeled[p,
Grid[{Graphics[{#[[1]], Thickness[0.1], Line[{{0, 0}, {1, 0}}]},
ImageSize -> {24, 24}, AspectRatio -> 8/24, ImagePadding -> 0],
#[[2]]} & /@ temp],
(* 指定圖例位置 *)
{{Right, Top}}]
]

p1 = Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi}];

mylegend[p1,{Sin,Cos}]

p2=ListLinePlot[Accumulate@RandomReal[{-1,1},100]&/@Range[5]]

mylegend[p2,{"Series-1","Series-2","Series-3","Series-4","Series-5"}]

p3=ContourPlot[Evaluate[x^2+y^2==#&/@Range[5]],{x,-3,3},{y,-3,3}]

mylegend[p3,{"Circle-1","Circle-2","Circle-3","Circle-4","Circle-5"}]




Tags:

讀者回應 ( 0 意見 )

發佈留言

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

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