Search code examples
gnuplot

How to make xtics label color variable in box plot in GNUPlot?


This is my attempt at making the xtics label color match the line color in a box plot in gnuplot:

$DATA << EOD
1 1
2 2
3 3
EOD

set linetype 1 linecolor rgb "red"
set linetype 2 linecolor rgb "green"
set linetype 3 linecolor rgb "blue"

set key off

set boxwidth 0.5
set style fill solid 0.5

set xrange [0:4]
set yrange [0:4]

set xtics     ("1" 1) textcolor rgb "red"
set xtics add ("2" 2) textcolor rgb "green"
set xtics add ("3" 3) textcolor rgb "blue"

plot $DATA using 1:2:1 with boxes linecolor variable

But it does not work: enter image description here

Any idea? Thanks!


Solution

  • I'm not sure whether you can set xtics individually in different colors. So, the following solution sets the xtics as empty lines and you plot your xtics with labels with a certain offset. The disadvantage is that you have to set the y-position here: (0) with offset 0,-1. I hope there are better solutions.

    Code:

    ### "xtic labels" in different colors
    reset session
    
    $Data << EOD
    1 1
    2 2
    3 3
    EOD
    
    set linetype 1 lc "red"
    set linetype 2 lc "green"
    set linetype 3 lc "blue"
    
    set key off
    set boxwidth 0.5
    set style fill solid 0.5
    
    set xrange [0:4]
    set yrange [0:4]
    set format x "\n"     # xtic label empty line
    
    plot $Data u 1:2:1 w boxes lc var, \
            '' u 1:(0):1:1 w labels tc var offset 0,-1 
    ### end of code
    

    Alternatively, you could use an offset relative to the graph:

    plot $Data u 1:2:1 w boxes lc var, \
            '' u 1:(0):1:1 w labels tc var offset 0, graph -0.05 
    

    Result:

    enter image description here