0

I'm plotting a data set as points and using a best-fitting line. Hence, I wanted to depict the points as points and the line as a line for accessibility and aesthetic purposes. Also, I know about the "Placed" command, but I haven't been able to employ it in order to put a legend inside a plot. I'm not used to the Wolfram language and I need to do this for class, so any help would be appreciated.

Here's my code:

data = ( {
    {0.049, 0.325},
    {0.188, 0.484},
    {0.329, 0.635},
    {0.470, 0.795},
    {0.576, 0.945},
    {0.766, 1.15},
    {0.907, 1.264},
    {1.056, 1.412},
    {1.196, 1.566},
    {1.336, 1.714},
    {1.500, 1.889}
   } );
p1 = ListPlot[data,
   PlotStyle ->  {Black, Thickness[0.07]}];

model = LinearModelFit[data, x, x];
model["BestFit"];
p2 = Plot[model["BestFit"], {x, 0, 1.5},
   PlotStyle -> {Pink, Thickness[0.003]}];

Legended[
 Show[p2, p1,
  PlotLabel -> "Deformation of spring by applied force",
  Frame -> True,
  FrameLabel -> {"Force (N)", "Lenght of the spring (m)"},
  LabelStyle -> Directive[Black, Bold, 15],
  GridLines -> Automatic,
   PlotRange -> All],
 PointLegend [{Black, Pink}, {"Experimental data", 
   "Linear regression"}]
 ]

This is the graph and legend

  • `Show[ListPlot[Legended[data,"Experimental data"]],Plot[Legended[model["BestFit"], "Linear Regression"],{x,0,1.5}]]` To position legends study https://mathematica.stackexchange.com/search?q=position+legend – Bill Jun 14 '23 at 18:41

0 Answers0