Search code examples
plotvisualizationwolfram-mathematica

Combining plots with distinct legends


First, sorry because I am very new to Mathematica, English and Stackoverflow. So, I have the following code:

k := (di^3 *dj*wi^2 + 2*di^2*(2 - dj)*wi*wj + 
     di*(2 - dj)*wj^2) / (4*di^2*wi + 4*(2 - dj) wj);
wi := 10;
wj := 9;

Table[Plot[
  Evaluate[f[b] /. 
    NDSolve[{f'[b] == 
       2*f[b]/(di*f[b] + 
           di^2*((2*b - (2 - dj)*f[b])/(di*
                 dj) + ((2*b - (2 - dj)*f[b])^2/(di*dj)^2 + (2 - 
                    dj)*(4*b - di*f[b])*f[b]/(di^3*dj))^(1/2) - 2*b)),
       f[0] == 10^-5}, f, {b, 0, k}]], {b, 0, k}, PlotRange -> All, 
  PlotLegends -> 
   StringTemplate["di = `a`, dj = `b`"][<|"a" -> di, "b" -> dj|>]],
 {dj, 0.5, 0.7, 0.1}, {di, 0.1, 0.3, 0.1}];

and I want to combine the curves like that, all in one graphics

k := (di^3 *dj*wi^2 + 2*di^2*(2 - dj)*wi*wj + 
     di*(2 - dj)*wj^2) / (4*di^2*wi + 4*(2 - dj) wj);
wi := 10;
wj := 9;

Show[Table[
  Plot[Evaluate[
    f[b] /. NDSolve[{f'[b] == 
        2*f[b]/(di*f[b] + 
            di^2*((2*b - (2 - dj)*f[b])/(di*
                  dj) + ((2*b - (2 - dj)*f[b])^2/(di*dj)^2 + (2 - 
                    dj)*(4*b - di*f[b])*f[b]/(di^3*dj))^(1/2) - 2*b)),
        f[0] == 10^-5}, f, {b, 0, k}]], {b, 0, k}, PlotRange -> All],
  {dj, 0.5, 0.9, 0.1}, {di, 0.1, 0.4, 0.1}]]

Can someone help me to put the legends like the graphics below?

RAveList = RandomReal[1, {11, 5}];
colors = ColorData[97];
labels = Row[{#, "-clusters"}] & /@ Range[2, 12];
ListPlot[Thread[Tooltip[RAveList, labels]], Joined -> True, 
 DataRange -> {0, 5}, PlotStyle -> colors, PlotLegends -> labels, 
 Axes -> None, Frame -> True]

Solution

  • You have 20 plots

    range = Catenate[Table[{dj, di}, {dj, 0.5, 0.9, 0.1}, {di, 0.1, 0.4, 0.1}]];
    Length[range]
    

    20

    Find a range of 20 colours in ColorData

    Column[Select[
      {#, Length[cols = ColorData[#, "ColorList"]], cols} & /@
       ColorData["Indexed"], #[[2]] >= 20 &]]
    

    ColorData[54] will do.

    colours = ColorData[54, #] & /@ Range[20];
    

    Assign 20 colours to f for the various {dj, di}

    MapThread[(f[#1] = #2) &, {range, colours}];
    

    Add PlotStyle and LineLegend showing {dj, di}

    GraphicsRow[{Show[Catenate[Table[Plot[Evaluate[f[b] /. NDSolve[
             {f'[b] == 2*f[b]/
                 (di*f[b] + di^2*((2*b - (2 - dj)*f[b])/(di*dj) +
                      ((2*b - (2 - dj)*f[b])^2/(di*dj)^2 +
                        (2 - dj)*(4*b - di*f[b])*f[b]/
                        (di^3*dj))^(1/2) - 2*b)),
              f[0] == 10^-5}, f, {b, 0, k}]], {b, 0, k},
          PlotRange -> All, PlotStyle -> f[{dj, di}]],
         {dj, 0.5, 0.9, 0.1}, {di, 0.1, 0.4, 0.1}]]],
      LineLegend[colours, range]}]
    

    enter image description here

    For a more visually ordered legend, listing according to the order as plotted.

    ordered = Catenate[Reverse /@ Transpose[
         Table[{dj, di}, {dj, 0.5, 0.9, 0.1}, {di, 0.1, 0.4, 0.1}]]];
    
    GraphicsRow[{Show[...],
      LineLegend[f /@ ordered, ordered]}]
    

    enter image description here