Search code examples
gnuplot

How can I make gnuplot skip every other label on the x-axis?


I am trying to plot some data using gnuplot. I gather the data on a daily basis, and I want to display the date on the x-axis when that data point was collected. I have figured out how to do that, but as I add data, the date labels begin to overwrite themselves. You can see the effect if you run the following script.

# Demonstrates the xtic overlap problem
# set terminal pngcairo size 640, 480
set title "Xtic Overlap Demonstration"
# set output "XticProblem.png"
set yrange[0:]
set xtics rotate font "Times,28"
# set xtics rotate font "Times,10"
set bmargin 7
set tmargin 4
plot "dates_basic.dat" using 2:xtic(1) with linespoints

Here is the dataset that I am using to test, stored in file "dates_basic.dat"

2023-11-06  0.8
2023-11-07  0.5
2023-11-08  2.0
2023-11-09  0.9
2023-11-10  1.3
2023-11-11  0.7
2023-11-12  1.2
2023-11-13  1.4
2023-11-14  1.5
2023-11-15  1.3
2023-11-16  0.3
2023-11-17  0.4
2023-11-18  0.7
2023-11-19  0.9
2023-11-20  0.4
2023-11-21  0.9
2023-11-22  0.4
2023-11-23  1.3
2023-11-24  1.4
2023-11-25  2.3
2023-11-26  1.8
2023-11-27  1.7
2023-11-28  0.6
2023-11-29  1.2
2023-11-30  1.2
2023-12-01  1.0
2023-12-02  1.7
2023-12-03  1.2
2023-12-04  1.4
2023-12-05  1.0
2023-12-06  1.4
2023-12-07  1.0
2023-12-08  0.4
2023-12-09  1.1
2023-12-10  0.7

In the example code I have set a large font so that you can see the effect. I would not normally use that large font, but even if I use a smaller font, the labels will eventually overwrite themselves when I go past a certain number of data points.

I have been trying to fix the issue by skipping every second label, leaving just the tic marks in those locations.

Everything I have tried has failed. I even tried using some modulo arithmetic on the record number to display spaces instead of the label on every other tic.

plot "dates_basic.dat" using 2:xtic( $0 % 2 > 0 ? " " : $1 ) with linespoints

but that fails with an error saying that $0 is not an integer.

"xtics-example.txt" line 10: non-integer operand for %

I have been searching for the solution for days, and I have found nothing that works.

How do it skip every other xtic label, or alternatively is there a better way to display the date information?

Thanks,

Banjo


Solution

  • If you have time data, I wouldn't recommend to use xtic() for creating the xtic labels. gnuplot is treating time data as seconds from January, 1st 1970. And gnuplot has some auto tic implemented (which mostly) gives reasonable results. In your case, it suggests a 7 day interval of the xtic labels, but you can force any other interval in seconds, e.g. 4 days: set xtics 3600*24*4.

    For further reading, check help xtics, help xtics time, help timecolumn, help time_specifiers, etc.

    Data: SO77648067.dat

    2023-11-06  0.8
    2023-11-07  0.5
    2023-11-08  2.0
    2023-11-09  0.9
    2023-11-10  1.3
    2023-11-11  0.7
    2023-11-12  1.2
    2023-11-13  1.4
    2023-11-14  1.5
    2023-11-15  1.3
    2023-11-16  0.3
    2023-11-17  0.4
    2023-11-18  0.7
    2023-11-19  0.9
    2023-11-20  0.4
    2023-11-21  0.9
    2023-11-22  0.4
    2023-11-23  1.3
    2023-11-24  1.4
    2023-11-25  2.3
    2023-11-26  1.8
    2023-11-27  1.7
    2023-11-28  0.6
    2023-11-29  1.2
    2023-11-30  1.2
    2023-12-01  1.0
    2023-12-02  1.7
    2023-12-03  1.2
    2023-12-04  1.4
    2023-12-05  1.0
    2023-12-06  1.4
    2023-12-07  1.0
    2023-12-08  0.4
    2023-12-09  1.1
    2023-12-10  0.7
    

    Script:

    ### plot time data
    reset session
    
    FILE = "SO77648067.dat"
    
    myTimeFmt = "%Y-%m-%d"
    set format x "%Y\n%m-%d" timedate   # \n is newline
    # set xtics 3600*24*4               # uncomment this line to force xtic labels every 4 days
    
    plot FILE u (timecolumn(1,myTimeFmt)):2 w lp pt 7 lc "red"
    ### end of script
    

    Result:

    enter image description here