Gnuplot Scatter Plot Example

A scatter plot shows the relationship between two measured variables, one point per observation — this example adds a third and fourth variable by letting each point take its size and its color from extra data columns.

$DATA << EOD
1.0  2.1  1.0  1
2.0  3.4  2.0  2
3.0  3.1  1.5  1
4.0  5.2  3.0  3
5.0  4.8  2.5  2
6.0  6.9  4.0  3
7.0  6.1  3.5  2
8.0  8.4  5.0  4
9.0  7.9  4.5  3
10.0 9.6  6.0  4
EOD
set title "Scatter Plot with Variable Size and Color"
set xlabel "x"
set ylabel "y"
set grid
set colorbox
plot $DATA using 1:2:3:4 with points pointtype 7 pointsize variable linecolor variable title "Observations"

How the script works

$DATA << EOD ... EOD defines an inline datablock: the ten lines between the markers become a virtual data file, so the example needs no external file. Columns are x, y, a size value, and a color value, in that order.

The plot line reads four columns with using 1:2:3:4. The first two are the point position. pointsize variable tells gnuplot to take each marker’s size from the next using column (column 3), and linecolor variable takes each marker’s color from column 4, mapped through the current palette. pointtype 7 is the filled circle on most terminals, and set colorbox shows the color scale legend.

Because size and color come from data, the same script works unchanged when you replace the ten sample rows with your own four-column measurements.

Run it online: Paste this script into the online terminal and run it — no install needed.

Variant to try

For fixed-size points in a single color, drop the extra columns: plot $DATA using 1:2 with points pointtype 7 pointsize 1.5 linecolor rgb "#5e2d79" notitle. To color by category instead of a continuous value, keep the fourth column as a small integer group id and add set palette defined (1 "#e41a1c", 2 "#377eb8", 3 "#4daf4a", 4 "#984ea3") before the plot.

Related tutorials: Line Styles, Colors and Point Types, Plot CSV Data in Gnuplot. See more copy-and-run scripts in the Gnuplot Examples index.

Scripts on this page are adapted from the official gnuplot 6.0 demo collection (gnuplot.info) and rewritten for this tutorial.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *