# How can I set PlotStyle for Show?

Consider:

pltabsall = Show[
pltAbs1001, pltAbs1005, pltAbs1006, pltAbs1007, pltAbs1008,
pltAbs1009, pltAbs1010, pltAbs1011, pltAbs1012, pltAbs1013,
pltAbs1014, pltAbs1015, pltAbs1016, pltAbs1017, pltAbs1018,
pltAbs1019, pltAbs1020, pltAbs1021, pltAbs1022, pltAbs1022,
pltAbs1023, pltAbs1024, pltAbs1025, pltAbs1026, pltAbs1027,
pltAbs1028, pltAbs1029, pltAbs1030, pltAbs1031, pltAbs1034,
pltAbs1035, pltAbs1036, pltAbs1037, pltAbs1039, pltAbs1040,
pltAbs1041]


Each plt is a plot, and I want to use Show and to automatic change plotstyle for all subplots, i.e. the color of each trace; is it possible? Without changing the settings from inside of PltAbs10xx?

• Why couldn't you set PlotStyle in each of the pltAbs objects? – J. M.'s ennui May 10 '16 at 12:50
• It is not clear, what do you want to achieve. Do you need to vary all plot styles simultaneously, or separately for different plts? Dynamically, or by multiple evaluations? What about this approach: a = {Thin, Dashed}; b = {Red, Dotted}; Show[{Plot[Sin[x], {x, 0, 2 \[Pi]}, PlotStyle -> a], Plot[Cos[x], {x, 0, 2 \[Pi]}, PlotStyle -> b]}] ? – Alexei Boulbitch May 10 '16 at 13:15

Graphics objects from Plot has a Directive, which act as the PlotStyle. Changing that would give you the desired result.

For instance:

Show[{fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10}]


Show[{fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10}/. Directive[__] -> Red]


If you want different colors for each function, use functions like Hue (as shown here) or ColorData, etc.

Show[Table[
ReplaceAll[#[[n]], Directive[__] -> Hue[(n-1)/(Length[#] - 1)]], {n,
Length[#]}] &@{fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9,
fn10}]


You add more options if you use Directive. e.g. Thick:

Show[Table[
ReplaceAll[#[[n]], Directive[__] ->
Directive[Thick, Hue[(n - 1)/(Length[#] - 1)]]], {n,
Length[#]}] &@{fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9,
fn10}]


• I would not recommend using a Part specification to extract the elements unless you had no other choice since they are not guaranteed to stay the same from release to release. But, there are plenty of other choices, like Jason's answer, above. At worst, you could flatten the structure using parser@Normal@plot with this parser. – rcollyer May 10 '16 at 14:04
• @rcollyer silly me, I didn't think of that. Changed my answer to use ReplaceAll and not ReplacePart. This will work, wouldn't it? – JungHwan Min May 10 '16 at 14:10
• You could use Directive[__], but then you're changing other aspects of the style that you might not want to change. I think I'd use _?ColorQ -> Red (provided you have v10) as it is more specific for this case. (+1 for being a reasonable idea, now) Other notes: check out MapIndexed, it will work better than Table[...]& @ {...}. – rcollyer May 10 '16 at 14:19
• @rcollyer GridLinesStyle uses Directive as well, so ReplaceAll will change that. However, _?ColorQ -> Red can also change it... In my Table[], I needed the length of the input list, so I decided not to use MapIndexed. Nothing is more confusing than two layers of pure functions! – JungHwan Min May 10 '16 at 14:32
• Right, if you want to change GridLinesStyle, then definitely replace Directive. I like ColorQ because it is nice and focused, but sometimes it is to focused. As far as layers of pure functions, I've done worse then two layers. I used to be a fan of chaining pure functions together through postfix (//), 10 or even 20 functions wasn't abnormal, and a complete nightmare to debug a week later when I needed to change something. :) – rcollyer May 10 '16 at 14:54

You have a list of plots that you have defined already, all using the default color,

(Evaluate[Symbol["plt" <> IntegerString[#]]] =
Plot[# x^2, {x, -3, 3}]) & /@ Range[7];
Show[{plt1, plt2, plt3, plt4, plt5, plt6, plt7}]


Boy that doesn't look right, so let's see if we can change that. Changing the color on any one plot is easy,

plt1 /. {{a__, Line[b__]} :> {a, Red, Line[b]}}


(I'm pretty sure there's a way to write that pattern without letters, but I don't know it)

To apply this to a list of plots, one way is to use MapIndexed,

Show[
MapIndexed[#1 /. {{a__, Line[b__]} :> {a, ColorData[97, First@#2],
Line[b]}} &, {plt1, plt2, plt3, plt4, plt5, plt6, plt7}]
]


You could do other things, like make every other line dashed,

Show[
MapIndexed[#1 /. {{a__, Line[b__]} :> {a, ColorData[97, First@#2],
Dashing[If[EvenQ[First@#2], .01, {}]], Line[b]}} &, {plt1,
plt2, plt3, plt4, plt5, plt6, plt7}]
]


• OK ... JasonB we have to talk about speed! +1 – user9660 May 10 '16 at 13:17
• The key is that I have a neural implant designed to go off when a question with one of my favorite tags is posted. :-) – Jason B. May 10 '16 at 13:19
• Hey Jason, could you please explain the code? I don't really understand how MapIndexed works here. Thanks! – Mikayel Apr 6 '18 at 7:36
• MapIndexed[#1 /. {{a__, Point[b__]} :> {a, ColorData[108, First@#2], Point[b]}} &, Graphics[{Point[Table[{ii, 0}, {ii, 10}]]}]] Why this does not work? – Mikayel Apr 6 '18 at 12:21
• @Mikayel - I'm using MapIndexed here to directly modify the Graphics expression. To do that you have to know what your Graphics looks like in order to do pattern replacements. In the code you pasted, there isn't anything to match the {a__, Point[b__]} pattern. – Jason B. Apr 6 '18 at 14:58