Let's make a bar graph with gnuplot

Create a bar graph using gnuplot and save it as an image file. The goal is to create the graph below.

Install gnuplot

You can easily install it on your Mac using Homebrew.

$brew install gnuplot

Create a bar graph

Prepare the data

I saved it with the file name sample-box.dat.

1 "2016" 9500
2 "2017" 10800
3 "2018" 11800
4 "2019" 12100
5 "2020" 16600

Display graph

Start gnuplot and display the graph.

If sample-box.dat is not in the current directory, specify the file path.

$ gnuplot 

	G N U P L O T
	Version 5.4 patchlevel 0    last modified 2020-07-13 

	Copyright (C) 1986-1993, 1998, 2004, 2007-2020
	Thomas Williams, Colin Kelley and many others

	gnuplot home:     http://www.gnuplot.info
	faq, bugs, etc:   type "help FAQ"
	immediate help:   type "help"  (plot window: hit 'h')

Terminal type is now 'qt'
gnuplot> 
gnuplot> plot 'sample-box.dat' using 1:3:xtic(2) with boxes
gnuplot>

Did you see a bar chart?

I think that the x-axis is displayed from "2016" to "2020", and the y-axis is the value in the third column of sample-box.dat to create a bar graph.

Save as an image

Specify the output destination with set terminal and set output and plot.

gnuplot> set terminal png font "Sans,9"

Terminal type is now 'png'
Options are 'nocrop enhanced size 640,480 font "Sans,9.0" '
gnuplot> set output 'test-boxes.png'
gnuplot> 
gnuplot> plot 'sample-box.dat' using 1:3:xtic(2) with boxes
gnuplot>

Was the graph saved as a test-boxes.png file?

Select a font that can display Japanese in your environment.

Simplify the output of bar graphs

Save the following file as png_plot-boxes.txt.

I also tried to specify the format of the graph.

set terminal png font "Sans,9"
set output 'test-boxes.png'
set boxwidth 0.5
set style fill solid
unset key
set title 'year sell'
set ylabel '(yen)'
set format "%'.0f"
set decimalsign locale; set decimalsign "."

plot [][0:] 'sample-box.dat' using 1:3:xtic(2) with boxes</pre>

You can execute the commands in the file all at once by executing the following.
<pre>
$gnuplot png_plot-boxes.txt

You can get the same result by starting gnuplot and using the load command.

gnuplot> load'png_plot-boxes.txt'

The following image is saved.

Associated Information