4
$\begingroup$

I have a plot in LogLinear scale I wanted to put the legend in the top of the plot horizontally. Can I place this manually, It is extremely difficult to place it through code. this is my customized legend. The full code is too big and takes more than forty minutes to produce the plot. the final plot . On top of this line inside the plot, I am planning to place the legend

p[5] = Graphics[{Thickness[0.008], AbsoluteDashing[{2, 7}], 
   Line[{{Log[0.001], 13.5}, {Log[0.008] , 13.5}}], 
   Text[StyleForm["Mode 1,", FontSize -> 30], {Log[0.08], 13.5}]}
$\endgroup$
4
  • $\begingroup$ does something like DynamicModule[{pt = Scaled[{.5, 1}]}, Plot[x Sin[x], {x, -10, 10}, ImageMargins -> 10, ImagePadding -> Scaled[.05], PlotRangeClipping -> False, PlotStyle -> Directive[Dashed, Red], Epilog -> Dynamic[Locator[Dynamic[pt], Panel[LineLegend[{Directive[Dashed, Red]}, {"legend"}]], Background -> White, LocatorRegion -> Full]]]] work? $\endgroup$
    – kglr
    Commented Sep 28, 2019 at 7:14
  • $\begingroup$ DynamicModule[{pt = Scaled[{.5, 1}]}, Plot[{x Sin[x], x Cos[x], x Tan[x]}, {x, -10, 10}, ImageMargins -> 10, ImagePadding -> Scaled[.05], PlotRangeClipping -> False, PlotStyle -> {Directive[Dashed, Red], Directive[Dashed, Blue], Directive[Dashed, Black]}, Epilog -> Dynamic[Locator[Dynamic[pt], Panel[LineLegend[{Directive[Dashed, Red]}, {"legend"}]], Background -> White, LocatorRegion -> Full]]]] $\endgroup$
    – acoustics
    Commented Sep 28, 2019 at 7:24
  • $\begingroup$ How to add legend if there are multiple lines in the plot like above $\endgroup$
    – acoustics
    Commented Sep 28, 2019 at 7:25
  • $\begingroup$ acoustics, posted an answer for the example in your comment. $\endgroup$
    – kglr
    Commented Sep 28, 2019 at 7:40

1 Answer 1

4
$\begingroup$
ClearAll[moveablelegend ]
moveablelegend = DynamicModule[{pt = Scaled[{.5, 1}]}, Show[#, Epilog -> 
  Dynamic[Locator[Dynamic[pt], #2, Background -> White, LocatorRegion -> Full]], ##3]] &;

Example:

plt = Plot[{x Sin[x], x Cos[x], x Tan[x]}, {x, -10, 10}, 
  ImageMargins -> 10, ImagePadding -> Scaled[.05], PlotRangeClipping -> False, 
  PlotStyle -> {Directive[Dashed, Red], Directive[Dashed, Blue], Directive[Dashed, Black]},
  PlotLegends -> LineLegend["Expressions", LabelStyle -> 18,  LegendLayout -> {"Row", 1}],
  ImageSize -> Large];

enter image description here

{plot, legend} = {plt[[1]], plt[[2, 1, 1]]};
moveablelegend[plot, legend, ImageSize -> Large]

enter image description here

moveablelegend[plot, Framed[Rotate[legend, Pi/2], FrameMargins -> 0], ImageSize -> Large]

enter image description here

$\endgroup$
2
  • $\begingroup$ Instead of expression can we say like function 1, function 2, function 3, because I did not get how to input theses, instead of expression $\endgroup$
    – acoustics
    Commented Sep 28, 2019 at 7:48
  • 1
    $\begingroup$ @acoustics, you can replace "Expressions" with {"function 1", "function 2", "function 3"} , that is use LineLegend[{"function 1", "function 2", "function 3"}, LabelStyle -> 18, LegendLayout -> {"Row", 1}]. $\endgroup$
    – kglr
    Commented Sep 28, 2019 at 7:53

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.