1
$\begingroup$

I am trying to plot two different functions into a single plot. Each function depends on R and r. I plot against r for different R's. So I end up with two different functions for all the different values of R. I use SwatchLegend to plot the legends of the different values of R. However, I also want to plot another legend that shows which dashing I am using for each function. Such that the final plot clearly shows which values I am using of R and that it is clear which dashing is of each function. My synthesized code is: First, I define the functions that I am using as

CYLINDER1[R_, r_] := 
  E^(-((2.789371726903078` BesselK[0, 0.010765343149384599` r])/
   BesselK[1, 0.010765343149384599` (2.125` + R)]));
SPHERE1[R_, r_] := 
  E^(-((3.002854381130314`*^8 E^(
    0.010765343149384599` (2.125` - r + R)) (2.125` + R))/(
   r (1.0765343149384598`*^8 + 10000000000/(2.125` + R)))));

Afterwards, I defined the values of R that I will vary, as well as the command that I will use to call the legends and placed them where I want. I also define some constants to create the dashing of my wish. And finally , I create a function to plot both functions.

RAD = {5, 15, 30, 80} ;
legrad[X_, Y_] := 
  Placed[SwatchLegend[{"5 \[Angstrom]", "15 \[Angstrom]", 
     "30 \[Angstrom]", "80 \[Angstrom]"}, 
    LabelStyle -> {FontSize -> 13}], {X, Y}];
l1 = 0.025;
l2 = 0.015;
d = 0.001;
s1 = l1/2;
s2 = l2/2;
C1g[X_, Y_, legX_, legY_] := 
  Plot[Evaluate@Table[ArcSinh[CYLINDER1[R, r]], {R, RAD}], {r, 0, X}, 
   PlotRange -> {-0.05 Y, Y}, PlotLegends -> legrad[legX, legY], 
   PlotStyle -> Dashing[{l1, s1, l1, s1, d, s1}]];
S1g[X_, Y_] := 
  Plot[Evaluate@Table[ArcSinh[SPHERE1[R, r]], {R, RAD}], {r, 0, X}, 
   PlotRange -> {-0.05 Y, Y}, 
   PlotStyle -> Dashing[{l2, s2, l2, s2, d, s2}]];

Finally I merge them with

CS1g[X_, Y_, legX_, legY_] := 
      Show[C1g[X, Y, legX, legY], S1g[X, Y], Frame -> True];

How and where do I add another legend to show the dashing and the functions (something like "--.-- Cylinder", "..-..- Sphere") Furthermore, since I am already asking, if I were to change the values of RAD={}. What can I do so I won't need to rewrite my labels?

Thanks in advance for the help.

$\endgroup$
2
$\begingroup$

You can use a LineLegend (with Dashing specs in the first argument) in one of the plots. Show combines legends from input plots intelligently.

Using a simpler example:

RAD = Range[3];
plot1 = Plot[Evaluate[Table[Sin[a x], {a, RAD}]], {x, 0, 2 Pi}, 
      PlotStyle -> Dashing[Tiny], 
      PlotLegends -> SwatchLegend[RAD, LegendLabel -> "RAD"]];

plot2 = Plot[Evaluate[Table[Cos[a x], {a, RAD}]], {x, 0, 2 Pi}, 
      PlotStyle -> Dashing[Medium], 
      PlotLegends ->  LineLegend[{Dashing[Tiny], Dashing[Medium]}, {"Sin", "Cos"}, 
          LegendLabel -> "Function"]];

Show[plot1, plot2]

enter image description here

You can also specify different positions for the two legends. For example, if we use

 PlotLegends -> Placed[SwatchLegend[RAD, LegendLabel -> "RAD", 
   LegendLayout -> "Row"], Top]

in plot1, Show[plot1, plot2] gives

enter image description here

Update: An alternative approach is to use a single hybrid legend with swatch and line elements, use it as the option setting for PlotLegends in one of the plots:

hybridlegend =  SwatchLegend[{## & @@ (ColorData[97] /@ Range[3]),
   Dashing[Tiny], Dashing[Medium]},
  {"rad 1", "rad 2", "rad 3", "Sin", "Cos"}, 
  LegendMarkerSize -> {Automatic, Automatic, Automatic , {30, 20}, {30, 10}}, 
  LegendMarkers -> {"Square", "Square", "Square", "Line",  "Line"}];

 Show[plot1, plot2]

enter image description here

Note: In version 9, you can use SwatchLegend[{Automatic, Automatic, Automatic, Dashing[Tiny], Dashing[Medium]}, ...] inside Plot and it picks the right colors for the swatch markers.

$\endgroup$

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.