Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make gnuplot show coordinates of a plotted function which have same x value as the mouse pointer?

Tags:

gnuplot

If in gnuplot I type plot(x**2), and get a plot of this function, in the lower-left corner of the plot I can see the coordinates corresponding to the position of my mouse pointer.

What I would like to know is if there is a way to "snap" the pointer to the function's graph (or rather the crosshairs whose coordinates are shown, making them share the same x-coordinate as the mouse-pointer).

The end effect will be that as I move my mouse left-to-right along the plot, a crosshair which has the same x-value as the pointer will be shown directly on the function's graph, and the current coordinates of this crosshair will be printed somewhere (e.g. in the lower-left corner of the plot). In other words, the printed crosshair coordinates would always be (x, f(x)) for some x value.

like image 477
Mark Avatar asked Jan 30 '13 22:01

Mark


1 Answers

While this would be useful for functions (x, f(x)), please note that gnuplot can also plot parametric functions as well as 2D and 3D surfaces, so this functionality would be of limited use. Note also that you can already output tables with set table and output values to the console with for and print.

If you do need interactivity, here is an MWE that outputs pairs of (x, f(x)) according to the mouse pointer X position if you click into the screen, as a label on the screen as well as via print to the console (remove as necessary).

#!/usr/bin/gnuplot -persist

## this binds commands to the mouse click that uses the MOUSE_X variable
## to do what you want
bind all "Button1" \
  'result=sprintf("(x, f(x)) = (%g, %g)", \
  MOUSE_X, f(MOUSE_X)); \
  set label 1 result at graph 0.05, graph 0.05; \
  print result; replot'

f(x) = x**2

plot f(x)
## the pause is needed only to keep gnuplot running,
## so you see the print output
## the label works without the pause
pause mouse
like image 94
Andreas Avatar answered Feb 23 '23 16:02

Andreas