I would like to make a histogram based on a series of data that I have in a .txt file. The first ten data of this file are:
Albania 1970
Antigua and Barbuda 11
Argentina 82219
Armenia 1905
Aruba 101
Australia 70111
Austria 50073
Azerbaijan 1656
Bahamas 93
Bahrain 463
However, I don't quite understand how GNUplot is used and am having errors running a script I've made. The script has the following structure:
#!/bin/bash
set title "Título"
set xlabel "X"
set ylabel "Y"
set style data histogram
plot 'fichero.txt'
When executing the script, the error I get is the following:
gnuplot ./grafico.plot
plot 'fichero.txt'
^
"./grafico.plot" line 5: Bad data on line 1 of file fichero.txt
Why do I get an error in the file data? I don't have much experience with gnuplot and I don't understand what could be wrong, so any help is appreciated.
Greetings.
One of the problems is that the names of some of the countries contain spaces. Those names must be protected by quotes, so your file should contain:
Another problem is that, by default, gnuplot expects the first column to contain numbers (which will be used as the x-coordinates of the points to paint) and the second column to contain data (which would be the height of the bars to paint). But your file is not folded to the expected structure and hence the error you get.
Luckily gnuplot is very flexible. To the command
plot
you can specify which columns of the file you want to use as data and which ones as "labels" of the ticks of the axis. In your case the specification would beusing 2:xtic(1)
that it means to use column 2 as data (Y-axis), and column 1 as labels for the x-axis ticks.On the other hand, the names of the countries are very long and overlap with each other, so that they can be read, it is convenient to put them rotated -90 degrees. This also requires expanding the lower margin of the figure to fit integers.
All this is done by the following script:
With the following result: