Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers 4

up vote 7 down vote accepted

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

share|improve this answer
    
See my answer for how to do it without recalculating the plot. –  ssch Nov 28 '12 at 15:37
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment
 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

on mouseover:

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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