# How to change color dynamically?

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.

-

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]}]


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

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]}]


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]


-
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. – Mr.Wizard May 30 '15 at 17:40

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}]


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}]


-

Or you could just use Manipulate:

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


-