Search code examples
gnuplot

How to use gnuplot to draw a bar chart, and color it using the 'accent' color palette?


data.txt

cola colb
name a 60
name b 55
name c 10
cola colb
namea 60
nameb 55
namec 10

this is my code.

set linetype 1  lc rgb '#7FC97F' # pale green
set linetype 2  lc rgb '#BEAED4' # pale purple
set linetype 3  lc rgb '#FDC086' # pale orange

set boxwidth 0.8 relative
set style fill solid border 

plot 'data.txt' using 0:2:($0+1):xtic(1) w boxes lc var

I want to know if there is any other more concise way? like

load "accent.pal" #load accent palette
plot 'data.txt' w boxes lc accent

I get accent.pal from Gnuplotting/gnuplot-palettes

=====New ======

1 with set key autotitle columnhead setkey

2 no set key autotitle columnhead nosetkey


Solution

  • Yes, there is. You have been pretty close.

    Short answer:

    plot 'data.txt' u 0:2:0:xtic(1) w boxes lc palette
    

    You have to be aware that the palette accent.pal uses 8 colors and no grading in between them (set palette maxcolors 8).

    $0 (basically the row number) will determine the color and the values will set cbrange. If you only have 3 data points, the colors will be taken equidistant from palette. If you want row 1 to get the first color and row N the Nth color you have to fix your cbrange to the number of colors in the palette, i.e.cbrange[1:8].

    Script:

    ### load palette from external file
    reset session
    
    $Data <<EOD
    cola colb
    namea 60
    nameb 55
    namec 10
    EOD
    
    set boxwidth 0.8 relative
    set style fill solid border 
    set yrange[0:]
    
    set cbrange[1:8]
    # unset colorbox     # uncomment if you want to remove the colorbox
    load "accent.pal"    # load accent palette
    
    plot $Data u 0:2:0:xtic(1) w boxes lc palette notitle
    ### end of script
    

    Result:

    enter image description here