113

How do you plot bar charts in gnuplot with text labels?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
tatwright
  • 37,567
  • 6
  • 21
  • 9

5 Answers5

136

Simple bar graph:

bar graph

set boxwidth 0.5
set style fill solid
plot "data.dat" using 1:3:xtic(2) with boxes

data.dat:

0 label       100
1 label2      450
2 "bar label" 75

If you want to style your bars differently, you can do something like:

multi color bar graph

set style line 1 lc rgb "red"
set style line 2 lc rgb "blue"

set style fill solid
set boxwidth 0.5

plot "data.dat" every ::0::0 using 1:3:xtic(2) with boxes ls 1, \
     "data.dat" every ::1::2 using 1:3:xtic(2) with boxes ls 2

If you want to do multiple bars for each entry:

data.dat:

0     5
0.5   6


1.5   3
2     7


3     8
3.5   1

gnuplot:

set xtics ("label" 0.25, "label2" 1.75, "bar label" 3.25,)

set boxwidth 0.5
set style fill solid

plot 'data.dat' every 2    using 1:2 with boxes ls 1,\
     'data.dat' every 2::1 using 1:2 with boxes ls 2

barchart_multi

If you want to be tricky and use some neat gnuplot tricks:

Gnuplot has psuedo-columns that can be used as the index to color:

plot 'data.dat' using 1:2:0 with boxes lc variable

barchart_multi2

Further you can use a function to pick the colors you want:

mycolor(x) = ((x*11244898) + 2851770)
plot 'data.dat' using 1:2:(mycolor($0)) with boxes lc rgb variable

barchart_multi3

Note: you will have to add a couple other basic commands to get the same effect as the sample images.

Brad
  • 5,845
  • 7
  • 30
  • 29
  • For many applications it's more convenient to use the `histogram` plotting style, especially for grouping and stacking values. – Christoph Feb 01 '15 at 14:18
  • @Christoph Hi,If I want to add key for this type of graph? what should I do ? For example If I want to add a key which say the blue line is "winter" and the red line is "summer" – alex Feb 12 '15 at 15:31
  • 1
    @alex Not sure, which of all those examples you mean: with `lc rgb variable` you cannot have different key entries. – Christoph Feb 12 '15 at 15:34
  • For whatever reason, I had to use the directive 'notitle' after the each plot clause in order to surpress the legend from coming up. – chinnychinchin Sep 29 '15 at 21:53
25
plot "data.dat" using 2: xtic(1) with histogram

Here data.dat contains data of the form

title 1
title2 3
"long title" 5
Rohit
  • 7,449
  • 9
  • 45
  • 55
tatwright
  • 37,567
  • 6
  • 21
  • 9
20

I would just like to expand upon the top answer, which uses GNUPlot to create a bar graph, for absolute beginners because I read the answer and was still confused from the deluge of syntax.

We begin by writing a text file of GNUplot commands. Lets call it commands.txt:

set term png
set output "graph.png"
set boxwidth 0.5
set style fill solid
plot "data.dat" using 1:3:xtic(2) with boxes

set term png will set GNUplot to output a .png file and set output "graph.png" is the name of the file it will output to.

The next two lines are rather self explanatory. The fifth line contains a lot of syntax.

plot "data.dat" using 1:3:xtic(2) with boxes

"data.dat" is the data file we are operating on. 1:3 indicates we will be using column 1 of data.dat for the x-coordinates and column 3 of data.dat for the y-coordinates. xtic() is a function that is responsible for numbering/labeling the x-axis. xtic(2), therefore, indicates that we will be using column 2 of data.dat for labels.

"data.dat" looks like this:

0 label       100
1 label2      450
2 "bar label" 75

To plot the graph, enter gnuplot commands.txt in terminal.

zb226
  • 9,586
  • 6
  • 49
  • 79
Marco Rosas
  • 496
  • 4
  • 11
13

I recommend Derek Bruening's bar graph generator Perl script. Available at http://www.burningcutlery.com/derek/bargraph/

JasonMc
  • 709
  • 7
  • 4
2

You can directly use the style histograms provide by gnuplot. This is an example if you have two file in output:

set style data histograms
 set style fill solid
 set boxwidth 0.5
 plot "file1.dat" using 5 title "Total1" lt rgb "#406090",\
      "file2.dat" using 5 title "Total2" lt rgb "#40FF00"
JoSSte
  • 2,953
  • 6
  • 34
  • 54