4

I've been searching for a while now to find out how to remove days of the week from a financial plot with no success.

I need the plot to just include the days of the week and completely miss out the weekends such that there is no 2-day gap in the financial chart.

I have the data in CSV format Open/Low/Close/High and it has the weekend data missing, it plots fine but I can't find how to not show the weekends, any help would be really appreciated.

I'd like to see it say M/T/W/T/F/M/T/W/T/F on the X basically rather than M/T/W/T/F/S/S/M etc...

Cheers,

Chris.

Chris
  • 367
  • 4
  • 13
  • 3
    I don't think people understand the question. It's NOT about how to remove weekends from the data file. There are NO weekend entries in the data file. The problem is that if you set the x axis to time mode, gnuplot reserves a point on the x axis for EACH calendar day. It inserts blank spaces on the x axis for weekends, EVEN IF there are NO entries for those days. I.e. gnuplot treats the x axis as a continuous timeline containing ALL calendar days. The OP asks how NOT to have spaces corresponding to weekends included (shown) on the x axis, as if those days didn't exists in the calendar at all. – MrSparkly Aug 07 '13 at 20:47

6 Answers6

1

As far as I know, this cannot be done with gnuplot itself - you need to bring the file into the desired shape before. If you are on Linux, this can be done with something like

awk '{if( index( $1, "S" ) == 0 ) print $0 >> "new.dat"}' old.dat

where old.dat is your original file and new.dat the new file without weekends. I have assumed here that your data file has the weekday as the first entry in each line.

This would work under Windows as well, but you would need to install Gawk for Windows first.

vaettchen
  • 7,299
  • 22
  • 41
0

I just came across set xdtics today. I doubt you're still working on this, but maybe that will be helpful for someone else... (see help xdtics)

mgilson
  • 300,191
  • 65
  • 633
  • 696
0

Assuming your data file isn't missing any weekdays, you can treat the date column as a string type. (If you're missing weekdays, your chart will skip over those dates without allocating any space for them, which is easy to miss, so beware.)

I have a date as the first column in my data file in YYYY-MM-DD format. The data I'm plotting is in the second column. Here are the relevant lines of my gnuplot configuration:

set format x '%s'
plot 'file' using 0:2:xtic(substr(strcol(1),6,10))

The set format line tells gnuplot how to print the x labels. The using config uses column 0 (the index) as the x parameter, column 2 (the data) as the y parameter, and provides special instructions for printing labels: only print characters 6–10. (This chops off the year portion, which helps the label fit without overlapping in my case.)

Also see this SO answer. I wouldn't want to replicate this "broken axis" solution for every weekend, but perhaps it could serve to inspire.

Jeff Terrell Ph.D.
  • 2,563
  • 26
  • 39
0

If you want to neglect the weekends on the time scale you can simply define a function which returns the day number after time(0) omitting the weekends. Note, that time(0) is 1970-01-01 00:00:00 for gnuplot 5.x and 2000-01-01 00:00:00 for gnuplot 4.x.

  • dw5(t) returns day number after time(0) omitting the weekends and NaN if t is a weekend day.
  • dw5tow7(n) returns the date from the "5-day-week" day number and NaN if input is NaN.
  • dw7Tic(n) returns the date label for xtic and empty string '' if input is NaN.

For example, the command:

do for [i=0:10] { print sprintf("%s % 4d",strftime("%Y-%m-%d",dw5tow7(i)),i) }

will return in gnuplot5.x:

1970-01-01    0
1970-01-02    1
1970-01-05    2
1970-01-06    3
1970-01-07    4
1970-01-08    5
1970-01-09    6
1970-01-12    7
1970-01-13    8
1970-01-14    9
1970-01-15   10

Script: (works for gnuplot>=5.0.0, Jan. 2015)

### remove weekends on time scale
reset

FILE = "SO9680677.dat"

# create some random test data
set print FILE
    t0 = time(0)
    y0 = 100
    do for [i=0:60] {
        t  = t0 + i*3600*24
        if (int(tm_wday(t)+1)%7>1) { 
            print sprintf("%s %g",strftime("%Y-%m-%d",t),y0=y0+rand(0)*1-0.5)
        }
    }
