Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » GNUPlot

Timeline Plots with GNUPlot

Submitted by on April 13, 2006 – 10:52 am 8 Comments

GNUPlot is an open-source versitile data plotting and mathematical charting application. Timeline plot is the most common type of a plot you would generate. It also seems that it’s one of the more capricious features of GNUPlot. Here are a few simple examples to get you over some problems that otherwise may not be obvious.

The most common date/time format used by most databases and applications looks something like this: 2006-04-13 11:42:12; or in general form: YYYY-MM-DD hh:mm:ss. Below is a generic data sample where the first column is date/time, while the second, third and fourth columns represent three different types of data. Each value in columns 2, 3, and 4 is, therefore, a function of time. Let’s also suppose that this data file is /tmp/data1.dat.

Create a new text file and call it, say, /tmp/plot1.gnu.

set title ‘Title of Your Plot’
set xdata time
set key box
set key bottom right
set size 1.5,1.5
set xlabel ‘Time’
set ylabel ‘Data’ font ‘Arial,12′
set autoscale
set timefmt “%Y-%m-%d %H:%M:%S”
set term png color
set output ‘/tmp/plot1.png’
plot 
‘/tmp/data1.dat’ using 1:3 title ‘Data 1’ with linespoints, 
‘/tmp/data1.dat’ using 1:4 title ‘Data 1’ with linespoints, 
‘/tmp/data1.dat’ using 1:5 title ‘Data 1’ with linespoints

And now to plot it:

gnuplot  /dev/null

If you paid attention, you should now have a /tmp/plot1.png file that shows a plot of your three data types as a function of time.

Sample GNUPlot timeline plot with three data types

Let’s review the options in the /tmp/plot1.gnu file line by line.

set title ‘Title of Your Plot’
This is simply the title of your plot. It will appear at the top of the resulting image.

set xdata time
You need to tell GNUPlot that you desire a timeline type of a plot.

set key box
GNUPlot will create a little box with the plot key. It’s useful for knowing which line represents what data.

set key bottom right
And, of course, you want to tell GNUPlot to place the key in an appropriate location so it doesn’t clash with the rest of the plot.

set size 1.5,1.5
The desired size of the resulting plot image in mysterious units.

set xlabel ‘Time’
X-Axis label

set ylabel ‘Data’ font ‘Arial,12′
Y-Axis label and font type/size. You can also specify font type and size for the X-axis.

set autoscale
Unless you want to plot only a specific chunk of data (say, from last Monday until today), you should tell GNUPlot to automatically scale the plot to include all available data.

set timefmt “%Y-%m-%d %H:%M:%S”
Now, this is important, so pay attention. Here you tell GNUPlot about the format of your date/time field. Because date and time are usually separated with a space, you need to include the whole thing in quotes.

set term png color
Here you express your desire for a color plot, unless you are into black-and-white.

set output ‘/tmp/plot1.png’
This is where GNUPlot will store the resulting image.


plot
‘/tmp/data1.dat’ using 1:3 title ‘Data 1’ with linespoints,
‘/tmp/data1.dat’ using 1:4 title ‘Data 1’ with linespoints,
‘/tmp/data1.dat’ using 1:5 title ‘Data 1’ with linespoints

OK, so here is the most important part. The “using 1:something” part tells GNUPlot that your X-Axis value (date and time in this case) is in the first column and your data is in clumns 3, 4, and 5. And that you would like to plot all three data types as separate lines that would also show data points (the linespoints option).

But shouldn’t it be “using 1:2”, “using 1:3”, and “using 1:4” instead, since your data contains only four columns: date/time, data1, data2, and data3? No it shouldn’t. Keep in mind that your date/time column is in fact two columns: date and time are separated by a space. So there you go.

Print Friendly, PDF & Email

