Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Overview

Gnuplot is a versatile and proficient plotting tool used for the visualization of data. However, when working with data files, we often come across situations in which the first line contains headers or metadata that we shouldn’t plot.

In this tutorial, we’ll go over various methods for ignoring the first line of a data file when plotting with Gnuplot.

2. Using skip

The skip option in Gnuplot is a built-in feature that allows us to ignore a given number of lines at the beginning of a data file.

The skip option allows us to specify how many lines Gnuplot should disregard before reading and plotting the actual data. Moreover, using the skip option avoids the need for further preprocessing, resulting in a straightforward and fast solution:

gnuplot> plot 'data.txt' skip 1 using 1:2 with lines

In this example, skip 1 tells Gnuplot to skip the first line of the data.txt file before choosing the data. This is useful when we know that our data file has a header row that shouldn’t be displayed in the graphic. The 1:2 portion instructs Gnuplot which columns to plot in. In this case, the first and second columns are used to represent the x and y axes in a two-dimensional plot. Also, lines indicates that the data should be plotted as a line graph.

Finally, employing the skip option is one of the simplest and most direct ways to deal with data files containing headers or irrelevant lines. Meanwhile, selecting the number of lines to skip ensures Gnuplot starts reading from the correct data line, thus allowing us to build our plots without having to edit the file externally.

3. Using sed

The sed command is another effective way to ignore the first line in a data file when using Gnuplot. sed, short for stream editor, is a versatile text manipulation tool in Linux operating systems. We use it to perform various tasks, including deleting lines from a file, which is exactly what we need in order to exclude the initial line of data.

When using Gnuplot, we can pipe the output of a sed command into it, allowing us to delete irrelevant lines before they reach our graphing engine:

$ sed '1d' data.txt | gnuplot -p -e "plot '-' using 1:2 with lines"

In this example, sed ‘1d’ data.txt instructs sed to delete the first line of the data.txt file. The crucial option here is 1d, which instructs sed to delete line 1. As a result, the data sent to Gnuplot no longer includes the first line, effectively skipping it. The pipe symbol | then transmits the sed command’s output to Gnuplot, where it’s used as plotting input. Moreover, the symbol instructs Gnuplot to read data from standard input, which is now the contents of data.txt, minus the first line.

Using sed with Gnuplot is a powerful way to ignore the first line of a data file. By leveraging sed’s text manipulation and piping its output into Gnuplot, we can skip unwanted lines without altering the original file.

4. Using head

When using Gnuplot, we can also exclude the first line of a data file with the head command. The head command is a basic but powerful tool that allows us to print the first part of a file. However, we can also use it to skip the first few lines and show the rest of the file. This is especially handy for omitting headers or metadata that may appear at the top of a data file.

The head command, by default, displays the first ten lines of a file, but when combined with the -n argument, we can specify the number of lines to display:

$ head -n +2 data.txt | gnuplot -p -e "plot '-' using 1:2 with lines"

In this example, the head -n +2 data.txt statement instructs head to output the contents of data.txt beginning with the second line. The +2 syntax specifies that head should begin printing the file on the second line, thus bypassing the first. The pipe | symbol then transmits the filtered output to Gnuplot, which reads and plots the data.

Furthermore, the gnuplot -p -e “plot ‘-‘ using 1:2 with lines” command instructs Gnuplot to handle the input. In addition, the -p flag allows the interactive plot window to remain open after the plot has been drawn. The using 1:2 portion specifies that Gnuplot plots the first and second columns of the data. Thus, with lines visualizes the data as a line graph.

5. Managing Data in Gnuplot Scripts

We can also alter data directly within a Gnuplot script. This approach provides flexibility and control, especially for automating the plotting process or using a single Gnuplot script. By using Gnuplot’s built-in functionality, we can skip particular lines of the data file, thereby eliminating the need to preprocess it externally.

Gnuplot can handle data in various ways, including using the every modifier to skip the first line:

gnuplot> set xrange [0:4]
gnuplot> set yrange [0:40]
gnuplot> plot 'data.txt' every 1::1 using 1:2 with lines

In this example, the set xrange [0:4] and set yrange [0:40] commands specify the range of values for the x and y axes. The plot ‘data.txt’ every 1::1 using 1:2 with lines statement skips the file’s first line by using the every modifier. Moreover, every 1::1 indicates that Gnuplot should begin processing on the second line, effectively ignoring the header.

In additon, the using 1:2 portion tells Gnuplot to plot the first column as the x-axis and the second column as the y-axis. However, the with lines option connects the data with lines.

Finally, by combining these options, we can skip unimportant lines in the Gnuplot script without using additional preprocessing.

6. Conclusion

In this article, we looked at several ways to ignore the first line of a data file while using Gnuplot. The built-in skip option is a straightforward and direct solution, while programs like sed and head provide strong preprocessing options. Meanwhile, Gnuplot scripts with the index option enable us to easily manage data within the plotting environment.

Besides that, each technique has different advantages, ranging from more simplicity to more versatility. By picking the appropriate method depending on our use case, we can streamline data visualization and ensure professional plots without modifying the original data file.

Finally, whether we’re working with headers, metadata, or other unused lines. These strategies allow us to properly address common data preparation difficulties in Gnuplot.