4

Supposing a long time ago I made a plot and now I wish to modify the plot without regenerating the data. I have the plot in a notebook and wish to get it into a new form. What options can I change? I have found a few but I need more. I guess I could get the data from the plot using this method and start afresh but this can be complex and I would rather avoid this approach. Here is a minimum working example. Let's suppose this is the old plot I made.

oldPlot = 
 Plot[Evaluate[Table[1/n Sin[2 π n t], {n, 4}]], {t, 0, 2}, 
  Frame -> True, FrameLabel -> {"Time", "Force"}, AspectRatio -> 1/3, 
  ImageSize -> 12 72, 
  PlotLegends -> 
   LineLegend[{"Force 1", "Force 2", "Force 3", "Force 4"}]]

Mathematica graphics

I now enter the following text into the notebook

newPlot =  ;

and then cut and paste the old plot (including the legend) before the ";" and press Shift-Enter. This gives me

Mathematica graphics

Now I can start playing and making changes, for example,

fig1b = Show[newPlot,
  ImageSize -> 8 72,
  AspectRatio -> 1/4,
  FrameLabel -> {"Time / s", "Force / N"},
  LabelStyle -> {FontFamily -> "Times", FontSize -> 12},
  FrameTicksStyle -> Thick]

Mathematica graphics

There are probable many more options I can change (not sure how to find them all it would be nice to know) but I can't seem to change the legend. The LabelStyle does nothing to the legend although it has nicely changed the FrameLabel. I have noticed that if I do

fig1b[[2]]

Mathematica graphics

I get something that seems detached from the main plot but I don't know how to change it. I also note that I can't abandon the old legend and put a new one into Show. It could be useful to do this. The main question: how do I change the legend with for example a new font and font size?

Thanks

kglr
  • 394,356
  • 18
  • 477
  • 896
Hugh
  • 16,387
  • 3
  • 31
  • 83
  • 1
    As a general reminder: only Graphics[] (or Graphics3D[] in the 3D case) options can be changed after the fact. So, e.g., you will not be able to change the setting of PlotPoints without regenerating your plot altogether. – J. M.'s missing motivation Mar 14 '18 at 14:02
  • A useful observation. Fortunately in my case it is measured data so I can't make any more points. Thanks. – Hugh Mar 14 '18 at 14:04

1 Answers1

3
Show[newplot /.  LineLegend[a_, b_, c___] :> 
   LineLegend[a, b, LabelStyle -> {FontFamily -> "Times", FontSize -> 12}, c], 
 ImageSize -> 8 72, AspectRatio -> 1/4, 
 FrameLabel -> {"Time / s", "Force / N"}, 
 LabelStyle -> {FontFamily -> "Times", FontSize -> 12}, 
 FrameTicksStyle -> Thick]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Thanks for the fast response. This works nicely. Can we even adjust the vertical position of the legend since this has slipped down a bit following the new aspect ratio? – Hugh Mar 14 '18 at 12:40
  • @Hugh, maybe newplot /. Placed[LineLegend[a_, b___], _] :> Placed[LineLegend[a, b, "Spacings" -> .5, LabelStyle -> {FontFamily -> "Times", FontSize -> 12}], {After, Center}]? – kglr Mar 14 '18 at 12:57
  • I think you want LineLegend[a_, b_,c___] :> LineLegend[a, b, LabelStyle -> {FontFamily -> "Times", FontSize -> 12}] to get rid of previous options. With just LineLegend[a_, b___] the old options are retained and the new alternatives do not work. – Hugh Mar 15 '18 at 16:02
  • @Hugh good catch; thank you. – kglr Mar 15 '18 at 19:54