4
$\begingroup$

Problem: I have a plot with legend stored in a variable.

datatry=Table[{RandomReal[{0, 10}], RandomReal[{0, 10}]}, {i, 10}, {j, 3}];
plot = ListPlot[datatry, PlotLegends -> LineLegend[Table[i, {i, 5}]]]

This plot already has a legend as you can see below: enter image description here

Now I want to change the variable 'plot' such that a label is added to the legend. I found a way to do this by replacing the entire linelegend part with a new linelegend and added plotlabel.

plot /. {LineLegend[
    List[Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.368417`, 0.506779`, 0.709798`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.880722`, 0.611041`, 0.142051`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.560181`, 0.691569`, 0.194885`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.922526`, 0.385626`, 0.209179`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.528488`, 0.470624`, 0.701351`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.772079`, 0.431554`, 0.102387`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.363898`, 0.618501`, 0.782349`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[1, 0.75`, 0], AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.647624`, 0.37816`, 0.614037`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.571589`, 0.586483`, 0.`], AbsoluteThickness[1.6`]]], 
    List[1, 2, 3, 4, 5], 
    Rule[LegendMarkers, 
     List[List[False, Automatic], List[False, Automatic], 
      List[False, Automatic], List[False, Automatic], 
      List[False, Automatic], List[False, Automatic], 
      List[False, Automatic], List[False, Automatic], 
      List[False, Automatic], List[False, Automatic]]], 
    Rule[Joined, 
     List[False, False, False, False, False, False, False, False, 
      False, False]], Rule[LabelStyle, List[]], 
    Rule[LegendLayout, "Column"]] -> 
   LineLegend[
    List[Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.368417`, 0.506779`, 0.709798`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.880722`, 0.611041`, 0.142051`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.560181`, 0.691569`, 0.194885`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.922526`, 0.385626`, 0.209179`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.528488`, 0.470624`, 0.701351`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.772079`, 0.431554`, 0.102387`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.363898`, 0.618501`, 0.782349`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[1, 0.75`, 0], AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.647624`, 0.37816`, 0.614037`], 
      AbsoluteThickness[1.6`]], 
     Directive[PointSize[0.012833333333333334`], 
      RGBColor[0.571589`, 0.586483`, 0.`], AbsoluteThickness[1.6`]]], 
    List[1, 2, 3, 4, 5], 
    Rule[LegendMarkers, 
     List[List[False, Automatic], List[False, Automatic], 
      List[False, Automatic], List[False, Automatic], 
      List[False, Automatic], List[False, Automatic], 
      List[False, Automatic], List[False, Automatic], 
      List[False, Automatic], List[False, Automatic]]], 
    Rule[Joined, 
     List[False, False, False, False, False, False, False, False, 
      False, False]], Rule[LabelStyle, List[]], 
    Rule[LegendLayout, "Column"], LegendLabel -> "Label Legend"]}

Which gives: enter image description here

Is there a simpler, easier way to do this?

$\endgroup$
4
  • 1
    $\begingroup$ LineLegend has an option for LegendLabel. PlotLegends -> LineLegend[Table[i, {i, 5}], LegendLabel -> "Label Legend" ] $\endgroup$
    – Lotus
    Commented Apr 9, 2018 at 10:08
  • 2
    $\begingroup$ plot /. LineLegend[a__] :> LineLegend[a, LegendLabel -> "Label Legend"]? $\endgroup$
    – kglr
    Commented Apr 9, 2018 at 10:10
  • $\begingroup$ hm, you are using some functions here I didn't know yet $\endgroup$ Commented Apr 9, 2018 at 10:11
  • $\begingroup$ but it works. thanks! $\endgroup$ Commented Apr 9, 2018 at 10:12

1 Answer 1

4
$\begingroup$
plot /. LineLegend[a__] :> LineLegend[a, LegendLabel -> "Label Legend"]

enter image description here

$\endgroup$
1
  • $\begingroup$ I have a graph with an existing PlotLabel and wanted to use this graph somewhere else with a new PlotLabel. How can I overwrite the existing label with a new label? $\endgroup$ Commented Sep 6, 2020 at 14:24

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.