4

I am trying to reproduce the answer here about Recovering data points from an image. I click the figure and press "Get Coordinates". I would like to copy the four coordinates by CTRL-C and then paste it to a newline by CTRL-V. However, it does not work. Systems tested: Ubuntu 16.04 64 bit, Linux Mint 17.

JasonB. The article Get Coordinates for Points in a Plot shows that you should be able to copy-paste the coordinates by CTRL-C/V but it does not work stably. I would like to understand why.


How can you copy coordinates of "Get Coordinates" in Figure?

2 Answers2

4

This turned out to be pretty simple, using ClickPane to capture the clicks, and MousePosition so that you can see the current coordinates.

After running the code below, and placing your mouse over the plot, the coordinates are displayed above the plot. After clicking, the coordinates you click are added to the list pts. This bypasses any interaction with the clipboard, which is desirable because it doesn't work very well in Linux (or at least in my installation).

image = Plot[Sin[x], {x, 0, 10}, ImageSize -> 400];
pts = {};
ClickPane[
 Column[{Dynamic[MousePosition["Graphics", "Out of range"]],
   image}],
 AppendTo[pts, #] &
 ]

enter image description here

You can make a function out of this via

SetAttributes[getCoordinates, HoldAll];
getCoordinates[image_, pts_] := Module[{},
  ClearAll@pts;
  pts = {};
  ClickPane[
   Column[{Dynamic[MousePosition["Graphics", "Out of range"]], 
     image}], AppendTo[pts, #] &]
  ];

The first argument of the function is the plot, and the second is the variable to which you want to assign the coordinates list. For example,

getCoordinates[
 DensityPlot[Sin[x^2 - y], {x, -5, 5}, {y, -5, 5}, 
  PlotPoints -> 50], mypointslist]

After entering the above, I click around a few times collecting points, and the points are stored in the list mypointslist

Jason B.
  • 68,381
  • 3
  • 139
  • 286
1

You might try to use Locatorfor this purpose. Try this:

    coord = {};

DynamicModule[{pt = ImageDimensions[im]/2 // N},
 Column[{
   Show[{
     image,
     Graphics[Locator[Dynamic[pt]]]
     }],
   Dynamic[pt],
   Button["Get coordinates", Clear[coord]; coord = pt]
   }]
 ]

where ´image` is the name of your image and coord is the global variable. You find the right place for the locator while reading the coordinates under the image, if necessary. As soon as you have a right place, press the button and the global variable, coord, will contain the chosen coordinates.

Have fun!

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96
  • Can you please explain little more how these commands work. So you propose this package https://reference.wolfram.com/language/ref/Locator.html – Léo Léopold Hertz 준영 May 03 '16 at 15:47
  • @Masi You should go to Menu/Help/Wolfram Documentation and type there the commands, such as Locator and others and you will get their detailed explanation. If after that you have questions, do ask them. – Alexei Boulbitch May 04 '16 at 09:57