5
$\begingroup$

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?

Enter image description here

$\endgroup$
  • $\begingroup$ Why couldn't you set PlotStyle in each of the pltAbs objects? $\endgroup$ – J. M. will be back soon May 10 '16 at 12:50
  • $\begingroup$ 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]}] ? $\endgroup$ – Alexei Boulbitch May 10 '16 at 13:15
6
$\begingroup$

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

$\endgroup$
  • 2
    $\begingroup$ 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. $\endgroup$ – rcollyer May 10 '16 at 14:04
  • $\begingroup$ @rcollyer silly me, I didn't think of that. Changed my answer to use ReplaceAll and not ReplacePart. This will work, wouldn't it? $\endgroup$ – JungHwan Min May 10 '16 at 14:10
  • 1
    $\begingroup$ 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[...]& @ {...}. $\endgroup$ – rcollyer May 10 '16 at 14:19
  • $\begingroup$ @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! $\endgroup$ – JungHwan Min May 10 '16 at 14:32
  • $\begingroup$ 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. :) $\endgroup$ – rcollyer May 10 '16 at 14:54
12
$\begingroup$

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

$\endgroup$
  • $\begingroup$ OK ... JasonB we have to talk about speed! +1 $\endgroup$ – user9660 May 10 '16 at 13:17
  • 4
    $\begingroup$ The key is that I have a neural implant designed to go off when a question with one of my favorite tags is posted. :-) $\endgroup$ – Jason B. May 10 '16 at 13:19
  • $\begingroup$ Hey Jason, could you please explain the code? I don't really understand how MapIndexed works here. Thanks! $\endgroup$ – Mikayel Apr 6 '18 at 7:36
  • $\begingroup$ MapIndexed[#1 /. {{a__, Point[b__]} :> {a, ColorData[108, First@#2], Point[b]}} &, Graphics[{Point[Table[{ii, 0}, {ii, 10}]]}]] Why this does not work? $\endgroup$ – Mikayel Apr 6 '18 at 12:21
  • $\begingroup$ @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. $\endgroup$ – Jason B. Apr 6 '18 at 14:58

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.