Message Boards Message Boards

0
|
279 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Display a legend in a graphic generated with Show[]

Posted 4 days ago

Currently, I am using Mathematica 13.2. For this version an Add-on PlotLegends (function ShowLegend[]) still exists, but is marked as obsolete:

Needs["PlotLegends`"]
General::obspkg: PlotLegends` is now obsolete. The legacy version being loaded may conflict with
 current functionality. See the Compatibility Guide for updating information.

Now, my question: Is there any other functionality to generate a legend within a Show[] construct? Here is an example what I am thinking about (PlotLegends does not work):

Show[ListPlot[Table[{f, Sin[2 \[Pi] 0.1 f]}, {f, 0., 200., 1.}], 
  PlotStyle -> Black, PlotMarkers -> {"x", Offset[10]}, 
  PlotLegends -> Automatic], 
 Plot[Sin[2 \[Pi] 0.1 f], {f, 0., 200.}, PlotStyle -> Orange, 
  PlotLegends -> Automatic, PlotRange -> All]]

Thank you for your time.

POSTED BY: Knut Buettner
4 Replies

Thank you very much for providing this example Hans.

POSTED BY: Knut Buettner
Posted 1 day ago

Here is one alternative of adding a legend to Show:

show=Show[
  ListPlot[Table[{f,Sin[2 Pi 0.1 f]},{f,0.,200.,1.}],PlotStyle->Black,PlotMarkers->{"x",Offset[10]}],
  Plot[Sin[2 Pi 0.1 f],{f,0.,200.},PlotStyle->Orange,PlotRange->All]
];

lgnd=Framed@Column@{
  PointLegend[{Black},{"Data Points"},LegendMarkers->Text["x"]],
  LineLegend[{Orange},{"Fitted Function"}]
};

Legended[show, lgnd]

enter image description here

POSTED BY: Hans Milton

As it seems PlotLegends does not work (i.e. does not show) when all curves have the identical Style (here: Orange) or if there is only a single curve. (And this would not make much sense either, IMHO.) This e.g. does work:

Show[ListPlot[Table[{f, Sin[2 \[Pi] 0.1 f]}, {f, 0., 200., 1.}], 
  PlotStyle -> Black, PlotMarkers -> {"x", Offset[10]}, 
  PlotLegends -> Automatic], 
 Plot[{Sin[2 \[Pi] 0.1 f], Cos[2 \[Pi] 0.1 f]}, {f, 0., 200.}, 
  PlotStyle -> Automatic, PlotLegends -> "Expressions", 
  PlotRange -> All]]
POSTED BY: Henrik Schachner

Thank you very much for your comments! To make it more understandable what I am looking for is something like such a graph (using old style legend construction):

Needs["PlotLegends`"]

Block[{lgnd},
 lgnd = {{{Graphics[{Black, Text["x"]}], 
     Style["Data Points", 
      FontSize -> 12]}, {Graphics[{Orange, 
       Line[{{0, 15}, {30, 15}}]}], 
     Style["Fitted Function", FontSize -> 12]}}, 
   LegendPosition -> {0.85, -0.20}, LegendSize -> {0.78, 0.25}, 
   LegendTextSpace -> 6, LegendShadow -> {.02, -.02}};
 ShowLegend[
  Show[ListPlot[Table[{f, Sin[2 \[Pi] 0.1 f]}, {f, 0., 200., 1.}], 
    PlotStyle -> Black, PlotMarkers -> {"x", Offset[10]}], 
   Plot[Sin[2 \[Pi] 0.1 f], {f, 0., 200.}, PlotStyle -> Orange, 
    PlotRange -> All]], lgnd]
 ]
POSTED BY: Knut Buettner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract