2
$\begingroup$

In linear plot, this can be achived by Locator like this:

https://mathematica.stackexchange.com/a/24935/13580

data = Table[{x, x x }, {x, 0, 100}]; 
Manipulate[(p1 = Sort[p][[All, 1]]; 
         lm = LinearModelFit[Select[data, (p1[[1]] < #[[1]] < p1[[2]] &)], x, x];
         Show[ListPlot[data, PlotRange -> 10^4 {-1, 1}], 
                  Plot[lm[x], {x, 0, 100},  PlotStyle -> Red]]), 
{{p, {{1, 0}, {100, 0}}}, {1, 0}, {100, 0}, Locator}]

enter image description here

But in logrithmic plot, Locator seems not work, possibly because it only returns a linear coordinate in a graphic.

The following code is adapted from above. One locator is missing, and the other does not return desired coordinate.

data = Table[{x, x^2 + RandomInteger[10]}, {x, 1, 100}];
Manipulate[(p1 = Sort[p][[All, 1]];
  nlm = NonlinearModelFit[
    Select[data, (p1[[1]] < #[[1]] < p1[[2]] &)], a x^b, {a, b}, x];
  Show[ListLogLogPlot[data, PlotRange -> 10^4 {0.00001, 1},PlotLabel -> {nlm[x], p}], 
   LogLogPlot[nlm[x], {x, 0.1, 100}, PlotStyle -> Red]]), 
 {{p, {{1., 1.}, {100., 1.}}}, {1.,1.}, {100., 1.}, Locator}]

One alternative is to take log of the data, then use linear fitting. But I hope there is a direct way without this extra step.

$\endgroup$
2
$\begingroup$

Log Plots coordinates are confusing even built in things: Asymmetric X and Y error bars on ListLogPlot

Locators positions p (exact graphics coordinates) are referring to actual coordinates (those indicated by Axes) like: Exp @ p.

So if one want to plot a point on position {10,10^4} then Log @ {10,10^4} has to be provided.

data = Table[{x, x^2 + RandomInteger[10]}, {x, 1, 100}];
DynamicModule[{
  p = Log@{{1, 10}, {10, 10}}
  },
 LocatorPane[Dynamic@p, (

   Show[
    ListLogLogPlot[data, PlotRange -> 10^4 {0.00001, 1}, 
     ImageSize -> 500],
    Graphics[{
      Dynamic@Line[{{#, Log[1]}, {#, Log[10.^4]}} & /@ p[[All, 1]]],
      Dynamic[
       ControlActive[{},
        p1 = Sort[p][[All, 1]];
        nlm = 
         NonlinearModelFit[
          Select[data, (Exp@p1[[1]] < #[[1]] < Exp@p1[[2]] &)], 
          a x^b, {a, b}, x];
        First@LogLogPlot[nlm[x], {x, 0.1, 100}, PlotStyle -> Red]
        ]
       ]
      }]
    ])
  ]
 ]

enter image description here

$\endgroup$
2
$\begingroup$
data = Table[{x, x^2 + RandomInteger[10]}, {x, 1, 100}];
Manipulate[(p1 = Sort[p][[All, 1]];
  nlm = NonlinearModelFit[Select[data, (E^p1[[1]] < #[[1]] < E^p1[[2]] &)], a x^b, {a, b}, x];
  Show[ListLogLogPlot[data, PlotLabel -> {nlm[x], E^p}],
       LogLogPlot[nlm[x], {x, 0.1, 100}, PlotStyle -> Red]]),
 {{p, {{0., 0.}, {Log@100., 0.}}},
       {0., 0}, {Log@100., 0.}, Locator}]

Mathematica graphics

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