5
$\begingroup$

How can I extract the legend from the following plot?

a line and scatter plot with a legend

fm[name_String, size_ : 8] := 
  ResourceFunction["PolygonMarker"][name, Offset[size], EdgeForm[]];

ListPlot[
 Table[Accumulate@RandomReal[1, 10] + i, {i, 6}], 
 PlotMarkers -> 
  fm /@ {"Triangle", "Y", "Diamond", "ThreePointedStar", 
    "FivePointedStarSlim", "TripleCross"},
 Joined -> True, 
 PlotStyle -> ColorData[52, "ColorList"], 
 PlotLegends -> 
  PointLegend[
   Automatic, LegendMarkerSize -> {50, 37}, 
   LegendLayout -> (Column[Row /@ #, Spacings -> -1] &)], 
 ImageSize -> 450]

This doesn't work. I want to obtain only the legend.

PointLegend[Automatic, LegendMarkerSize -> {50, 37}, 
  LegendLayout -> (Column[Row /@ #, 
      Spacings -> -1] &)] /@ (fm /@ {"Triangle", "Y", "Diamond", 
    "ThreePointedStar", "FivePointedStarSlim", "TripleCross"})
$\endgroup$

3 Answers 3

6
$\begingroup$

Without knowing the exact location of the legend, and after assigning your plot to variable plot:

First@ Cases[plot, _PointLegend, All]

extracted legend


In response to the comment requesting a generator for these legends independent of the plotting function:

ClearAll[legend]
Options[legend] = {Joined -> Automatic};
legend[n_Integer?Positive, size : (_?NumericQ) : 8, colorScheme : (_Integer?(1 <= # <= 114 &)) : 52, OptionsPattern[]] :=
 PointLegend[
   ColorData[colorScheme] /@ Range[n],
   Automatic,
   Joined -> OptionValue[Joined],
   LegendMarkers -> (ResourceFunction["PolygonMarker"][#, Offset[size], EdgeForm[]] & /@ ResourceFunction["PolygonMarker"][All]),
   LegendMarkerSize -> {50, 37}, 
   LegendLayout -> (Column[Row /@ #, Spacings -> -1] &)
 ]

These can be used as follows:

{legend[3, 5, 97], 
 legend[5, 8, Joined -> True], 
 legend[4, Joined -> True]}

three examples of generated legends

$\endgroup$
2
  • $\begingroup$ Assuming that you don't have that plot, how would you plot the legend? My initial purpose is to plot legend so I can use it for other things as well but this one is also great to know. $\endgroup$
    – hana
    Jun 27 at 18:14
  • 1
    $\begingroup$ @hana I've added a generator function. See the edit. $\endgroup$
    – MarcoB
    Jun 27 at 18:40
6
$\begingroup$
fm[name_String, size_ : 8] := 
  ResourceFunction["PolygonMarker"][name, Offset[size], EdgeForm[]];
plot = ListPlot[Table[Accumulate@RandomReal[1, 10] + i, {i, 6}], 
  PlotMarkers -> 
   fm /@ {"Triangle", "Y", "Diamond", "ThreePointedStar", 
     "FivePointedStarSlim", "TripleCross"}, Joined -> True, 
  PlotStyle -> ColorData[52, "ColorList"], 
  PlotLegends -> 
   PointLegend[Automatic, LegendMarkerSize -> {50, 37}, 
    LegendLayout -> (Column[Row /@ #, Spacings -> -1] &)], 
  ImageSize -> 450]
plot[[2, 1]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks, assuming that you don't have that plot, how would you plot the legend? $\endgroup$
    – hana
    Jun 27 at 14:01
1
$\begingroup$

A simpler way may be to set ImageSize -> 0

fm[name_String, size_ : 8] := 
  ResourceFunction["PolygonMarker"][name, Offset[size], EdgeForm[]];
ListPlot[Table[Accumulate@RandomReal[1, 10] + i, {i, 6}], 
 PlotMarkers -> 
  fm /@ {"Triangle", "Y", "Diamond", "ThreePointedStar", 
    "FivePointedStarSlim", "TripleCross"}, Joined -> True, 
 PlotStyle -> ColorData[52, "ColorList"], 
 PlotLegends -> 
  PointLegend[Automatic, LegendMarkerSize -> {50, 37}, 
   LegendLayout -> (Column[Row /@ #, Spacings -> -1] &)], 
 ImageSize -> 0]

enter image description here

$\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.