Bappy
Bappy

Reputation: 13

Gnuplot bar diagram different color with value on top of bar

My data set is simple:

CPU         5.7  
Memory      3.7

I want to plot a simple bar diagram with different colors for each value and the corresponding values should be shown on top of each bar. I also want to plot the ylabel and the legend. It should almost look like the the following diagram:

sample-plot

Is this possible in gnuplot? There seems to be hardly any document for doing this in gnuplot. Plotting bars with historgram seems easy but styling with different colors, the value on top and the legends part is turning out to be a bit tricky for me. Can someone please help me?

Thanks in advance.

Upvotes: 1

Views: 2121

Answers (1)

maij
maij

Reputation: 4218

Maybe the following comes quite close: bar diagram

This is the gnuplot script:

set terminal pngcairo
set output "data.png"

set title "CPU and Memory"

set nokey

set boxwidth 0.8
set style fill solid

set xrange [-1:2]
set xtics nomirror

set yrange [0:7]
set grid y
set ylabel "Time"

plot "data.dat" using 0:2:3:xtic(1) with boxes lc rgb var ,\
     "data.dat" using 0:($2+0.5):2 with labels
  • The pseudo column 0, which is the current line number, is used as x value.
  • I have added a third column to your data which contains the color as rgb value.
  • The value on top of the bars is printed by the with labels command. It requires a using with three values: x, y, string. The part ($2+0.5) takes the y-value from the second column and adds 0.5.
  • The identifiers "CPU" and "Memory" are printed below the corresponding bar instead of using a separate key.

And this is the modified datafile:

CPU    5.7  0x4472c4
Memory 3.7  0xed7d31

Upvotes: 4

Related Questions