2012-01-02 8 views
6

exp、2^x、3^xのような複数の関数を描画すると、各関数のラベルを生成できますか?今mathematicaで関数名を自動的に生成する方法は?

私のコードは:

Plot[{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic, PlotStyle -> {Red, Green, Blue}] 

私は何を意味することは、それが何であるかの機能をユーザーに伝えるために、この場合は3つのラベルを生成します。

のような:

enter image description here

がどのようにこれを生成するのですか?

+0

それは[]の代わりにプロットのグラフィックスを使用するが、[これ](http://stackoverflow.com/a/7547457/353410)はあなたにいくつかのアイデアを与えるかもしれない –

+2

[Mathematicaの異なるカーブにどのようにラベルを付けるのですか?](http://stackoverflow.com/questions/7221315/how-do-i-label-different-curves-in -mathematica) – abcd

答えて

4

おそらく、次のようになります。PlotTooltipを使用して、ツールチップでGraphicsオブジェクトを生成します。次いで、所望の位置に所望のテキストを配置するために、ツールチップを書き換える:

Plot[ 
[email protected]{Exp[x], 2^x, 3^x}, {x, -5, 2}, 
AspectRatio -> Automatic, 
PlotStyle -> {Red, Green, Blue}, 
PlotRange -> All, 
PlotRangePadding -> 1.1] /. 
{ 
Tooltip[{_, color_, line_}, tip_] 
:> 
{Text[Style[tip, 14], {.25, 0} + line[[1, -1]]], color, line} 
} 

Mathematica graphics

+0

これはシンボリックグラフィックスオブジェクトの置換えの力を本当に美しく巧みに使います。私たちは通常、このようにすべての後処理を行います。 –

1

一つの方法は、PlotLegends

を使用することです(私はあまりそれを好きではないが、それはあなたが欲しいものを行うための簡単な方法です)

<< PlotLegends` 
Clear[x]; 
funs = {Exp[x], 2^x, 3^x}; 
legends = Map[[email protected][#, "TR", 12] &, funs]; 
Plot[[email protected], {x, -5, 2}, AspectRatio -> Automatic, 
PlotStyle -> {Red, Green, Blue}, PlotLegend -> legends] 

enter image description here

がにヘルプを参照してください凡例をもっとカスタマイズしてください。上記はデフォルトを使用しています。 Tooltip表示ラベル付き

http://reference.wolfram.com/mathematica/PlotLegends/tutorial/PlotLegends.html

3

別の方法は、マウスポインタは、関数のグラフにある間:

Plot[[email protected]{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic, 
              PlotStyle -> {Red, Green, Blue}] 
4

私はルールが何であるかわからない、同じ質問に対して、別の答えを別のものを追加するための。しかし、これを行う別の方法があります。これを私の最初の答えに加えることになったら、私はそれをすることができます。

テキストコマンドを使用して、手作業でテキストラベルを追加できます。私はそれがより良く見えると思う。

Clear[x]; 
funs = {Exp[x], 2^x, 3^x}; 
funNames = Style[#, 12] & /@ funs; 

(*the x-axis plot range used *) 
from = -5; to = 2; 

(* generate the coordinates at the end of the plot lines*) 
pos = Map[{to, #} &, funs /. x -> to]; 

(*generate the text labels *) 
text = Map[Text[#[[1]], #[[2]], {-1, 0}] &, Thread[{funNames, pos}]]; 

プロット最終的な結果は、( こと追加のラベルが完全に見られるように、範囲をプロットするパディングのほとんどを添加)

Plot[funs, {x, from, to}, 
PlotRangePadding -> {1, 1}, 
PlotStyle -> {Red, Green, Blue}, 
PlotRange -> All, 
Epilog -> text 
] 

enter image description here

更新(:ここで一つの方法であります1)

サムは以下のように簡単な方法で質問しました。私は今確信していません。しかし、このメソッドを使いやすくするための1つの方法は、関数を作成し、この関数を一度呼び出すだけでTextラベルを生成することです。あなたはいつもあなたが使用している他のすべての関数を置いて、それを呼び出すだけでこの関数を置くことができます。まず

(*version 1.1*) 

myLegend[funs_List,     (*list of functions to plot*) 
    x_,         (*the independent variable*) 
    from_?(NumericQ[#] && Im[#] == 0 &),(*the x-axis starting plot range*) 
    to_?(NumericQ[#] && Im[#] == 0 &) (*the x-axis ending plot range*) 
    ] := Module[{funNames, pos, text, labelOffset = -1.3}, 

    (*make label names*) 
    funNames = Style[#, 12] & /@ funs; 

    (*generated the coordinates at the end of the plot lines*) 
    pos = Map[{to, #} &, funs /. x -> to]; 

    (*generate the Text calls*) 
    text = Map[Text[#[[1]], #[[2]], {labelOffset, 0}] &, 
    Thread[{funNames, pos}]] 
    ]; 

関数を記述そして今、ちょうどあなたがラベルにプロットしたい、いつでも上記の呼び出し:ここ

は何かです。それはちょうど1〜2行余分なコードになります。このように:

enter image description here

あなたが好きなあなたはそれを変更することができます。

Clear[x] 
from = -5; to = 2; 
funs = {Exp[x], 2^x, 3^x}; 

Plot[funs, {x, from, to}, PlotRangePadding -> {1, 1}, 
PlotStyle -> {Red, Green, Blue}, PlotRange -> All, 
Epilog -> myLegend[funs, x, from, to]] 

enter image description here

は、ここではいくつかの例です。

+0

それはとてもいいようですが、私にとってはちょっと複雑すぎます。それを行う簡単な方法はありますか?ありがとう〜 – sam

関連する問題