8 Comments »

  • Maggie says:

    I am getting an mylabel error while using sprintf statement in gnuplot which includes string and float… Ie., mylabel(s,v)=sprintf(“%-10s = %6.3f”,s,v).. if you can please explain this command line… Thank you

  • Andrew S says:

    I am writing a c program, and I need to plot my results. I know I can print results to file, and then copy them in qti plot or origin, but I need to plot a lot of stuff, and when I see a plot, if I see some error, I change something in program and then I need to plot that again… You get the point… If I would print to file, and then copy/paste it would just take too much time.
    I also know that I can use some program like IDL or MATLAB, but I need a c program.
    I read on the internet that I can plot my graph in gnuplot from c, but I don’t know how. I also saw some c code that does that, but it doesn’t open gnuplot. I guess I need to do something before I run my program, like download something or set something that will link c and gnuplot… By the way, I am using linux (ubuntu).
    And please, don’t answer my question with “You have that on internet”, “It’s easy”, or that sort of stuff. Also, please write everything that I should do, I am not really that good with computers.

  • The Inc says:

    I’ve been struggling with the installation for a long time. Can somebody please tell me the procedure for the installation? If GNUPLOT doesn’t work please suggest some other software which can plot graphs using data files.
    The installation seems to be fine. But when i say “gnuplot ” in the terminal or in X11, i get the following:

    dyld: Library not loaded: /usr/local/lib/libaquaterm.1.0.0.dylib
    Referenced from: /usr/local/bin/gnuplot
    Reason: image not found
    Trace/BPT trap
    Finally figured it out……

    It does take a long time… but seems to be running fine.

    Source:
    http://gavmacprogramming.wordpress.com/2007/05/02/installing-gnuplot/

  • wwwavid360gamercom says:

    I need to extract column names and their corresponding data types from ~200 tables on a SQL database. I could do this by hand by using MS Enterprise Manager, but a script similar to the functionality of this script would be helpful.

    select name from sysobjects where type = ‘U’

  • RichT says:

    When I run my program, and try to make my graph, it gives me this:

    gnuplot> plot “winds.txt” using 2:1 title “U” with lines, “winds.txt” using 3:1 title “V” with lines
    ^
    can’t read data file “winds.txt”
    “plot_winds.gnu”, line 22: util.c: No such file or directory

    I don’t know what I am doing wrong, nor how to make it work.

    This is my code:

    // include statements
    #include
    #include

    // declare functions
    float convert_temp(float temp_tenth);

    // main program
    int main()
    {
    // declare variables
    float skip; // skip unused variables from sounding
    float height, temp_tenth, temp, wind_dir, wind_spd;
    float U, V;

    int i; // counter for loop
    const int header_length = 4; // header length is constant
    char buff[256]; // array of characters to hold line of input file

    FILE *input, *temp_out;

    // open files
    input = fopen(“BIS2011012012.txt”,”r”);
    temp_out = fopen(“temperature.txt”,”w”);

    // skip file header
    for (i=0; i<header_length; i++)
    fgets(buff,256,input);

    while(fgets(buff,256,input)) // while not end of file
    {
    // read height (m) and temperature (C*10) from sounding
    sscanf(buff,"%f %f %f %f %f %f %f",&skip,&skip,&height,&temp_tenth,&skip,&wind_dir,&wind_spd);

    // convert temperature
    if (temp_tenth!=99999){
    temp = convert_temp(temp_tenth);

    // write variables to output file
    if (temp<10000){
    fprintf(temp_out,"%8.2f %8.2fn",height,temp);
    }
    }
    }
    //convert wind speed and direction and print to text file
    {
    U==-1*wind_spd*sin((wind_dir*M_PI)/180);
    V==-1*wind_spd*cos((wind_dir*M_PI)/180);
    FILE *outfile = fopen("winds.txt", "w");
    if (outfile)
    {
    fprintf(outfile, "%d,%d", U, V);
    }
    }

    }

    float convert_temp(float temp_tenth)
    {
    // declare local variables
    float temp;

    temp = temp_tenth/10.0;

    return(temp);

    }
    The temperature sounding data worked just fine; it's the wind data that I am having issues with.
    I feel like it's something wrong with my code, but the error makes it sound like I am in the wrong directory… I don't know.

  • Mathew says:

    I have a surface z=2-(x^2)/2 and two planes y=x and y=0. I wish I could visualize them, so that my students could see the intersection of those plane in 3 dimensional space. Please help….

  • Sonny says:

    The 4.4 version from Sourceforge gives no insallation help.

  • Matthew S says:

    I want a windows executable of gnuplot software in english .
    I want to run it on a windows platform.

Leave a Reply

%d bloggers like this: