MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Legends for Data Plots

  • To: mathgroup at smc.vnet.net
  • Subject: [mg129980] Re: Legends for Data Plots
  • From: Alexei Boulbitch <Alexei.Boulbitch at iee.lu>
  • Date: Thu, 28 Feb 2013 21:30:22 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net

Here is a routine that I go through a lot. Take some data set, do a fit
or two, then plot the data along with the fit(s). Simple enough to do.
Now, how can I do a Legend that incorporates both the data (usually
points) and the fits (usually lines)? I can use PlotLegends to get the
lines bit, but I usually put the points on the Plot with Epilog.

One thing I thought of was to generate a dense Table of the fitting
function(s) and then use ListLinePlot along with Legends; however, here
all works, except the Legends entry for the data is a line and not a
point even though I used something like Joined->{False,True,True} to put
points down for the data and lines for the fits.

Thanks,

Kevin


Hi, Kevin,

I also often do that and met the same problem. There is a way around it that I am aware of, not without drawbacks though.
You may combine by Show the ListPlot(s) with your data containing the PointLegend statement with the Plot(s) of your fitting functions along with the LineLegend statements. You will need only to play a bit with Placement to accurately arrange the legends, say, one over another.

To give an example, suppose this:

lst = Transpose[{RandomReal[{0, 3}, 30],
     RandomReal[{-0.03, 0.03}, 30]}] /. {x_, y_} -> {x, x*Exp[-x] + y};
ft = FindFit[lst, a + b*x*Exp[-c*x], {a, b, c}, x];

are the data and its fit.
The following displays the data and the fit function:

Show[{
  ListPlot[lst, PlotRange -> {{0, 3.5}, Automatic},
  
   (* The PointLegend starts *)
   PlotLegends ->
    Placed[PointLegend[{"    data"}, LabelStyle -> {Blue, 10}],
     Scaled[{0.69, 0.9}]]
   (* The PointLegend ends *)
   ],
 
  Plot[a + b*x*Exp[-c*x] /. ft, {x, 0, 3},
   PlotRange -> {{0, 3.5}, Automatic}, PlotStyle -> Red,
  
   (* The LineLegend starts *)
   PlotLegends ->
    Placed[LineLegend[{Row[{Style["f(x)=", Italic],
         Style[NumberForm[a + b*x*Exp[-c*x] /. ft, {2, 1}],
          Italic]}]}, LabelStyle -> {Red, 10}], Scaled[{0.8, 0.8}]]]
  (* The LineLegend ends *)
 
  }]

This more or less makes what you are looking for. The drawback is that (i) one has to spend some time playing with the placement and blank spaces to accurately position the two legends, and (ii) there is no simple way to make a common frame. Or at least I do not see such a way. One may, of course, make a custom legend himself, disregarding the built-in functions. This is not difficult, but it is not what you asked about.

Have fun, Alexei


Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone :  +352-2454-2566
Office fax:       +352-2454-3566
mobile phone:  +49 151 52 40 66 44

e-mail: alexei.boulbitch at iee.lu







  • Prev by Date: Re: how to set initial values to symbolic variable array?
  • Previous by thread: Legends for Data Plots
  • Next by thread: Formatting Input Cells within a NotebookWrite[]