3
$\begingroup$

I have this Plot:

Plot[{Sin[x], 3 Sin[x], Sin[x]/2}, {x, -2 Pi, 2 Pi}, 
ImageSize -> Large, AxesLabel -> {HoldForm[x], HoldForm[y]}, 
PlotLegends -> "Expressions", PlotStyle -> {Blue, Red, Green}]

In the graphic result, there is sin(x)/2

Is there a way to change it in 1/2 sin(x)?

$\endgroup$
  • $\begingroup$ The simplest way is to set the legend manually, and type in the form that you prefer. $\endgroup$ – Szabolcs May 11 '17 at 8:32
  • $\begingroup$ @Szabolcs If I am not in the Plot function, but I am somewhere else, is there a way to do that? And by the way, how can I set the legend manually? $\endgroup$ – Ale1794 May 11 '17 at 8:37
  • 1
    $\begingroup$ You can use PlotLegends -> {Sin[x], 3 Sin[x], HoldForm[1/2] Sin[x]}. I do not understand your other question: "If I am not in the Plot function, but I am somewhere else" $\endgroup$ – Szabolcs May 11 '17 at 8:41
  • $\begingroup$ @Szabolcs I think you answer that, nominating HoldForm $\endgroup$ – Ale1794 May 11 '17 at 8:48
  • $\begingroup$ @Szabolcs I meant if i would like to write somewhere 1/2*sin[x] and not sin[x]/2 $\endgroup$ – Ale1794 May 11 '17 at 8:52
1
$\begingroup$

Here are a couple of ways.

1. We post-process the output. One has to take care, since we change the expression, that the result makes sense. The $excludedForms indicate expressions not to be altered. For a general purpose function, this might be quite long. For the OP's problem it need only be _Graphics.

$excludedForms = _Graphics | _Graphics3D  (* |_Rule|_InterpolatingFunction etc. *); 
fractionForm = # /.
  {x : $excludedForms :> x, HoldPattern[Times][c__?NumericQ, r__] :> Row[{c, r}, " "]} &;

2. This is more general, but overrides the built-in formatting process for Times.

ClearAll[fractionForm];
fractionForm /: MakeBoxes[fractionForm[expr_], form_] :=     
 Internal`InheritedBlock[{Times},
  Unprotect[Times]; 
  Times /: MakeBoxes[HoldPattern[Times][c__?NumericQ, r__], f_] := 
    MakeBoxes[Row[{c, r}, " "], f];
  Protect[Times];
  MakeBoxes[expr, form]
  ];

In both cases, we get this:

Plot[{Sin[x], 3 Sin[x], Sin[x]/2}, {x, -2 Pi, 2 Pi}, 
  AxesLabel -> {HoldForm[x], HoldForm[y]}, 
  PlotLegends -> "Expressions", PlotStyle -> {Blue, Red, Green}
  ] // fractionForm

Mathematica graphics

$\endgroup$
1
$\begingroup$

This does it automatically for this example.

exp = {Sin[x], 3 Sin[x], Sin[x]/2};
Plot[exp, {x, -2 Pi, 2 Pi}, 
 ImageSize -> Large, AxesLabel -> {HoldForm[x], HoldForm[y]}, 
 PlotLegends -> (exp /. Rational[a_, b_] :> HoldForm[a/b]), 
 PlotStyle -> {Blue, Red, Green}]

No idea how generally this will work.

$\endgroup$
1
$\begingroup$

In:

Plot[{Sin[x], 3 Sin[x], Sin[x]/2}, {x, -2 Pi, 2 Pi}, 
 ImageSize -> Large, AxesLabel -> {HoldForm[x], HoldForm[y]}, 
 PlotLegends -> {"sin(x)", "3 sin(x)", "1/2 sin(x)"}, 
 PlotStyle -> {Blue, Red, Green}]

Out:

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.