set print

SecPerDay    = 3600*24
SecPerWeek   = 7*SecPerDay
isWeekend(t) = int(tm_wday(t)+1)%7 < 2
myTimeFmt    = "%Y\n%m-%d"
tOff         = tm_year(0)==1970 ? 3 : 5     # offset gnuplot5.x: 3, gnuplot4.x: 5
dw5(t)       = isWeekend(t) ? NaN : int(t/SecPerDay) - 2*int((t+tOff*SecPerDay)/SecPerWeek)
dw5tow7(n)   = n==n ? n*SecPerDay + (int(n+tOff)/5)*2*SecPerDay : NaN
dw7Tic(n)    = n==n ?  strftime(myTimeFmt,dw5tow7(n)) : ''

set key top center out noautotitle
set grid x,y
set ytics 1

set multiplot layout 2,1

    set xrange[:] noextend
    set format x myTimeFmt timedate
    plot FILE u (timecolumn(1,"%Y-%m-%d")):2 w lp pt 7 lc rgb "red" ti "with weekends"

    set format x "%g\n" numeric
    plot FILE u (dw5(timecolumn(1,"%Y-%m-%d"))):2 w lp pt 7 lc rgb "web-green" ti "without weekends", \
           '' u (t0=dw5(timecolumn(1,"%Y-%m-%d"))):(NaN):xtic(dw7Tic(t0)) every 5

unset multiplot
### end of script

Replace the above multiplot section with the snippet below and the script will run with gnuplot>=4.6.0 (March 2012). Maybe with further tweaking it can be made work with gnuplot 4.4.0.

### version for gnuplot 4.6.0, March 2012
set multiplot layout 2,1
    set timefmt "%Y-%m-%d"
    set xdata time
    set format x myTimeFmt
    plot FILE u 1:2 w lp pt 7 lc rgb "red" ti "with weekeends"

    set format x "%g\n"
    plot FILE u (dw5(timecolumn(1))):2 w lp pt 7 lc rgb "web-green" ti "without weekends", \
           '' u (t0=dw5(timecolumn(1))):(NaN):xtic(dw7Tic(t0)) every 5 w p
unset multiplot

Result:

Actually, after all, the larger your time range the less you will notice if there are weekends or not.

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
0

The data is not shown in the file, the file is just weekday based and misses the weekends. If you plot the data you get these 2day gaps at the weekend so I want to remove these gaps. It's more really to do with the x axis having weekends in to make it linear.

Here is an example of part of the file:

2006-03-23T16:59 1.7470 1.7324 1.7471 1.7344 0.0000 0.0000 0.0000 0.0000
2006-03-24T16:59 1.7346 1.7308 1.7441 1.7428 0.0000 0.0000 0.0000 0.0000
2006-03-27T17:59 1.7424 1.7415 1.7492 1.7459 0.0000 0.0000 0.0000 0.0000
2006-03-28T17:59 1.7462 1.7422 1.7537 1.7424 0.0000 0.0000 0.0000 0.0000

If you look at the dates, there are gaps in the file. There should be gaps because these days have no data. The graph however should run without gaps and that is what I am trying to achieve.

Chris
  • 367
  • 4
  • 13
  • Just a hacky idea: You could replace the date / time strings with line numbers, so you get your graph without gaps, and add the lost date information using appropriate text labels. – vaettchen Mar 20 '12 at 04:10
-1

Using some external tool (I would wrote a bash or python script for that, I believe; it should not be difficult), you can insert lines for weekend days (one line for a day) into your data file, like this:

2006-03-26T00:00 NaN NaN NaN NaN NaN NaN NaN NaN

(or you can just append those NaNs for weekends at the end of data file and use unique keyword)

and then plot, let's say, the first data with using 1:($2) with linespoints, not using 1:2 ...

This should work for you.

brownian
  • 435
  • 3
  • 13