# Trouble with syntax combining Plot and Switch

**URL:** <https://community.wolfram.com/t/trouble-with-syntax-combining-plot-and-switch/21602>\
**Category:** Questions\
**Tags:** graphics-and-visualization\
**Created:** [February 14, 2023, 4:38pm UTC](https://community.wolfram.com/t/trouble-with-syntax-combining-plot-and-switch/21602 "2023-02-14T16:38:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gernotheissel](https://avatars.discourse-cdn.com/v4/letter/g/8797f3/32.png) [@gernotheissel](https://community.wolfram.com/u/gernotheissel)\
**Post date:** [February 14, 2023, 4:38pm UTC](https://community.wolfram.com/t/trouble-with-syntax-combining-plot-and-switch/21602/1 "2023-02-14T16:38:47Z")

</div>

Hi! If I

```
Plot[{Sin[x],Cos[x]},{x,0,2π}]

```

then I get the sine plotted in the default blue, and the cosine in the default orange. The command

```
key="one";
Switch[key,"one",{Sin[x],Cos[x]},"two",{-Sin[x],-Cos[x]}]

```

gives `{Sin[x],Cos[x]}`. Following this up with

```
Plot[%,{x,0,2π}]

```

results again in both curves being plotted in different colors. However

```
Plot[Switch[key,"one",{Sin[x],Cos[x]},"two",{-Sin[x],-Cos[x]}],{x,0,2π}]

```

results in both curves being plotted in the same color, and I could not manage to manually change that by using the option `PlotStyle ` option.

Why is that so, and how can I change the plot colors if I use Plot with Switch in this way?

An example notebook is attached!  
Thanks for help! : )

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/78f5cdd3-d483-402d-bee6-bdcf3c53151f)

---

<div class="post-metadata">

**Author:** ![hansmilton](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hansmilton/32/8656_2.png) [@hansmilton](https://community.wolfram.com/u/hansmilton)\
**Post date:** [February 14, 2023, 5:28pm UTC](https://community.wolfram.com/t/trouble-with-syntax-combining-plot-and-switch/21602/2 "2023-02-14T17:28:58Z")

</div>

It works if `Evaluate` is used to, well, evaluate the `Switch` argument before assigning values to _x_.

```
key="one";
Plot[Evaluate@Switch[key,"one",{Sin[x],Cos[x]},"two",{-Sin[x],-Cos[x]}],{x,0,2*Pi}]

```

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [February 14, 2023, 5:44pm UTC](https://community.wolfram.com/t/trouble-with-syntax-combining-plot-and-switch/21602/3 "2023-02-14T17:44:41Z")

</div>

Plot needs to hold its arguments. The first argument is an expression that needs to be evaluated for each sample point. If it were evaluated immediately, it might no longer be in a form suitable for substituting the sample points. Now, I don’t know why, but it seems like Plot commits to the color scheme based on the number of plot expressions it initially sees. So, if you pass in a list, it seems like it’ll count the number of items in the list and use that number to determine how many different colors to use. In your case, you’ve passed in a single expression (the Switch–remember, it’s being held, so Plot has no idea that it will eventually evaluate to a list), so Plot (apparently–I’m really just guessing here) commits to a single color.

The most common workaround for situations like these is to wrap the expression you want to plot in Evaluate:

```
Plot[Evaluate[
  Switch[key, "one", {Sin[x], Cos[x]}, "two", {-Sin[x], -Cos[x]}]], {x, 0, 2 \[Pi]}]

```

But you need to be careful with this strategy and ensure that after a forced evaluation the expression will still be in a form that Plot can use.

---

<div class="post-metadata">

**Author:** ![gernotheissel](https://avatars.discourse-cdn.com/v4/letter/g/8797f3/32.png) [@gernotheissel](https://community.wolfram.com/u/gernotheissel)\
**Post date:** [February 14, 2023, 10:00pm UTC](https://community.wolfram.com/t/trouble-with-syntax-combining-plot-and-switch/21602/4 "2023-02-14T22:00:31Z")

</div>

I think your guess might be right here! : ) In any case, the solution works. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![gernotheissel](https://avatars.discourse-cdn.com/v4/letter/g/8797f3/32.png) [@gernotheissel](https://community.wolfram.com/u/gernotheissel)\
**Post date:** [February 14, 2023, 10:00pm UTC](https://community.wolfram.com/t/trouble-with-syntax-combining-plot-and-switch/21602/5 "2023-02-14T22:00:50Z")

</div>

Thank you so much!
