Gnuplot Candlestick Chart Example

A candlestick chart summarizes price movement over a series of periods, showing for each period where the price opened, where it closed, and how far it ranged in between.

$DATA << EOD
1  100  97  104  103
2  103  101  108  106
3  106  100  107  101
4  101  99   105  104
5  104  102  110  109
6  109  105  111  106
7  106  104  112  111
8  111  108  115  113
EOD
set title "Candlestick Chart (Sample OHLC Data)"
set xlabel "Trading day"
set ylabel "Price"
set boxwidth 0.6
set xrange [0:9]
set grid y
plot $DATA using 1:2:3:4:5 with candlesticks linecolor rgb "#333333" notitle

How the script works

The datablock holds one row per trading day in the order gnuplot’s candlesticks style expects: day, open, low, high, close. Each candle’s box spans from open to close, and its thin whisker spans from low to high.

using 1:2:3:4:5 passes exactly those five columns, in that order, to with candlesticks. Getting this order right is the whole trick of the style: swap low and high and every candle renders incorrectly.

set boxwidth 0.6 sets each candle to 0.6 of a day wide, leaving a visible gap between candles, and set xrange [0:9] adds half a day of margin on both sides of the eight data points. A rising day (close above open) and a falling day are drawn the same way here; the box endpoints tell you which is which.

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

Variant to try

To color rising and falling candles differently, plot the data twice with a filter: once with using 1:2:3:4:5:($5 >= $2 ? 1 : 1/0) style logic is fragile, so the common approach is two filtered passes with an inline condition on the data, or pre-splitting the file into up-days and down-days and plotting each with its own linecolor. A simpler variant is the financebars style, which draws open/close ticks instead of boxes using the same five columns.

Related tutorials: Axis Ranges, Tics and Log Scales, 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 *