Question

I have these data.txt:

[1384395497218,679],[1384399097218,679],[1384402697218,679]...

[timestamp,value]

Where first is timestamp 1384395497218 -> 1384395497.218 -> Thu, 14 Nov 2013 02:18:17 GMT. The second is count of something

I would like to plot lines graph with dates on the x-axis and count on the y-axis. How can I do this?

Example:
x-axis = "14 November" y-axis = "679"..
Was it helpful?

Solution

That is a quite strange input format. If you can, you should choose one which is better suited. Gnuplot cannot handle this, you must preprocess your data file before plotting it. That can be done e.g. with perl and regular expressions as follows:

If you have a data file test.txt with the values

[1384395497218,679],[1384399097218,679],[1384402697218,679]

and use the following call on the command line:

perl -pe 's/\[(\d+)([\d]{3}),(\d+)\],?/\1.\2 \3\n/g' test.txt

you get your data formatted as

1384395497.218 679
1384399097.218 679
1384402697.218 679

which can be plotted with gnuplot. To have the preprocessing done on-the-fly, use the syntax

plot '< perl ...'

A short script for you data is e.g.:

set timefmt '%s'
set xdata time
set format x '%d %B'
set xtics 24*60*60
getdata = '< perl -pe ''s/\[(\d+)([\d]{3}),(\d+)\],?/\1.\2 \3\n/g'' test.txt'
plot getdata using 1:2 with linespoints notitle

Some more notes:

  • The time format %s reads in a timestamp, which can also have decimal digits (You could also drop them.
  • set xtics 24*60*60 sets one major tic at every day
  • With set format x '%d %B' you can set a different format for the output formatting of the time on the x-axis.

Result with 4.6.4 is:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top