Skip to content

Instantly share code, notes, and snippets.

@heemayl
Last active November 5, 2015 08:48
Show Gist options
  • Save heemayl/7403e87a5b106cf9b3f7 to your computer and use it in GitHub Desktop.
Save heemayl/7403e87a5b106cf9b3f7 to your computer and use it in GitHub Desktop.
Basics of GNU Plot
#!/usr/bin/env gnuplot
## We are plotting a time-series data, Time in `HH:MM` is separated by a space with their respective Number, you can think of the number as the Number of Users of an Application. Here is a snippet of the input file "input.txt" (remove the hashes, of course):
#12:00 2345
#12:15 5084
#12:30 2490
#12:45 3490
#13:00 4567
#13:15 4240
#13:30 3210
###### Each command is followed by explanation. Note that this is a very basic set of commands for GNU Plot. ######
set title "Time vs Number of Users" font "calibri, 25"
##`set title` sets the title of the graph/image, `font` sets the font name and size. We are setting the `font` as "calibri" and font size as 25. We can also set only the font size keeping the font as default which is "arial" e.g. `set title font ", 20"`.
set xlabel "Time in HH:MM" font "calibri, 20"
##Label on X-axis and font
set ylabel "Number of Users" font "calibri, 20"
##Label on Y-axis and font
set term jpeg size 1200,800
##`set` is used to set options of GNUplot. `set term` indicates what type of output to generate. Here we are generating JPEG image having size of 1200x800 pixels. The default is 640x480.
set output "/tmp/out_image.jpg"
##By default the resultant image is shown on STDOUT, `set output` redirects the output to the given file, the filename must be enclosed in double quotes
set xdata time
##`set xdata` indicates the datatype used on the X-axis, `set xdata time` indicates the datatype is date/time.
set timefmt "%H:%M"
##Indicates the format of the X-axis data given in the input, In our case we have used `Hour:Minute` format. For example, if our input is in the form `2015-12-04 14:45:34` then we would use `set timefmt '%Y-%m-%d %H:%M:%S'`
## Things to note for the `tics` options:
## - A `tic` is a point of notation on the graph. Major tics are labelled tics i.e. `|` with labels e.g. on X-axis the time `01:30`, major tic-marks always refer to an input value. `set xtics` is used to manipulate major tic options on X-axis, similarly `ytics` is used for Y-axis.
## - There is another kind of tic-mark known as minor tics. These occur in between the major tics and are not associated with an input. These can be manipulated by `set mxtics` and `set mytics` for X-axis and Y-axis respectively.
set xtics format "%H:%M"
##The label format of the major tics on X-axis, the specifiers are similar to `set timefmt`
set xtics font ", 15"
##Sets font name and size for major tics on X-axis.
#set xtics rotate by 90
##Will rotate the major tics on X by 90 degrees counter clock-wise
set xtics "00:15"
##Range of intervals i.e. difference between two successive major tics on X-axis, it has another version which will set the start time and end time too having the format `set xtics "start", "interval", "end"` e.g. `set xtics "12:00", "00:30", "18:30"`. The end value can be omitted.
set grid xtics
##`set grid` will set grid on major tics (both X & Y), we can use `set grid xtics` to draw grids on `X` axis's majot tics only. Similar goes for `ytics` and minor tics `mxtics`, `mytics`.
unset mxtics
##This will unset minor tic-marks on the X-axis that occurs in between the major tics. To turn it on we can use `set mxtics`, this will sub-intervals with a frequency of 2-5 (Depending on input). We can set the number of sub-intervals for minor tics by `set mxtics <freq>` e.g. `set mxtics 2`, this will generate 2 sub-intervals i.e. 1 minor tic between 2 major tics.
#### Let's modify Y-axis parameters, on Y-axis we are plotting number of users i.e. positive integers so we don't have much to modify.
set ytics 500
set mytics 2
set ytics font ", 15"
set grid ytics mytics
set key off
##`set key` shows the options given to the `plot` command on the image, to turn it off we need `set key off`.
## Let's plot our data
plot "input.txt" using 1:2 with lines linewidth 10
##`plot` command is used to draw plots, `input.txt` is our input file having all the input data to be plotted. `using` option of `plot` tells which columns are to be plotted, `using 1:2` means plot column-1 and column-2 of input data where column-1 is matched against column-2. `with` defines the style we want to plot data as e.g. `with lines` will plot data with `lines` style. Some other styles are `impulses`, `dots`, `steps`, `points`, `labels`. We can also set other parameters like `linewidth`, `linecolor`, `linetype` of a style using appropriate options of `with`. For example to set a `linewidth` of 10 with the `lines` style we have used `with lines linewidth 10`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment