|
|
Subscribe / Log in / New account

New features in gnuplot 5.4

LWN.net needs you!

Without subscribers, LWN would simply not exist. Please consider signing up for a subscription and helping to keep LWN publishing

July 22, 2020

This article was contributed by Lee Phillips

Gnuplot 5.4 has been released, three years after the last major release of the free-software graphing program. In this article we will take a look at five major new capabilities in gnuplot. First, we briefly visit voxel plotting, for visualizing 3D data. Since this is a big subject and the most significant addition to the program, we'll save the details for a subsequent article. Next, we learn about plotting polygons in 3D, another completely new gnuplot feature. After that, we'll get caught up briefly in spider plots, using them to display some recent COVID-19 infection data. Then we'll see an example of how to use pixmaps, a new feature allowing for the embedding of pictures alongside curves or surfaces. Finally, we'll look at some more COVID-19 data using the new 3D bar chart.

A full accounting of all of the improvements and bug fixes in 5.4 can be found in the release notes. More gnuplot history can be found in our May 2017 article on the soon-to-be-released gnuplot version 5.2, which described its new features, some of which have been expanded in 5.4.

Gnuplot's staying power

Gnuplot is an early free-software success story. It was the first widely used open-source graphing utility, and became the tool of choice for people performing Fortran simulations of oceans and atomic bombs, for example. As a standalone, compiled C program, it endures as a workhorse of technical graphics in large part due to its speed and stability when confronted with enormous data sets.

Its usual means of control is through an interactive prompt at the terminal, or by executing scripts written in its scripting language. Gnuplot's primary niches are creating visualizations for scientific and other technical publications, and displaying live graphs of streamed data from sensors, simulations, or server statistics.

Gnuplot is language-independent, in contrast to packages implemented as libraries for a specific programming language. Some see the necessity to learn gnuplot's scripting language as a hindrance, but, in my experience, it is no more onerous than learning the de facto domain-specific language defined by the interface to a plotting library. Gnuplot can be controlled through a socket, and can plot data from a pipe or FIFO; this makes it simple to use with any programming language. To make this even more convenient, most popular languages have gnuplot interfaces; for example, Gaston for Julia or Gnuplot.py for Python.

A major factor in gnuplot's popularity with scientists is its interoperability with TeX and LaTeX, automating the creation of sophisticated documents with plots and text that share typefaces. Gnuplot is extensively customizable, and makes simple things easy while making complex things manageable, such as creating composite illustrations by aligning a set of graphs, or doing such arcane things as embedding a vector field in a surface (but you still can't make a pie chart—unless you get slightly creative). Finally, gnuplot can produce this output on a wide variety of output devices, or in a Jupyter notebook using the gnuplot kernel.

Voxel plots

The standout new feature arriving in gnuplot 5.4 is voxel plotting. The idea of an image, such as a photograph, being composed of a 2D array of pixels is familiar. Each pixel has an x and a y coordinate, and a color. The extension of the concept of the pixel to the third dimension is the voxel; in fact, the word is short for "volume pixel".

But what do voxels have to do with the plotting and visualization of functions and data?

Up to now, all of the plot types in gnuplot have handled functions of one or two variables. In the case of two variables, we had a choice of surface, contour, or image plots (sometimes called "heat maps"). The introduction of voxels allows us, for the first time, to visualize functions of three coordinates: x, y, and z. Of course, until gnuplot gets a holographic output terminal, we are still confined to the surface of screens or paper, so the final result will be a perspective rendering of some aspect of the 3D data or function.

Voxel data sets are familiar in medical imaging, where they are used to display the results of MRIs or CAT scans, and in engineering or physics, where they are helpful in understanding such things as the 3D flow pattern around a propeller. Gnuplot provides an assortment of techniques for plotting voxel data.

In a future article, we'll go into these techniques in depth, with a variety of examples. For now, we'll display one sample of a voxel plot, motivated by an example from physics: the potential field ("voltage") due to a dipole, which is two opposite charges fixed in place. This is a model for such things as a water molecule, when you are reasonably far away from it.

If the two charges are placed on the vertical (z) axis, they give rise to the 3D potential field visualized in the following figure.

[A dipole field]

In our followup article we'll see the various other ways in which we can visualize this same data. The complete script that created this plot can be found in the Appendix.

Polygons

The splot command gets a new ability in gnuplot 5.4: it can now plot sets of closed 2D polygons positioned in a 3D space. It can do so in two distinct ways, to produce different effects.

In the first way, a list of vertex positions, defining a set of polygons, is plotted with a new command:

    splot <$vertices> with polygons
This colors all of the polygons identically, so either transparency or lighting is required to create a usable rendering.

The second way is more versatile, because it allows different colors and opacities to be applied to each polygon. This method uses gnuplot's new polygon object type. Instead of a block of coordinates, we define a list of objects. These six polygon objects are arranged to form a box with one side tilted open:

    set style fill transparent solid 0.8
    set obj 1 polygon from 0,0,0 to 1,0,0 to 1,1,0 to 0,1,0 to 0,0,0\
	depthorder fillcolor "blue"
    set obj 2 polygon from 0,0,0 to 0,0,1 to 1,0,1 to 1,0,0 to 0,0,0\
	depthorder fillcolor "#AAAA00"
    set obj 3 polygon from 1,0,0 to 1,1,0 to 1,1,1 to 1,0,1 to 1,0,0\
	depthorder fillcolor "#33AAAA"
    set obj 4 polygon from 0,0,0 to 0,1,0 to 0,1,1 to 0,0,1 to 0,0,0\
	depthorder fillcolor "#CC0066"
    set obj 5 polygon from 0,1,0 to 0,1,1 to 1,1,1 to 1,1,0 to 0,1,0\
	depthorder fillcolor "#33FF66"
    set obj 6 polygon from 0,0,1 to 0,1,1 to 1,1,1.5 to 1,0,1.5 to 0,0,1\
	depthorder fillcolor "#AAAAAA"

The first line tells gnuplot to fill objects with a solid color of opacity 0.8, which is slightly transparent. In the subsequent set object commands, depthorder ensures that the parts of the polygon farther from the "eye" will be drawn before the closer parts, so that the rendering will look right. Each command also defines the vertices of a rectangle and its color.

After defining these objects, they will be drawn to accompany any splot command until they are undefined. One of gnuplot's quirks is that there is no way to just plot polygons nor any other objects directly. They are intended to be plotted along with a curve or surface. So in a case like this, where we really just want the polygons and have no surface to plot, we have to accommodate the program by using the splot command, but without actually drawing a surface. One way to do this is to plot a surface that exists entirely outside the range of the axes:

    splot -1

That will produce the picture below.

[A transparent box]

Spider plots

A spider plot (also known by several other names including radar chart) is another new graph type in gnuplot. Spider plots are intended to visualize multivariate data, so they have a handful of axes, each one representing a different variable. In this sense they are similar to parallel axis plots, which first became available in gnuplot 5.2, and were described in our previous article. The difference is that, in a spider plot, the axes all intersect at a common point, rather than being parallel. Although their use is controversial in some quarters, they can create interesting illustrations, and are an easy way to construct certain types of diagrams.

Those who have used parallel axis plots in gnuplot should be aware that this version changes the syntax; existing scripts will break, which is regrettable.

To illustrate the kind of thing that spider plots can be applied to, I downloaded some recent COVID-19 data and extracted the numbers for the confirmed case rates for six countries on June 12 and July 12, arranging the data like this:

    Italy, United States, Honduras, France, Canada, Switzerland
    3905.638, 6112.782, 774.286, 2383.218, 2583.822, 3577.396
    4016.203, 9811.656, 2784.865, 2615.946, 2843.902, 3779.832

Those numbers are the positive cases per one million people on the two different dates. Here is a complete script, for making the plot below (line numbers have been added to facilitate the explanation):

    (1) set title "COVID cases per million, 12Jun and 12Jul 2020\n" font "Times,16"
    (2) set spiderplot
    (3) set datafile separator comma
    (4) set for [p=1:6] paxis p range [0:10000]
    (5) set for [p=1:6] paxis p tics format ""
    (6) set paxis 4 tics 2000 font ",8" format "%g"
    (7) set style spiderplot fillstyle transparent solid 0.3 border\
            linewidth 1 pointtype 6 pointsize 1.2
    (8) set grid spider linetype -1 linecolor "grey" lw 1
    (9) plot for [i=1:6] "spidey.dat" using i title columnhead

Line (2) is required to make a spiderplot. Line (3) says that the data is separated by commas. The next two lines set up six axes, with their ranges and tics. We only want numbers on one of the axes, which we turn on with line (6). Line (7) causes the fillstyle of the polygons formed by the data to have a solid, transparent color, bounds them with a border, and asks for open circles (pointtype 6) with a pointsize of 1.2. Line (8) draws the grid, which is the set of grey lines that are somewhat evocative of a spiderweb; linetype -1 is a solid line. The last line draws the plot.

The looping construct (for [i=1:6]) is now required for parallel axis or spider plots. The file is stored on disk with the name "spidey.dat", and the loop combined with using i just means to loop through each of the six numbers on each line, creating a new polygon from each line. If we wanted to skip some numbers, we could alter the loop here. The words "title columnhead" say to take the first line of the file and use the words there for axis labels.

[spiderplot]

In the plot, the data for June 12 is shown in purplish, and the greenish area shows the data a month later. One can immediately see that four of the countries experienced a very small growth in the number of cases, whereas the US and Honduras show a much faster growth. Finally, the plot makes it clear that the confirmed case percentage is much larger in the US than in the other countries shown.

Pixmaps

A new concept in gnuplot 5.4, the pixmap, is an image object that is placed at a fixed location in a 2D or 3D plotting space. Pixmaps can be used for logos, backgrounds, or as illustrative labels attached to specific points on curves or surfaces.

Suppose a planetary scientist has a model of some property of planets in the solar system, described by the function f(), depending on two variables. A plot of f() would be a surface. It might be effective to label various locations on this surface with pictures of the planets that those locations correspond to. This is possible using pixmaps. If there is an icon of Saturn on disk, and if Saturn has the properties -7 and -9 for the two variables in the model, then this command will establish a pixmap object placing the Saturn image on the surface at the location for those two variables:

    set pixmap 2 "saturn.png" at -7, -9, f(-7, -9) width screen 0.06

The clause set pixmap 2 gives the index 2 to the object; this is used to unset or redefine it later if required. The words "width screen 0.06" set the width of the pixmap to be 0.06 of the total width of the graph; some trial and error was needed to find a good size here.

After all the desired pixmaps are defined using similar commands, using splot on the function draws the surface along with all of the planets. The complete script that produces the following graph is in the Appendix.

[Planetary pixmap]

3D bar charts

Gnuplot has had bar charts for a while. The new release takes these into the third dimension. Now you can set the boxdepth as well as the boxwidth of your columns of data. The commands for 3D bar charts, or box plots, as they are known in gnuplot, are a pretty straightforward extension of the 2D version. To plot 2D histograms or bar charts, the plot command takes a series of horizontal coordinates and values, and the clause with boxes. To make a 3D bar chart, we use the 3D version of plot, splot, which originally meant "surface plot", supplying x, y, and a value for each bar.

This can be a good visualization of discrete data that depends on two variables. As an example, we'll take the COVID-19 data that we used for our spider plot and add a few more months, to look at everything from near the beginning of the pandemic in March until close to the present. Here's the data, extracted from the same source as before:

    Italy, United States, Honduras, France, Canada, Switzerland
    2.729, 34.945, 0.202, 206.114, 74.18, 3.964, "3-12"
    617.373, 1436.877, 39.679, 2518.465, 2867.833, 1601.048, "4-12"
    1854.187, 2137.452, 202.532, 3635.583, 3496.515, 4072.221, "5-12"
    3905.638, 6112.782, 774.286, 2383.218, 2583.822, 3577.396, "6-12"
    4016.203, 9811.656, 2784.865, 2615.946, 2843.902, 3779.832, "7-12"

