bashcsvgnuplotgnu

Cannot change date format in gnuplot


I want to adjust the x axis format whilst using a csv file to load my data. The script below does not work with the associated data.

min_example.csv

12-03 17:40:54,6
12-03 18:40:54,5
12-03 19:40:54,9
set datafile separator ","
set xdata time
set timefmt "%d-%m %H:%M:%S"
set format x "%d %H"
plot 'min_example.csv' using 1:2:xtic(1)

The output is still in the "%d-%m %H:%M:%S" format. Many thanks!

When using a dataset with more data (e.g., 12/4) it still sorts correctly by date and time, but I can't modify anything else. The xtics are an explicit list. I already checked that the "space" character is not a \t; it is definietly a space.


Solution

  • Your script has bypassed normal formatting of the x-axis tic labels by using the command plot ... using 1:2:xtic(1). The xtic(1) part tells it to use the content (i.e. the character string) in column 1 as the tic label.

    You need to apply the new format to the time value represented by the string in column 1, not to the character string itself. The time value represent by the string in column 1 can be accessed as timecolumn(1). Let's define a function to apply the new format to a time value:

    reformat(time) = strftime("%d %H", time)
    

    and now rewrite the script to use that function in plotting

    $DATA << EOF
    12-03 17:40:54,6
    12-03 18:40:54,5
    12-03 19:40:54,9
    EOF
    
    set datafile separator ","
    set xdata time
    set timefmt "%d-%m %H:%M:%S"
    reformat(time) = strftime("%d %H", time)
    plot '$DATA' using 1:2:xtic(reformat(timecolumn(1)))
    

    enter image description here