How to plot to multiple windows in Gnuplot

On my Ubuntu, Gnuplot plots into a Qt window (terminal type qt) by default. Every invocation of the plot command redraws the contents of this single Qt window.

Sometimes, I need to plot two different data sets in two different windows, so that I can compare them side-by-side. To do this specify a window number, an integer starting from 0.

For example:

set terminal qt 0
plot("data_0.txt")

set terminal qt 1
plot("data_1.txt")

Tried with: Gnuplot 5.0 and Ubuntu 14.04

Advertisements

How to output to PDF in Gnuplot

Gnuplot is a great tool for quickly plotting data from a file and viewing it in a window:

plot("sales_data.txt")

To write or print this plot to a PDF file, set the pdf terminal, specify the output filename and plot again:

set terminal pdf
set output "sales_data.pdf"
plot("sales_data.txt")

Tried with: Gnuplot 5.0 and Ubuntu 14.04

How to plot points in color in Gnuplot

Points plotted with color in Gnuplot
Points plotted with color in Gnuplot

I had some 2D data in a file in the following format:

6.37026 2.42441 0
4.16001 6.55597 2
3.54946 7.15539 2
4.35229 3.8448 1
7.12706 1.2563 3
1.98478 8.68739 0
7.67213 2.22039 0
3.85744 7.3549 1

Each data point is on a separate line. The first and second columns were the X and Y coordinates of the data. The third column represents that which of the 4 classes (0-3) that points belonged to.

To understand the data better, I wanted to visualize it by drawing a 2D plot with a point for each data point. To distinguish the points belonging to different classes, I wanted points of each class to be drawn with a distinct color.

Once your data is in this format, achieving the above is easy in Gnuplot:

plot("sales_data.txt") with points palette

Tried with: Gnuplot 5.0 and Ubuntu 14.04

How to use Caca terminal of Gnuplot

Plot drawn using caca terminal of GNUPlot
Plot drawn using caca terminal of GNUPlot

Gnuplot can plot in ASCII at the terminal if the dumb terminal is chosen. However, this does not fully utilize the Unicode characters and color support of modern terminals. The Caca terminal of Gnuplot uses all the character and color capabilities of your terminal to generate beautiful and colorful plots. It does this by using the Caca library which provides these capabilities.

Build or Install

Gnuplot can be installed easily:

$ sudo apt install gnuplot

Note that support for caca terminal was added in Gnuplot 4.7. If you have that version and it was built with support for caca, then the commands in the next section should work directly.

If you do not have 4.7 and later and if it was not built with caca support, you need to build Gnuplot from source. Here are the steps I followed to build it from source:

  • Download Gnuplot source code from here and unzip it to a directory.

  • Install the Caca library if you do not have it already:

$ sudo apt install libcaca-dev
  • While building Gnuplot, I got an error in source files that use WxWidgets. To fix that, I opened the configure file, looked for the WX_CXXFLAGS option and appended -std=c++11 to its string.

  • While building Gnuplot, I got an error about linking with a X11 library. To fix that, I opened the configure file, looked for the WX_LIBS option and appended -lX11 to its string.

  • We are now ready to configure and build. Remember to configure with support for caca, since it is still an experimental feature and not enabled by default:

$ ./configure --with-caca
$ make
  • If the build is successful, you can install Gnuplot:
$ sudo make install

Usage

  • To use the caca terminal in Gnuplot:
gnuplot> set terminal caca

You can now plot anything and Caca draws it using ASCII/Unicode characters and colors.

  • You will notice that Caca opens a X window to draw the plot. If you would like it draw the plot in the terminal itself, then you need request caca to use a driver that can draw at the terminal.

  • You can view the list of drivers supported by caca:

gnuplot> set terminal caca driver list
  • Of these, the ncurses and slang drivers can draw to terminal. I found that using slang draws in color, but it shows lots of question mark symbols. So, I use ncurses:
gnuplot> set terminal caca driver ncurses

Now plot anything and view it in beautiful ASCII/Unicode and colors at the terminal!

  • You can find an extensive list of plots drawn using Caca here.

Tried with: Gnuplot 5.0.0, Caca 0.99.beta18 and Ubuntu 14.04

Gnuplot: Plotting a 3D Triangulation

Gnuplot can be used to plot or write to file existing 3D triangulations. Though its output is not fancy or beautiful, it can be a quick tool to check results. Here is how to render:

  1. A 3D triangulation or 3D surface mesh consists of tetrahedrons or triangles. Write the triangulation as a list of line segments to a text file. If the triangulation consists of tetrahedrons, then these could be the edges of the tetrahedrons. The line segments file should be written in the following format:
    3 4 9
    6 9 1
    
    10 45 78
    23 67 90
    

    Note that the 3D coordinates of each of the two vertices of a line segment should lie on separate lines. Also, each pair of point coordinates is separated from the next by 2 newlines. This is the format required by Gnuplot.

  2. Open Gnuplot, plot the segments file using points, lines or linespoints using a surface plot:
    gnuplot> splot "Segments.txt" with lines
    

How to plot at terminal using GNUPlot

Gnuplot is an awesome graph plotting program that has been around since the time of dot-matrix printers and teletype terminals. Little wonder that it has support for such archaic output devices even today. One such output terminal supported by Gnuplot is called the dumb terminal.

I find the dumb terminal pretty useful to have around in the initial stages of plotting to get a rough idea of the data. The default terminal on Windows is called wxt and gnuplot pops up a new window to display the plot. If you find that and switching between windows distracting, use the dumb terminal which plots a quaint ASCII-art graph! 🙂

To switch to a dumb terminal:

gnuplot> set terminal dumb

Reference: Page 220 from the book Gnuplot in Action