Now each row has a label for the date appended, which we can use to create tic labels. The complete script that produces the following plot is in the Appendix. We have arranged things so that each country gets its own color, and the heights of the bars indicate the confirmed case rate for each country and month.

[3D bar chart]

Many improvements

Here we've only covered the major new features, but there are many additional improvements in 5.4, mentioned in the release notes and the official manual [2.1MB PDF]. The extensive interactive help available within gnuplot has also been thoroughly revised in the new version to cover all the new features. One of the new enhancements is automatic use of 64-bit integer arithmetic when running on a system that supports it. Most of gnuplot's internal calculations are done in floating point, but when using 32-bit integers, some operations, such as factorials or exponentiation, would return floats even when supplied with integer arguments if the results would overflow 32-bit integers. Now those functions return integer results when given integer arguments. Other new features are the ability to project graphs onto any of the coordinate planes, more ways to render surfaces and contour lines, improved array syntax, support for the LaTeX pict2e picture environment, and more Bessel functions, which is always nice.

Gnuplot is not everyone's cup of tea. But because of its combination of power, scriptability, and flexibility, it occupies a unique niche in the world of technical and scientific graphics. Those who become conversant with its sometimes quirky ways are regularly rewarded by a relentless process of improvement that stretches back to the 1980s and shows no signs of slowing down.


Index entries for this article
GuestArticlesPhillips, Lee


(Log in to post comments)

New features in gnuplot 5.4

Posted Jul 23, 2020 10:58 UTC (Thu) by LtWorf (subscriber, #124958) [Link]

I certainly found gnuplot's language and results easier to use than matplotlib when I was writing a thesis and a related paper.

But I have not touched either since then, ~7 years ago.

New features in gnuplot 5.4

Posted Jul 23, 2020 13:43 UTC (Thu) by benjamir (guest, #133607) [Link]

(I don't want to shoot the messanger) Seems to enable a lot of "graph porn". Most 3D plots are barely readable. Bars are not comparable. But there's probably a lot of demand for that anyway... from all that I would take only the spider. Voxels seem /cool/, but can I rotate that ;-)

I got something from from the the first two and the last paragraph -- the boring stuff. Very informative, thank you!

New features in gnuplot 5.4

Posted Jul 25, 2020 14:36 UTC (Sat) by anton (subscriber, #25547) [Link]

I tend to agree. For example, instead of the 3D bar chart, a 2D line chart would probably have presented the same data more clearly. On the positive side, Gnuplot does not give its users pie charts.

New features in gnuplot 5.4

Posted Jul 23, 2020 23:09 UTC (Thu) by gus3 (guest, #61103) [Link]

The spider graph is handy when compass directions constitute an axis of the data. A couple years ago, I saw one on an "about our library" pamphlet, illustrating which part of our town (or county) the patrons came from. It was a simple north/south/east/west graph, but the visual was perfectly clear.

New features in gnuplot 5.4

Posted Jul 23, 2020 23:42 UTC (Thu) by leephillips (subscriber, #100450) [Link]

Exactly; they make the most sense when the circular arrangement of axes actually means something. There are a couple of nice examples here:

http://www.thefunctionalart.com/2012/11/radar-graphs-avoi...

New features in gnuplot 5.4

Posted Jul 24, 2020 19:52 UTC (Fri) by lobachevsky (subscriber, #121871) [Link]

gnuplot.py looks abandoned, at least on my cursory look, but there is support for Gnuplot in Jupyter notebooks via a Gnuplot kernel. This might be a more timely reference.

New features in gnuplot 5.4

Posted Jul 24, 2020 20:37 UTC (Fri) by leephillips (subscriber, #100450) [Link]

gnuplot.py still works, including on Python 3: https://github.com/keszybz/gnuplot-py (but note also that interface libraries are not necesary to use gnuplot from any language).

We did link to the Jupyer gnuplot kernel.


Copyright © 2020, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds