I would like to make a candlesticks chart with the day in the X axis for ever 5 days. But when I use "xticlabels" it just ignore the "set xtics 5"
My code:
set terminal pdf size 29.7cm,21.0cm dashed
set output 'Ciclo.pdf'
set datafile separator ","
set border linecolor rgbcolor "black"
set palette defined (-1 'red', 1 'green')
set cbrange [-1:1]
unset colorbox
d_source = "ciclo.csv"
set grid x
set grid y
set title "Title"
unset xlabel
set xtics 5, 5
set xtics rotate by -45
unset ylabel
set ytics
set style fill solid
plot d_source using 1:3:4:5:6:($6 < $3 ? -1 : 1) with candlesticks palette notitle
plot d_source using 1:3:4:5:6:($6 < $3 ? -1 : 1):xticlabels(2) with candlesticks palette notitle
My data set is: Count, Date, Open, High, Low, Close
the output with out xticlabels:
Edit 1:
There is gaps in the financial data (holidays and weekends). and formatting the x-axis as date or I will have gaps in my plot or it will present the wrong date.
Rephrasing the question, how to control the number of tics with text labels.
Edit 2:
Using the bellow function will do the trick:
everyNth(countColumn, labelColumnNum, N) = \
( (int(column(countColumn)) % N == 0) ? stringcolumn(labelColumnNum) : "" )
set xtics rotate 90
plot d_source using 1:3:4:5:6:($6 < $3 ? -1 : 1):xticlabels(everyNth(1, 2, 5)) with candlesticks palette notitle
Theozh solution (second script) is the best one!
N = 5
plot d_source using 1:3:4:5:6:($6 < $3 ? -1 : 1) with candlesticks palette notitle, \
d_source every N using ($0*N):(NaN):xtic(2)
If you use xticlabels()
or shorter xtic()
, your labels will be text labels, not numerical values.
So, if you want the x-axis to be a time axis, simply define it as such.
set format x "%d/%m/%Y" timedate
In gnuplot, time is handled as seconds passed since January, 1st 1970.
If you now want tics every five days set xtics 5*24*3600
(which is five days in seconds).
Script:
### plot timedata candlestick
reset session
fmt = "%d/%m/%Y"
secPerDay = 24*3600
# create some random test data
set table $Data
y0 = 100
t0 = time(0)
plot '+' u 0:(strftime(fmt,t0+$0*secPerDay)):(y0):(yH=y0+rand(0)*10): \
(yL=y0-rand(0)*10):(y0=yL+rand(0)*(yH-yL)) w table
unset table
set grid x,y
set rmargin 8
set key noautotitle
set style fill solid noborder
set format x fmt timedate
set xtics 5*secPerDay rotate by -45
color(colO,colC) = column(colC)<column(colO) ? 0xff0000 : 0x00ff00
plot $Data u (timecolumn(2,fmt)):3:4:5:6:(color(3,6)) w candlesticks lc rgb var
### end of script
Result:
Addition:
Here is a version where the data has no weekend days and only every N
th tic is actually labeled.
pseudocolumn 0
as x-value (check help pseudocolumns
)N
th (here: 5) data point setting the date as text xtic labelScript: (requires gnuplot>=5.4, because of w table if ...
)
### plot time data without weekend/holiday gaps
reset session
fmt = "%d/%m/%Y"
secPerDay = 24*3600
# create some random test data without weekends
set table $Data
y0 = 100
t0 = time(0)
plot '+' u (d=int(tm_wday(t0))%6, sprintf("%d %s %g %g %g %g", $0, \
strftime(fmt, t0=t0+secPerDay), y0, yH=y0+rand(0)*10, \
yL=y0-rand(0)*10, y0 = d ? yL+rand(0)*(yH-yL) : y0)) w table if d
unset table
set grid x,y
set rmargin 8
set key noautotitle
set style fill solid noborder
set format x fmt timedate
set xtics 5*secPerDay rotate by -45
color(colO,colC) = column(colC)<column(colO) ? 0xff0000 : 0x00ff00
N = 5
plot $Data u 0:3:4:5:6:(color(3,6)) w candlesticks lc rgb var, \
'' every N u ($0*N):(NaN):xtic(2)
### end of script
Result: