1
votes

Its some time series stock data I am plotting. I.e. x-axis is date while y is stock price.

My datafile "Cir2.txt" looks like this:

2/25/2016,18600,10/23/2008,11000

I would like to plot a circle with center at (2/25/2016,18600) and with radius of length that will have the point (10/23/2008,11000) be on the circle itself.

I plot it like this:

plot 'Cir2.txt' u 1:2:timecolumn((sqrt((timecolumn($1,"%m/%d/%Y")-timecolumn($3,"%m/%d/%Y"))**2 + ($2-$4)**2))/2) with circles

However, the radius plotted seems to be much shorter and it doesn't pass through the point (10/23/2008,11000) as expected. It seems the radius parameter need a very huge number to show something meaningful. And seems that related to the fact that x-axis is a date.

1
sorry, I mean this: plot 'Cir2.txt' u 1:2:(sqrt((timecolumn($1,"%m/%d/%Y")-timecolumn($3,"%m/%d/%Y"))**2 + ($2-$4)**2))/2) with circles - ALEUNG
full script below: set title "Hang Seng Gann" font ",20" set key off set size ratio -1 1,1 set xdata time set timefmt "%m/%d/%Y" set xrange ["01/01/1990":"05/29/2017"] noreverse set format x "%m/%d/%Y" set mouse mouseformat 3; set yrange [ 0.00000 : 32000.0000 ] noreverse nowriteback set xlabel 'Date' set ylabel 'Price' set grid set datafile separator "," plot 'HS2.txt' using 1:2:4:3:5 with candlesticks,\ 'Cir2.txt' u 1:2:((sqrt((timecolumn($1,"%m/%d/%Y")-timecolumn($3,"%m/%d/%Y"))**2 + ($2-$4)**2))/2) with circles - ALEUNG

1 Answers

1
votes

This is a bit complicated by the fact (as the documentation states) that the radius is in units of the x-axis:

The circles style plots a circle with an explicit radius at each data point. If three columns of data are present, they are interpreted as x, y, radius. The radius is always interpreted in the units of the plot's horizontal axis (x or x2). The scale on y and the aspect ratio of the plot are both ignored.

As a workaround, one could first generate an empty plot using p 1/0 to initialize the internal Gnuplot variables (see below), transform the coordinates into dimensionless ones, calculate the radius there, and finally express this radius in the units of the x-axis.

customFormat="%m/%d/%Y"

set datafile separator ","
set xdata time
set format x "%m/%Y"
set timefmt customFormat

set xrange ["01/01/1990":"05/29/2017"]
set yrange [0:32000]
set xtics ("10/23/2008" "10/23/2008" 0, "2/25/2016" "2/25/2016" 0)

set xlabel 'Date'
set ylabel 'Price'
set grid

unset key

p 1/0

ex = (GPVAL_X_MAX - GPVAL_X_MIN) / (GPVAL_TERM_XMAX - GPVAL_TERM_XMIN)
ey = (GPVAL_Y_MAX - GPVAL_Y_MIN) / (GPVAL_TERM_YMAX - GPVAL_TERM_YMIN)

fn(x0, x1, y0, y1) = sqrt( ((x1-x0)/ex)**2 + ((y1-y0)/ey)**2 )*ex

p \
    'Cir2.txt' u 1:2 w p lc rgb 'red', \
    '' u 3:4 w p lc rgb 'blue', \
    '' u 1:2:(fn(timecolumn(1, customFormat), timecolumn(3, customFormat), $2, $4)) w circles lc rgb 'black'

This then produces:

enter image description here