I want to create a plot style with a table and manipulate the variables in the plot style.

Menge = {g, h};
  Plot[{0.5 x + 1, x}, {x, -1, 5},
    PlotStyle -> Table[Opacity[Menge[[i]]], {i, 1, Length[Menge]}]],
{g, {1, 0}}, 
{h, {1, 0}}]

This code won't work. But when i create the table and simply copy the output it works.
Can someone help me out?

  • $\begingroup$ ah sorry. i mixed up the variables. That's just a small example. I justs want the PlotStyle to be generated from a set like Menge. The arguments for the Manipulate should also be generated from Menge. This i have already working, but i'm struggling with the plotstyle. $\endgroup$ – ralpho1337 Dec 7 '13 at 9:18
  • 1
    $\begingroup$ g inside Manipulate is not the same as the one in Menge. Manipulate is scoping h ang g. Try adding Initialization :> (Menge = {g, h};) to the manipulate. $\endgroup$ – Kuba Dec 7 '13 at 9:26

Is this what you are looking for?

Edit: Menge outside as OP wants.

Menge := {g, h};
 Dynamic@Plot[{0.5 x + 1, x}, {x, -1, 5}, PlotStyle -> Opacity /@ Menge],
 {g, {1, 0}}, {h, {1, 0}}, 
 LocalizeVariables -> False]
  • $\begingroup$ nearly. I want the Elements of Menge to come from "outside" of the manipulate. In my sheet Menge is a quite big set which is generated dynamically. The arguments for the Manipulate i generate with $Evaluate@(Sequence @@ Table[{Menge[[i]], {1, 0}}, {i, 1, Length[Menge]}])$ and now i also want to generate the PlotStyle dynamically to show and hide my plotted functions which i generated earlier. $\endgroup$ – ralpho1337 Dec 7 '13 at 9:38
  • $\begingroup$ @user11004 Ok, so you can use LocalizeVariables->False or,what I strongly recommend, use DynamicModule to have full control what is important for bigger projects. $\endgroup$ – Kuba Dec 7 '13 at 9:41
  • $\begingroup$ Thank you! LocalizeVariables solved my problem. But i will have a look at DynamicModule! Thanks for your patience! $\endgroup$ – ralpho1337 Dec 7 '13 at 9:44

I really don't see the value of having a list of opacity control variables external to the Manipulate expression. Were you to change the external list, you would still have to edit the Manipulate to add or remove slider controls to support your changes. So why not just do it all in the Manipulate expression?

  Dynamic @ Plot[{0.5 x + 1, x}, {x, -1, 5}, PlotStyle -> Opacity /@ vars],
  {{op1, 1, "g-opacity"}, 0, 1, .1, Appearance -> "Labeled"},
  {{op2, 1, "h-opacity"}, 0, 1, .1, Appearance -> "Labeled"},
  {vars, ControlType -> None},
  Initialization :> (vars := {op1, op2})]



If you want Menge to be global then the minimum alteration to what you have done to get this to work would be this:

 Dynamic@Plot[{0.5 x + 1, x}, {x, -1, 5},
   PlotStyle -> Opacity /@ Menge[g, h]],
 {{g, 1}, 0, 1},
 {{h, 1}, 0, 1},
 Initialization :> {Menge[g_, h_] := {g, h}}]

enter image description here

I always would recommend that you have your external function(s) within Initialization. From there you can add other things to make it look pretty.


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.