1
$\begingroup$

I have:

Manipulate[
 If[reflection, Plot[{-func}, {x, -3, 3},
   PlotStyle -> Red,
   PlotRange -> {{-3, 3}, {-3, 3}},
   GridLines -> Automatic,
   PlotLabel -> StringForm["f(x) = ``", -func]],
  Plot[{func}, {x, -3, 3},
   PlotStyle -> Blue,
   PlotRange -> {{-3, 3}, {-3, 3}},
   GridLines -> Automatic,
   PlotLabel -> StringForm["f(x) = ``", func]]],
 {func, {Sqrt[x], Abs[x], x^2, x^3}},
 {reflection, {False, True}}]

I'd like to make my plot label red when reflection is true and blue if reflection is false. How do you add color to StringForm["f(x) = ``", func]?

$\endgroup$

1 Answer 1

4
$\begingroup$

You can use LabelStyle -> Directive[Bold, Red] and LabelStyle -> Directive[Bold, Blue]

Manipulate[
 If[reflection, 
  Plot[{-func}, {x, -3, 3}, PlotStyle -> Red, 
   PlotRange -> {{-3, 3}, {-3, 3}}, GridLines -> Automatic, 
   PlotLabel -> StringForm["f(x) = ``", -func], 
   LabelStyle -> Directive[Bold, Red]], 
  Plot[{func}, {x, -3, 3}, PlotStyle -> Blue, 
   PlotRange -> {{-3, 3}, {-3, 3}}, GridLines -> Automatic, 
   PlotLabel -> StringForm["f(x) = ``", func], 
   LabelStyle -> Directive[Bold, Blue]]], {func, {Sqrt[x], Abs[x], 
   x^2, x^3}}, {reflection, {False, True}}]

red

EDIT

Per request, changing the label and not the ticks can be done:

Individually styled labels can be used with LabelStyle and have higher priority -a mathematica help page

So we change the label directive to one color and wrap the label in a style, which is given priority:

If[reflection,
  Plot[{-func}, {x, -3, 3}, PlotStyle -> Cyan, 
   PlotRange -> {{-3, 3}, {-3, 3}}, GridLines -> Automatic,
   PlotLabel -> Style[StringForm["f(x) = ``", -func], Red],   
   LabelStyle -> Directive[Bold, Magenta]], 
  Plot[{func}, {x, -3, 3},
   PlotStyle -> Cyan, PlotRange -> {{-3, 3}, {-3, 3}}, 
   GridLines -> Automatic, 
   PlotLabel -> Style[StringForm["f(x) = ``", -func], Blue], 
   LabelStyle -> Directive[Bold, Magenta]]], {func, {Sqrt[x], Abs[x], 
   x^2, x^3}}, {reflection, {False, True}}]

diff colors

diff colors2

$\endgroup$
3
  • $\begingroup$ Very easy solution. But I think I would like just my plot label to change colors and note the ticks. But thanks for a very nice easy answer. $\endgroup$
    – David
    Feb 9, 2016 at 18:03
  • $\begingroup$ @David also possible! check out the edits- I used outlandish colors to make each part obvious. $\endgroup$ Feb 9, 2016 at 18:19
  • $\begingroup$ Thanks. These are some very nice options. $\endgroup$
    – David
    Feb 10, 2016 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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