0

I have this data file (TotalDurationBarPlot.dat):

Indexed list 934
Tree list 3692
Array list 12274
Linked list 48188

What I wish to achieve is a histogram chart with 4 bars: one for Indexed list, one for Tree list, one for Array list, one for Linked list. My requirements are:

  1. Each bar has it's unique color,
  2. On top of each bar, there is a number denoting the height of the bar. (For example, above the Tree list, there is 3692.)
  3. It would be nice to have a legend on a light gray background with a thin black legend border at the top right corner.

My current attempt

As of now, my data file looks like:

# ILL TL AL LL
934 3692 12274 48188

... and my gnuplot script looks like:

set title font "Monospaced,13" 'Total duration'
set grid
set key right top
set style data histograms
set style histogram cluster gap 2
set style fill solid border 2
set xtics format ""
set grid ytics
set ylabel "Milliseconds"
set yrange [0:70000]
# set boxwidth 0.5 relative
# set label "Array list\n134908 ms" at graph 0.145,0.9

ArrayListColor   = "#491d75";
IndexedListColor = "#b32929";
LinkedListColor  = "#d49435";
TreeListColor    = "#12520b";

plot 'TotalDurationBarPlot.dat' using 1 title "Indexed list" linecolor rgb IndexedListColor, '' using 2 title "Tree list" linecolor rgb TreeListColor, '' using 3 title "Array list" linecolor rgb ArrayListColor, '' using 4 title "Linked list" linecolor rgb LinkedListColor, '' u 0:1:1 with labels offset -6.0,-100.0 title ""

set terminal png size 650,350 enhanced font "Monospaced,13"
set output 'TotalDuration.png'
replot
exit

It produces:

List comparison

Edit 1

The gnuplot code provided in the answer by @meuh produces the following plot:

Bad bars

2
  • 1
    You should move your post to stackoverflow.com where there are some very competent users who might answer you quickly.
    – meuh
    Commented May 23, 2023 at 16:01
  • @meuh Fair enough. Thanks for the hint!
    – coderodde
    Commented May 23, 2023 at 16:23

1 Answer 1

1
+50

I followed some of the hints you got from stackoverflow and, in case you don't have your own solution yet, ended with this close-enough approximation, using the original multiline data file.

set terminal png size 650,350 enhanced font "Monospaced,13"
set output 'TotalDuration.png'
set style fill solid border 2
set key noautotitle
set key box opaque fillcolor "0x7faaaaaa"
set grid x,y
set boxwidth 0.8 relative
set ylabel "Milliseconds"
set yrange [0:90000]

Array   = 0x491d75
Indexed = 0xb32929
Linked  = 0xd49435
Tree    = 0x12520b

list=""
plot 'TotalDurationBarPlot.dat' \
    u 0:3:(value(strcol(1))):xtic(1)   with boxes lc rgb var, \
 '' u 0:3:3:(list=list." ".stringcolumn(1)) with labels offset 0,0.7, \
 for [i=1:4] '' u (NaN):(NaN):(value(word(list,i))) with boxes lc rgb var title word(list,i)

I had to resort to gnuplot 6.1, in order to get the key "fillcolor" option, but that should also be in 5.4, which I don't have to hand. The main feature used here is to save the column 1 text ("Indexed", "Tree",...) in a string variable list, and then use word(list,i) to extract one word for the title. The for loop reads the file but does not plot anything as all the data is invalid, NaN.

The colours are set in 4 variables that have the same name as column 1 of the data. You get column 1 as a string with strcol(1), then use function value() to get the value of a variable of that name. This is an integer which is used by lc rgb var. The same is done in the for loop, but here the name is obtained from the collected list of names.

plot

I suggest that for any single aspect of this output that you want to change, you post a question on stackoverflow, so that iteratively you get to the answer you want.

14
  • Thank you for your time! Just one more detail: how could I control the bar colors in the gnuplot script you have provided? In my script, they are defined with ArrayListColor, IndexedListColor, etc.
    – coderodde
    Commented May 25, 2023 at 12:11
  • I've updated with a possible solution. You can see the diffs if you click on the link "edited XX hours/days ago".
    – meuh
    Commented May 25, 2023 at 13:27
  • Cant quite replicate the plot in your answer.
    – coderodde
    Commented May 26, 2023 at 3:40
  • That's a little vague. Are you using gnuplot 6.1?
    – meuh
    Commented May 26, 2023 at 5:15
  • I use version 5.4.6.
    – coderodde
    Commented May 26, 2023 at 5:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .