I need to generate a gnuplot histogram in order to see the CPU and RAM evolution of my cluster per month :
I want to generate the histogram from this file :
July 2018,19%,46% August 2018,20%,45% September 2018,20%,41% October 2018,21%,39% November 2018,21%,39% December 2018,21%,41% January 2019,25%,46% February 2019,27%,50%
To do that, this my code :
set title " CLUSTER 1 " set terminal png truecolor size 960, 720 set output " cluster1.png" set key below set grid set style data histograms set style fill solid 1.00 border -1 set datafile separator "," plot 'cluster.txt' using 2:xtic(1) title " CPU consumption (%) ", '' using 3 title " RAM consumption (%)"
For the moment, I have this result :
But as you can see, I have a problem with my x axies. The dates overlap each other and I’m not able to change that… Can you show me how to change that ?
And, can you tell me how I can put the percents in/above the histograms bars ?
Finally, I would like an histograms like this :
Advertisement
Answer
To wrap words in categories, you can replace a space to line break if necessary with a ternary function:
f(w) = (strlen(w) > 10 ? word(w, 1) . "n" . word(w, 2) : w)
It replaces a space to “n” if a length of a label is more than 10 characters.
To add a percentage sign on Y axis, set y format like this:
set format y "%g%%"
To add labels, use plot with labels:
'' using 0:($2+1):(sprintf("%g%%",$2)) with labels notitle, '' using 0:($3+1):(sprintf(" %g%%",$3)) with labels notitle
You may need to change bottom margin of the plot to fit two-line labels and key:
set bmargin at screen 0.1
So the script becomes like this:
f(w) = (strlen(w) > 10 ? word(w, 1) . "n" . word(w, 2) : w) set title "CLUSTER 1" set terminal png truecolor size 960, 720 set output "cluster1.png" set bmargin at screen 0.1 set key below set grid set style data histograms set style fill solid 1.00 border -1 set boxwidth 0.7 relative set yrange [0:] set format y "%g%%" set datafile separator "," plot 'cluster.txt' using 2:xtic(f(stringcolumn(1))) title " CPU consumption (%) ", '' using 3 title " RAM consumption (%)", '' using 0:($2+1):(sprintf("%g%%",$2)) with labels notitle, '' using 0:($3+1):(sprintf(" %g%%",$3)) with labels notitle