5
$\begingroup$

I am new to Mathematica and want to change the FillingStyle Dynamically in this Plot. I want that when I pick a color from the "Color" then, it should dynamically change in the Sin plot.

 a = RGBColor[1, 2, 0]
 Row[{Button["<<Color>>", a = SystemDialogInput["Color"]], 
Evaluate[Plot[Sin[m], {m, -Pi, Pi}, Filling -> Bottom, 
FillingStyle -> Dynamic[a]]]}]

I am able to pick a color but the plot is not changing dynamically. But, it changes when I am executing it again.

$\endgroup$
11
$\begingroup$

The expression FillingStyle -> value is not preserved in the output Graphics expression of Plot, therefore it cannot be changed after-the-fact in that fashion. Instead you need to regenerate the plot when the value of a changes, meaning that Dynamic needs to surround Plot:

a = RGBColor[1, 2, 0]
Row[{Button["<<Color>>", a = SystemDialogInput["Color"]], 
  Dynamic @ Plot[Sin[m], {m, -Pi, Pi}, Filling -> Bottom, FillingStyle -> a]}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ See my answer for how to do it without recalculating the plot. $\endgroup$ – ssch Nov 28 '12 at 15:37
  • $\begingroup$ @ssch Strangely despite your comment I only saw your answer now. Belated +1. $\endgroup$ – Mr.Wizard May 30 '15 at 17:41
8
$\begingroup$

In case someone with a possibly slow to generate plot want to do this it is possible without recalculating the plot.

Looking at the GraphicsComplex part of the plot:

p = Plot[Sin[m], {m, -Pi, Pi}, Filling -> Bottom, FillingStyle -> Blue];
p[[1]]
(* GraphicsComplex[{{-3.14159, -1.28228*10^-7}, ... }}, ..., RGBColor[0, 0, 1]...] *)

One can guess that the RGBColor part is replaceable

a = RGBColor[0, 0, 1];
p = Plot[Sin[m], {m, -Pi, Pi}, Filling -> Bottom, 
   FillingStyle -> a]/.a->Dynamic[a];

Row[{Button["<<Color>>", a = SystemDialogInput["Color"]], 
  p}]

plot

Looks a bit odd with the blue line so lets change PlotStyle as well. Note that even with a very slow function the plot can be regenerated instantly:

c = RGBColor[0, 0, 1];(*Initial color*)
p = Plot[Pause[0.01]; Sin[m], {m, -Pi, Pi}, Filling -> Bottom, 
   FillingStyle -> c, PlotStyle -> c] /. c -> Dynamic[c];

Column[{ColorSlider[Dynamic[c]], p}]

enter image description here

$\endgroup$
6
$\begingroup$

Or you could just use Manipulate:

Manipulate[
   Plot[Sin[m], {m,-Pi,Pi}, Filling -> Bottom, FillingStyle->color],
  {color,RGBColor[1,2,0]}]

enter image description here

$\endgroup$
5
$\begingroup$

PaneSelector

 Column[{PaneSelector[{False -> 
   Panel[Style["filling color...", "Subsection", FontColor -> Dynamic[color]]], 
   True -> ColorSlider[Dynamic[color],  AppearanceElements -> "SwatchSpectrum"]},
   Dynamic[CurrentValue["MouseOver"]]],
 Dynamic@Plot[ x Cos[x], {x, -2 Pi, 2 Pi}, ImageSize -> 500,
    Filling -> Axis,  FillingStyle -> color]}]

enter image description here

Overlay

Dynamic@Overlay[{PaneSelector[{False -> 
      Plot[x Cos[x], {x, -2 Pi, 2 Pi}, ImageSize -> 500, 
       Filling -> Axis, FillingStyle -> color], 
     True -> ColorSlider[Dynamic@color, AppearanceElements -> "SwatchSpectrum"]}, 
    CurrentValue["MouseOver"]], 
   Plot[x Cos[x], {x, -2 Pi, 2 Pi}, ImageSize -> 500, Filling -> Axis,
     FillingStyle -> color]}, All, 1]

enter image description here

$\endgroup$
  • $\begingroup$ I answered this question in the most simple way possible (feeling that was probably most helpful to the author) and never returned to it. Nice to see that others took it deeper, even if I only notice now. $\endgroup$ – Mr.Wizard May 30 '15 at 17:40

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.