Re: Reading Coordinates from Plot
- To: mathgroup at smc.vnet.net
- Subject: [mg80166] Re: Reading Coordinates from Plot
- From: Albert <awnl at arcor.net>
- Date: Tue, 14 Aug 2007 06:59:15 -0400 (EDT)
- References: <200708121115.HAA27714@smc.vnet.net> <f9p564$r44$1@smc.vnet.net>
Hi,
> The following code, based upon some posts of "Albert" to that thread,
> allows you to click points ONLY on the graph of the function and see the
> coordinates of that point below the plot, where from which you can copy
> the current coordinate pair and paste it elsewehere. Unfortunately, as
> it stands it does not allow you to produce all at once, and hence to
> copy, all the points you've clicked.
>
> trackPointOnPlot[f_,{a_,b_},strt_: Automatic]:=
> Module[
> {start=strt/.Automatic->a},DynamicModule[{p={start,f[start]}},
> Column[{
> LocatorPane[Dynamic[p,(p={First@#,f[First@#]})&],
> Plot[f[x],{x,a,b}]],
> Dynamic[p]
> }]
> ]
> ]
>
> (* examples to try *)
>
> trackPointOnPlot[Cos,{0,10},Pi]
> trackPointOnPlot[Cos,{0,10}]
Murray mentioned my code which was actually written for a slightly
different problem.
Since I was missing the behavior myself, I made another attempt to mimic
the original V5 behavior. Still this does not give a good answer why
something like that isn't provided with Mathematica in the first place.
Also worth a note is that with <<Version5`Graphics` one can get the old
behavior, including the reading off of coordinates.
The function below can be wrapped around any 2d Graphics-Object and
should work with most, a simple example usage is shown after the
definition. It shows you the coordinates of where your mouse position
sits and when clicking appends the coordinates to a list which is copied
to the clipboard. The copy to clipboard functionality can probably be
done better, what I came up with is rather a kind of workaround. Also I
have not found a possibility to get the information of whether the
Alt-Key is hold on the keyboard, that functionality seems to be limited
to regular letters and some more, according to the docs. I wonder why
this limitation has been necessary and irritated as I am :-) I don't
make use of any keyboard events at all...
Well, here is the code:
ShowAndGetCoordinates[g_Graphics] := Deploy[DynamicModule[{
coords = {Indeterminate, Indeterminate},
collected = {},
copytoclipboard = Function[
Block[{nb},
nb = CreateDocument[ExpressionCell[#], Visible -> False];
SelectionMove[nb, Before, Notebook];
SelectionMove[nb, Next, Cell];
SelectionMove[nb, All, CellContents];
FrontEndTokenExecute[nb, "Copy"];
NotebookClose[nb];
]
]
},
Panel[
Column[{
EventHandler[
Show[g,
Graphics[{AbsolutePointSize[6],
Dynamic[Point /@ collected]}]],
{
"MouseMoved" :> (coords = MousePosition["Graphics"]),
"MouseClicked" :> (
Block[{nb},
coords = MousePosition["Graphics"];
AppendTo[collected, coords];
copytoclipboard[collected];
]
)
}
]
,
Panel[
Column[{
Labeled[
InputField[
Dynamic[ToString@NumberForm[coords[[1]], {10, 2}]], String,
Enabled -> False, ImageSize -> 170], "x", Left],
Labeled[
InputField[
Dynamic[ToString@NumberForm[coords[[2]], {10, 2}]], String,
Enabled -> False, ImageSize -> 170], "y", Left],
Button["Reset", copytoclipboard[collected = {}],
ImageSize -> 70]
}]
]
}]
]
]]
Example Usage:
ShowAndGetCoordinates[
Plot[Sin[x], {x, 0, Pi}, PlotStyle -> Red, ImageSize -> 500,
Frame -> True]]
Or, if you prefer to see this in an extra window:
CreateDialog[
ShowAndGetCoordinates[
Plot[Sin[x], {x, 0, \[Pi]}, PlotStyle -> Red, ImageSize -> 500,
Frame -> True]]
Of course, like always, appearance is a matter of taste and there is
room for improvement, so let me know if you have some...
hth,
albert
- References:
- Reading Coordinates from Plot
- From: jrc <jrchaff@mcn.net>
- Reading Coordinates from Plot