gnuplot

gnuplot place a label to a relative location in plot


I have a script that collects internet speed up/down data for a plot. The same script also computes running averages of the last 24hrs. Those numbers are handed into the gnuplot script and supposed to be placed into the plot. Both X and Y axis are dynamic and I need to place the label at a percentage of the axis.

Desired example (pseudo code) :

set label sprintf("Dwonload 24hr avg.: %6s Mb/sec",dw) at (90% x-axis,20% y-axis)
set label sprintf("Upload   24hr avg.: %6s Mb/sec",up) at (90% x-axis,10% y-axis)

Launching gnuplot from bash script (dw and up populated by script):

gnuplot -e "dw='$dw'" -e "up='$up'" speed.gpl

Gnuplot script:

#! /usr/bin/gnuplot
set xdata time
set timefmt "%Y-%m-%dT%H:%M"
set format x "%m/%d %H:%M"
set ytics format "%.0s%c"
set xtics rotate by 60 right
set grid xtics
set grid ytics
set label sprintf("Upload   24hr avg.: %6s Mb/sec",up) at  0,0
set label sprintf("Download 24hr avg.: %6s Mb/sec",dw) at  0,
set terminal pngcairo transparent size 1000,750
set output 'speed.png'
plot 'speed_1wk.dat' using 4:7 with linespoints title 'Download', \
     'speed_1wk.dat' using 4:8 with linespoints title 'upload'
set output
set terminal x11

Note the X axis is in time format. How will that impact the x-coordinate of the labels?

My output does not show the labels.

current output

Is there another method in gnuplot to place text than set label ??


Solution

  • Gnuplot uses several different coordinate systems.

    You can mix the coordinate systems freely, but you have to put the appropriate keyword in front of the coordinate value. To put your both your labels above the graph you might for example use the commands below. Note that the keyword "left" tells gnuplot to left justify the text (i.e. 1st character of the text is at that position). Give each label a number so that you can refer to it in multiple commands.

    set tmargin at screen 0.9    # reserve the top 1/10 of the screen as blank
                                 # The top border of the plot will lie exactly at screen 0.9
    set label 1 sprintf("Upload   24hr avg.: %6s Mb/sec",up)
    set label 1 left at  graph 0.0, screen 0.950
    set label 2 sprintf("Download 24hr avg.: %6s Mb/sec",dw)
    set label 2 left at  graph 0.0, screen 0.925
    

    Since the labels are positioned without regard to the units or ranges used in the plot, it doesn't matter if you use time units on x or log scale on y, or change the range of the plot, or whatever.