Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive

MathGroup Archive 2013

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

Search the Archive

Re: fit curve on to an imported graph

  • To: mathgroup at
  • Subject: [mg131215] Re: fit curve on to an imported graph
  • From: Alexei Boulbitch <Alexei.Boulbitch at>
  • Date: Mon, 17 Jun 2013 06:31:12 -0400 (EDT)
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:

I have a graph in .png format generated from other software and want to
generate some curves like (a/x)^-1/b in mathematica and compare the curve
to the graph directly.

I am wondering how could I import the .png image and generate the
(a/x)^-1/b on to that image?

If the curve can be shown in Manipulate[...] way that would be better,
since I can adjust the parameters a and b and see the changing on the
background of the imported image.

thanks for your help!


I have written a program to do such things. This program uses the image with a curve or points on it and  retrieves it in a form of a list. After that you may use standard Fit, FindFit or Manipulate to fit your model to that list.

I have already published this program on this site, however, today I did not manage to find a reference to it. In addition I improved few things in it . For these reasons I will publish it here once more. It consists of the description of a function copyCurve[ ] and of the code defining the function itself.

Write in a Mathematica notebook:

im=Image["copy-paste here your image" ,ImageSize->450]

Then apply the copyCurve function to it:


and follow the instructions below.

Have fun, Alexei

The function copyCurve


The function copyCurve enables one to get the coordinates of points of a curve plot found on an image, and memorizes them in a list entitled "listOfPoints"


image is any image. It should have Head=Image, if it is a Graphics object wrap it with Image statement. The code uses specific Image properties during the rescaling.


The Checkbox "whiteLocatorRing" defines, if the locators are shown by a single color ring (unchecked), or with two rings, the outer having a color defined by the ColorSlider (see below), the inner one being white. This may be helpful, if working with a too dark image.

size controls the size of the image. The default value is 450. This slider is used to adjust the size to the one to enable the most comfortable work with the image plot.

opacity controls the opacity of the line connecting the locators

thickness controls the thickness of the double ring that forms each locator.

lineThickness controls the thickness of the line connecting the locators

color is the colour slider that controls the colour of the outer ring forming the locator and the line connecting them. The inner locator ring is always white or no white ring at all.

radius controls the radius of the locators.

InputFields: should be supplied by the reference points x1 and x2 at the axis x, as well as y1 and y2 at the axis y.

Buttons: The buttons "Memorize scale X" and "Memorize scale Y" should be pressed after the first two locators are placed on the corresponding reference points (presumably, located at the x or y axes). Upon pressing the button "memorize scale ..." the corresponding reference points are memorized. The button "Make list of the curve points" should be pressed at the end of the session. Upon its pressing the actual list of points representing those of the curve is assigned to the global variable "listOfPoints"

Operation sequence:

Step 1: Execute the function

Step 2: Enter the reference points at the plot x axes into the input fields. Press Enter.

Step3: Alt+Click on the point with x-coordinate x1. This brings up the first locator visible as a circle. Alt+Click (Command + Click for Mac OS X) on that with x2 which gives rise to the second locator. Adjust the locators, if necessary. Press the button "Memorize scale X".

Step 4: Enter the reference points at the plot y axes into the input fields. Press Enter. Move the two already existing locators to the points with the coordinates y1 and y2. Press the button "Memorize scale Y". Now the both scales are captured.

Step 5: Move the two already existing locators to the first two points of the curve to be captured. Alt+Click on other points of the curve. Each Alt+Click will generate an additional locator. Adjust locators, if necessary. To remove, press Alt+Click on unnecessary locators.

Step 6: Press the button "Make the list...". This assigns the captured list to the global variable "listOfPoints". Done.

The "listOfPoints" is a global variable. It can be addressed everywhere in the notebook.

The function copyCurve


copyCurve[image_] :=
   DynamicModule[{pts = {}, x1 = Null, x2 = Null, y1 = Null, y2 = Null, X1,
     X2, Y1, Y2, \[CapitalDelta]X, \[CapitalDelta]Y, g, myRound},
    myRound[x_] := Round[1000*x]/1000 // N;
    (* Begins the column with all the content of the manipulate *)
      (* Begin LocatorPane*)
         Show[{Image[image, ImageSize -> size],
           Graphics[{color, AbsoluteThickness[lineThickness],
             Opacity[opacity], Line[Union[pts]]}]
               }], LocatorAutoCreate -> True,
        (* Begin Locator appearance *)
        Appearance -> If[whiteLocatorRing,
          Graphics[{{color, AbsoluteThickness[thickness],
             Circle[{0, 0}, radius + thickness/2]}, {White,
             AbsoluteThickness[thickness], Circle[{0, 0}, radius]}},
           ImageSize -> 10]
          Graphics[{{color, AbsoluteThickness[thickness],
             Circle[{0, 0}, radius + thickness/2]}}, ImageSize -> 10]](*
        End Locator appearance *)
                              ],(* End LocatorPane*)
      (* Begin of the block of InputFields *)
      , Row[{ Style["\!\(\*SubscriptBox[\(x\), \(1\)]\):"],
         FieldHint -> "Type  \!\(\*SubscriptBox[\(x\), \(1\)]\)",
         FieldSize -> 7, FieldHintStyle -> {Red}],
        Spacer[20], Style["   \!\(\*SubscriptBox[\(y\), \(1\)]\):"],
         FieldHint -> "Type  \!\(\*SubscriptBox[\(y\), \(1\)]\)",
         FieldSize -> 7, FieldHintStyle -> {Red}]
      Row[{ Style["\!\(\*SubscriptBox[\(x\), \(2\)]\):"],
         FieldHint -> "Type \!\(\*SubscriptBox[\(x\), \(2\)]\)",
         FieldSize -> 7, FieldHintStyle -> {Red}],
        Spacer[20], Style["   \!\(\*SubscriptBox[\(y\), \(2\)]\):"],
         FieldHint -> "Type  \!\(\*SubscriptBox[\(y\), \(2\)]\)+Enter",
         FieldSize -> 7, FieldHintStyle -> {Red}]
      (* End of the block of InputFields *)
      (* Begin the buttons row *)
        (* Begin button "Memorize scale X" *)
        Button["Memorize scale X",
         X1 = Min[Transpose[myRound /@ Union[pts]][[1]]];
         X2 = Max[Transpose[myRound /@ Union[pts]][[1]]];
         \[CapitalDelta]X = X2 - X1;
         ],(* End of button "Memorize scale X" *)
        (* Begin button "Memorize scale Y" *)
        Button["Memorize scale Y",
         Y1 = Min[Transpose[myRound /@ Union[pts]][[2]]];
         Y2 = Max[Transpose[myRound /@ Union[pts]][[2]]];
         \[CapitalDelta]Y = Y2 - Y1;
         ](* End of button "Memorize scale Y" *)
        }],(* End the buttons row *)
      (* Begin button "Make the list of the curve's points" *)
      Button[Style["Make the list of the curve's points" , Bold],
       g[{a_, b_}] := {(x1*X2 - x2*X1)/\[CapitalDelta]X +
          a/\[CapitalDelta]X*Abs[x2 - x1], (y1*Y2 - y2*Y1)/\[CapitalDelta]Y+
          b/\[CapitalDelta]Y*Abs[y2 - y1]};
       listOfPoints = Map[myRound, Map[g, pts]]
                   ](* End of button "Make the list..." *)
      }, Alignment -> Center](*
    End of column with all the content of the manipulate *)
    ],(* End of the DynamicModule *)
   (* The massive of sliders  begins *)
   Column[{Row[{Control[{whiteLocatorRing, {True, False}}], Spacer[50]}],
     Row[{Spacer[32.35], Control[{{size, 450}, 300, 800}], Spacer[38.5`],
       Control[{{opacity, 0.5}, 0, 1}]}],
     Row[{Spacer[10.], Control[{{thickness, 1}, 0.5, 5}], Spacer[13.65],
       Control[{{lineThickness, 1}, 0, 10}] }],
     Row[{Spacer[22.8], Control[{color, Red}], Spacer[59.3],
       Control[{{radius, 0.5}, 0, 3}]}]
     }, Alignment -> Center],(* The massive of sliders ends *)
   (* Definitions of sliders *)
   ControlType -> {Checkbox, Slider, Slider, Slider, Slider, ColorSlider,
   ControlPlacement -> Top, SaveDefinitions -> True

Alexei BOULBITCH, Dr., habil.
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

  • Prev by Date: Re: computation of two-point objects
  • Next by Date: Re: Calculation of a not so simple integral
  • Previous by thread: fit curve on to an imported graph
  • Next by thread: Getting the plotted data from a graph