2
$\begingroup$

I don't understand how to modify the ColorData in the mapIndexed for the code from another post found here and is copied below for reference. What I want to do is control the color of each trace as well as its style (e.g., bold, dashed).

My main problem is I do not understand what "[#2[[1]]]" is doing. I understand what a slot is, but apparently not in this example. Slot one references both datasets, slot 2 gives the first two colors in the ColorData pallet chosen, and slot 3 does not exist.

If I try and force it using PlotStyle -> {{Black,Thick},{Blue,Thick,Dashed}} in the MapIndexed function it just takes the first entry for both plots. It also won't match the axes colors for reader reference.

I also tried removing PlotStyle -> {{Black,Thick},{Blue,Thick,Dashed}} and putting it in the options of the function calling the module the same error occurs.

Any help would be most appreciated!

 Module[{fgraph, ggraph, frange, grange, fticks, 
   gticks}, {fgraph, ggraph} = 
   MapIndexed[
Plot[#, {x, x1, x2}, Axes -> True, 
  PlotStyle -> ColorData[1][#2[[1]]]] &, {f, g}]; {frange, 
grange} = (PlotRange /. AbsoluteOptions[#, PlotRange])[[
  2]] & /@ {fgraph, ggraph}; fticks = N@FindDivisions[frange, 5]; 
 gticks = Quiet@
Transpose@{fticks, 
  ToString[NumberForm[#, 2], StandardForm] & /@ 
   Rescale[fticks, frange, grange]}; 
Show[fgraph, 
 ggraph /. 
Graphics[graph_, s___] :> 
 Graphics[
  GeometricTransformation[graph, 
   RescalingTransform[{{0, 1}, grange}, {{0, 1}, frange}]], s], 
   Axes -> False, Frame -> True, 
   FrameStyle -> {ColorData[1] /@ {1, 2}, {Automatic, Automatic}}, 
    FrameTicks -> {{fticks, gticks}, {Automatic, Automatic}}]]```
$\endgroup$
3
  • 2
    $\begingroup$ If you are on V12+ take a look at the ResourceFunction CombinePlots. $\endgroup$ Commented Dec 22, 2020 at 23:53
  • $\begingroup$ @RohitNamjoshi that was super easy! I spent around 2.5 hours trying to figure the other code out. I still wonder what I was missing in the old code $\endgroup$
    – ProfessorL
    Commented Dec 23, 2020 at 0:56
  • $\begingroup$ From the previous code, this is all I had to do with my two plots formatted as I liked (p111 and p222): ResourceFunction["CombinePlots"][p111, p222, "AxesSides" -> "TwoY"] $\endgroup$
    – ProfessorL
    Commented Dec 24, 2020 at 4:11

1 Answer 1

1
$\begingroup$

I think you would prefer to use Lukas Lang's CombinePlots if you tried.

In case you need to play with variations on TwoAxisPlot, you can add an optional argument for styles and options as follows:

ClearAll[TwoAxisPlot2]
TwoAxisPlot2[{f_, g_}, {x_, x1_, x2_}, styles : {_, _} : (ColorData[97] /@ {1, 2}), 
    o : OptionsPattern[]] := Module[{fgraph, ggraph, frange, grange, fticks,  gticks}, 
 {fgraph, ggraph} = MapThread[Plot[#, {x, x1, x2}, Axes -> True, PlotStyle -> #2] &, 
     {{f, g}, styles}]; 
 {frange, grange} = (PlotRange /. AbsoluteOptions[#, PlotRange])[[2]] & /@ 
     {fgraph, ggraph}; 
 fticks = N @ FindDivisions[frange, 5];
 gticks = Quiet @ Transpose@{fticks, ToString[NumberForm[#, 2], StandardForm] & /@ 
       Rescale[fticks, frange, grange]};
 Show[fgraph, ggraph /. Graphics[graph_, s___] :> 
     Graphics[GeometricTransformation[graph, 
       RescalingTransform[{{0, 1}, grange}, {{0, 1}, frange}]], s], 
   Frame -> True, FrameStyle -> {styles, {Automatic, Automatic}}, 
   FrameTicks -> {{fticks, gticks}, {Automatic, Automatic}}, o, 
   Axes -> False, ImageSize -> Medium]]

Examples:

Row[{TwoAxisPlot2[{Sin[x], 10 x Cos[x]}, {x, 0, 2 Pi}, 
      ImageSize -> 400, LabelStyle -> 16], 
  TwoAxisPlot2[{Sin[x], 10 x Cos[x]}, {x, 0, 2 Pi}, {Red, Green}, 
      ImageSize -> 400, LabelStyle -> 16]}, Spacer[10]]

enter image description here

$\endgroup$
4
  • $\begingroup$ thanks. That seems to work well. However, I am running V12.2 and the Combine Plots works well and is much easier to implement. I would still like to know what I was missing per my initial post. Thanks for taking the time and Happy COVID Holidays! $\endgroup$
    – ProfessorL
    Commented Dec 24, 2020 at 4:10
  • $\begingroup$ @ProfessorL, with plotstyles = {{Black, Thick}, {Blue, Thick, Dashed}} (which you can add in first argument of Module) you need to replace PlotStyle -> ColorData[1][#2[[1]]] with PlotStyle -> plotstyles[[#2[[1]]]] and (if you want to use these styles for the two vertical axes) use FrameStyle -> {plotstyles[[#]] & /@ {1, 2}, {Automatic, Automatic}}. $\endgroup$
    – kglr
    Commented Dec 24, 2020 at 4:45
  • $\begingroup$ That worked! I still wish I knew what the [[#2[[1]]]] is doing. I am calling the index #2 in plotstyles and then calling the first position in the list from that. I suppose #2 is calling either fgraph or ggraph. I am still struggling with why #2 calls these when it is inside the MapIndexed. $\endgroup$
    – ProfessorL
    Commented Dec 24, 2020 at 18:55
  • $\begingroup$ @ProfessorL, the examples MapIndexed[foo[#, #2] &, {a, b, c}] and MapIndexed[{bar[#], buz[#2]} &, {a, b, c}] and MapIndexed[{bar[#], {x, y, z, w}[[#2[[1]]]]} &, {a, b, c}] might help re how the slots # and #2 work in the first argument of MapIndexed. $\endgroup$
    – kglr
    Commented Dec 25, 2020 at 3:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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