Reputation: 639
In Gnuplot, can I highlight specific values on a linear graph? By highlight", I mean draw the lines from the axis values to a specific point on the graph and print that spot with a dot. I have a few such interesting values. I can "draw" all this manually with "set arrow" etc, but I wonder if there's a built-in mechanism.
Upvotes: 1
Views: 301
Reputation: 26123
Here is a different solution:
If you don't have a function but a data file, you need to adapt the script.
Script:
### mark special points on a function
reset session
f(x) = x**2 + 4*x + 1
$Marks <<EOD
-5
7
EOD
do for [i=1:|$Marks|] {
x0 = real($Marks[i])
y0 = f(x0)
set obj i rect from graph -1, graph -1 to x0,y0 fs empty
set label i at x0,y0 sprintf("(%g,%g)",x0, y0) point pt 7 lc "red" right offset 0,1
}
plot f(x) w l lc "blue"
### end of script
Result:
Upvotes: 1
Reputation: 2476
EDIT2: As @Ethan answered, there will be a built-in solution since version 6.0. Until then, there is a hand-made solution, if you have a function f(x) [left image]:
# gnuplot 5.4.4
reset; set grid;
xmin=0
ymin=0
set xrange [xmin:]
set yrange [ymin:]
highlight(x, y, xmin, ymin) \
= sprintf("set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set label at %f,%f point pt 7 lc -1 ps 2; ", xmin, y, x, y, x, ymin, x, y, x, y)
f(x)=0.5*x+0.2
eval highlight(4,2.2,xmin,ymin)
eval highlight(6,f(6),xmin,ymin)
plot f(x) notitle
EDIT: as the OP asked for a solution which uses data points from a file instead of a function, there is a possible solution [right image]:
# gnuplot 5.4.4
reset; set colors classic; set grid
datafile='007_d.dat'
xmin=0
ymin=0
set xrange [xmin:]
set yrange [ymin:]
highlight(x, y, xmin, ymin) \
= sprintf("set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set label at %f,%f point pt 7 lc -1 ps 2; ", xmin, y, x, y, x, ymin, x, y, x, y)
byx(x) \
= sprintf("plot datafile u ($1 == %f ?$1:1/0):2; \
eval highlight(GPVAL_DATA_X_MIN,GPVAL_DATA_Y_MIN,xmin,ymin)", x)
byy(y) \
= sprintf("plot datafile u ($2==2.7?$1:1/0):2; \
eval highlight(GPVAL_DATA_X_MIN,GPVAL_DATA_Y_MIN,xmin,ymin)", y)
eval highlight(4,2.2,xmin,ymin)
eval byx(6)
eval byy(2.7)
plot datafile w lp ps 2 notitle
NOTE: you can use datafile as an extra parameter for byx() / byy() ofcourse; also you can play with xmin amd ymin as your particular needs.
ATTENTION: the code assumes, that you have unique x-y data pairs.
Upvotes: 1
Reputation: 15118
My data come from a file, not a math function. Is there a way to get the y value for a given x, so that "highlight" would automatically retrieve it for a given x?
Gnuplot version 6 will support this. It has been in the development branch for a while now. If you are willing to build from source or try the test build, it is available in the release candidate for 6.0 available from the gnuplot home site on SourceForge.
Full documentation and example figure here: http://gnuplot.info/docs_6.0/loc4436.html
Short example:
plot DATA using 1:2 smooth cnormal watch y=0.25 watch y=0.5 watch y=0.75
Upvotes: 3
Reputation: 180
I think you could simplify this by replacing xmin with GPVAL_X_MIN and ymin with GPVAL_Y_MIN.
These are builtin variables.
reset
set grid
highlight(x, y) \
= sprintf("set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set label at %f,%f point pt 7 lc -1 ps 2; ", GPVAL_X_MIN, y, x, y, x, GPVAL_Y_MIN, x, y, x, y)
f(x)=0.5*x+0.2
eval highlight(4,2.2)
eval highlight(6,f(6))
plot f(x) notitle
replot
The replot is there because the builtin variables are not set until you plot once.
Upvotes: 0