2
$\begingroup$

I have been trying to combine two plots; each plot is similar to this.

For the first plot -

fdata1 = {{2, 100}, {6, 90}, {10, 80}, {14, 65}, {18, 40}};
gdata1 = {{2, 0.1}, {6, 0.3}, {10, 0.37}, {14, 0.26}, {18, 0.05}};
g1 = Interpolation[gdata1];

gmin1 = MinValue[{g1[x], 2 <= x <= 18}, x]
gmax1 = MaxValue[{g1[x], 2 <= x <= 18}, x]

p1 = Legended[
  ListPlot[fdata1, Joined -> True, Frame -> True, 
   FrameLabel -> (Style[#, 14] & /@ {"x", "f(x)"}), 
   ColorFunction -> Function[{x, y}, ColorData["Rainbow"][Rescale[g1[x], {gmin1, gmax1}]]], 
   ColorFunctionScaling -> False
  ], 
  BarLegend[{"Rainbow", {gmin1, gmax1}}, LegendLabel -> HoldForm[g1[x]]]
]

For the second plot:

fdata2 = {{2, 50}, {6, 75}, {10, 81.3}, {14, 85}, {18, 90}};
gdata2 = {{2, 0.7}, {6, 0.8}, {10, 0.97}, {14, 1.02}, {18, 1.5}};
g2 = Interpolation[gdata2];

gmin2 = MinValue[{g2[x], 2 <= x <= 18}, x]
gmax2 = MaxValue[{g2[x], 2 <= x <= 18}, x]

p2 = Legended[
  ListPlot[fdata2, Joined -> True, Frame -> True, 
   FrameLabel -> (Style[#, 14] & /@ {"x", "f(x)"}), 
   ColorFunction -> Function[{x, y}, ColorData["Rainbow"][Rescale[g2[x], {gmin2, gmax2}]]], 
   ColorFunctionScaling -> False
  ], 
  BarLegend[{"Rainbow", {gmin2, gmax2}}, LegendLabel -> HoldForm[g2[x]]]
]

Then I combined them with Show:

Show[p1, p2]

It results in two BarLegends, one above the other. Instead, I would like them placed side by side. How can I achieve that?

enter image description here

$\endgroup$
4
  • $\begingroup$ For your code in my machine (13.1.0 for Mac OS X x86 (64-bit)) they are placed side by side. $\endgroup$
    – demm
    Sep 14, 2022 at 16:44
  • $\begingroup$ On MMA 12.1.3 Win10-64 they show up side by side already: i.stack.imgur.com/BznMz.png. $\endgroup$
    – MarcoB
    Sep 14, 2022 at 16:45
  • 1
    $\begingroup$ Same side by side output on v12.2.0, Win7-x64. Please add a picture of what you see on your screen and the output of $Version. $\endgroup$
    – Syed
    Sep 14, 2022 at 16:55
  • $\begingroup$ I am getting this - i.imgur.com/KWxGKOo.jpeg $\endgroup$
    – PoreyS
    Sep 14, 2022 at 17:27

2 Answers 2

5
$\begingroup$

What version are you using? With v13.1, the legends are side-by-side.

$Version

"13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)"

Clear["Global`*"]

fdata1 = {{2, 100}, {6, 90}, {10, 80}, {14, 65}, {18, 40}};
gdata1 = {{2, 0.1}, {6, 0.3}, {10, 0.37}, {14, 0.26}, {18, 0.05}};

fdata2 = {{2, 50}, {6, 75}, {10, 81.3}, {14, 85}, {18, 90}};
gdata2 = {{2, 0.7}, {6, 0.8}, {10, 0.97}, {14, 1.02}, {18, 1.5}};

Plotting,

Show[
 MapIndexed[
  Module[{xmin, xmax, g, gmin, gmax},
    {xmin, xmax} = MinMax[#1[[1, All, 1]]];
    g = Interpolation[#1[[2]]];
    gmin = MinValue[{g[x], xmin <= x <= xmax}, x];
    gmax = MaxValue[{g[x], xmin <= x <= xmax}, x];
    Legended[
     ListPlot[#1[[1]],
      PlotLabels -> Placed[
        StringForm["f``(x)", First[#2]],
        Above],
      PlotStyle -> Thick,
      Joined -> True,
      Frame -> True,
      FrameLabel -> (Style[#, 14] & /@ {"x", HoldForm[f[x]]}),
      ColorFunction -> Function[{x, y},
        ColorData["Rainbow"]
         [Rescale[g[x], {gmin, gmax}]]],
      ColorFunctionScaling -> False],
     BarLegend[{"Rainbow", {gmin, gmax}},
      LegendLabel -> StringForm["g``(x)", First[#2]]]]] &,
  {{fdata1, gdata1}, {fdata2, gdata2}}]]

enter image description here

EDIT: For version 12.0

$Version

(* "12.0.0 for Mac OS X x86 (64-bit) (April 7, 2019)" *)

Clear["Global`*"]

fdata1 = {{2, 100}, {6, 90}, {10, 80}, {14, 65}, {18, 40}};
gdata1 = {{2, 0.1}, {6, 0.3}, {10, 0.37}, {14, 0.26}, {18, 0.05}};

fdata2 = {{2, 50}, {6, 75}, {10, 81.3}, {14, 85}, {18, 90}};
gdata2 = {{2, 0.7}, {6, 0.8}, {10, 0.97}, {14, 1.02}, {18, 1.5}};

Calculate the plots and legends

{plts, legends} = 
  Transpose@MapIndexed[
    Module[{xmin, xmax, g, gmin, gmax}, 
     {xmin, xmax} = MinMax[#1[[1, All, 1]]];
      g = Interpolation[#1[[2]]];
      gmin = MinValue[{g[x], xmin <= x <= xmax}, x];
      gmax = MaxValue[{g[x], xmin <= x <= xmax}, x];
      {ListPlot[#1[[1]], 
        PlotLabels -> 
          Placed[StringForm["f``(x)", First[#2]], Above], 
        PlotStyle -> Thick, 
        Joined -> True, 
        Frame -> True, 
        FrameLabel -> (Style[#, 14] & /@ {"x", HoldForm[f[x]]}), 
        ColorFunction -> 
         Function[{x, y}, ColorData["Rainbow"][
          Rescale[g[x], {gmin, gmax}]]], 
        ColorFunctionScaling -> False],
       BarLegend[{"Rainbow", {gmin, gmax}}, 
        LegendLabel -> StringForm["g``(x)", First[#2]]]}] &, 
    {{fdata1, gdata1}, {fdata2, gdata2}}];

Display in a Row

Row[Flatten[{Show[plts, ImageSize -> Medium], legends}, 1]]

enter image description here

$\endgroup$
2
  • $\begingroup$ I have been using v=12.0.0. Probably, the problem is with this older version. $\endgroup$
    – PoreyS
    Sep 14, 2022 at 19:32
  • $\begingroup$ Thank you kindly. It works for me. $\endgroup$
    – PoreyS
    Sep 15, 2022 at 2:55
0
$\begingroup$

What about this:

Legended[ListPlot[Join[fdata1, fdata2], Joined -> True, Frame -> True,
   FrameLabel -> (Style[#, 14] & /@ {"x", "f(x)"}), 
  ColorFunction -> 
   Function[{x, y}, 
    ColorData["Rainbow"][Rescale[g1[x], {gmin1, gmax1}]]], 
  ColorFunctionScaling -> False], 
 BarLegend[{"Rainbow", {Min[gmin1, gmin2], Max[gmax1, gmax2]}}, 
  LegendLabel -> HoldForm[g[x]]]]

enter image description here

??

$\endgroup$
1
  • 1
    $\begingroup$ Sorry. For my case, I can not join fdata1 and fdata2. $\endgroup$
    – PoreyS
    Sep 14, 2022 at 17:28

Your Answer

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

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