0

I have the following gnuplot script:

set title font "Monospaced,13" 'Total duration'
set terminal png size 650,350 enhanced font "Monospaced,13"
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]

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 output 'TotalDuration.png'
replot
exit

The data file TotalDurationBarPlot.dat is:

# ILL TL AL LL
934 3692 12274 48188

... and it produces:

enter image description here

Q: I would like to color the four bars. How could I do it?

coderodde
  • 1,269
  • 4
  • 17
  • 34

1 Answers1

1

Why do you want to use histogram plotting style if you have a simple bar chart which you can simply draw with boxes? And why don't you stay with the data format as in your earlier question?

This could also be of interest (almost duplicate): gnuplot: Colour-coding specific / individual histogram bars

Edit: even simpler, add another column with the hexadecimal RGB color code.

Further reading: help int, help xticlabels, help linecolor variable, help colorspec.

Script:

### bar chart with variable box color
reset session

$Data <<EOD
"Indexed list"    934   0xb32929
"Tree list"      3692   0x12520b
"Array list"    12274   0x491d75
"Linked list"   48188   0xd49435
EOD

set style fill solid 1.0
set boxwidth 0.8
set key noautotitle
set grid x,y
set tics out
set yrange[0:60000]

plot $Data u 0:2:3:xtic(1) w boxes lc rgb var, \
        '' u 0:2:2 w labels offset 0,0.7
### end of script

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
  • Close, but no cigar. On Windows gnuplot 5.4.7, an 0 bytes long PNG is generated. On Linux gnuplot 6.1, gnuplot complains that there is no `png` terminal available even though it is listed in the list of supported terminals. – coderodde Jun 01 '23 at 11:48
  • @coderodde 1. use `set term pngcairo` 2. add a `set output` as the last line. – theozh Jun 01 '23 at 11:57
  • Unfortunately, the problem persists: on windows gnuplot 5.4.7, all of the terminals `png`, `pngcairo` and `svg` produce empty files. The LInux gnuplot 6.1 does not support the `pngcairo` to begin with. However, when I run the script from the command line, it flashes prompty the plot and it appears to be what I want, so I guess we are getting closer. – coderodde Jun 01 '23 at 12:11
  • @coderodde phew, maybe remove the `exit` at the end (why do yo need this?). Maybe gnuplot closes before it can close the output file. Well, if I save the script file as text file, e.g. "myPlot.gp" and in the gnuplot console type `load 'myPlot.gp'`. The sequence is `set output 'myPNG.png'; plot ... ; set output # to close the PNG file` Have you put `set output` at the end? This should work fine. – theozh Jun 01 '23 at 12:18
  • I found another solution: I set the terminal to `wxt`, run the gnuplot script after which the plot pops up in a window. Then, I just save the file in the window to a desired PNG file. – coderodde Jun 01 '23 at 12:22
  • All in all, thank you for your patience! – coderodde Jun 01 '23 at 12:22
  • @coderodde finally! Well, it's not the idea of the pngcairo terminal to use wxt and save it manually via user dialog as PNG. – theozh Jun 01 '23 at 12:25
  • Finally!? Let me tell you: it’s hard to navigate in the maze of information when you have severe ADD and schizophrenia that requires extensive sedative medication. – coderodde Jun 01 '23 at 13:00
  • 1
    @coderodde ok, I understand. I agree that gnuplot documentation and examples are sometimes really a maze to find the right information. But it is "amazing" what it can do ;-) – theozh Jun 01 '23 at 14:02