Search code examples
plotgnuplotcolorbar

Gnuplot: Associating line and point color of a dataset to a colorbar


Consider the following minimal gnuplot script:

set terminal epslatex size 4.1,3
set out 'Plot.tex'

plot './Plot1.out' u 1:3 notitle w linespoints lt 1 pt 6 ps 1 lc rgb 'black', \
     './Plot2.out' u 1:3 notitle w linespoints lt 1 pt 6 ps 1 lc rgb 'red', \
     './Plot3.out' u 1:3 notitle w linespoints lt 1 pt 6 ps 1 lc rgb 'blue', \
     './Plot4.out' u 1:3 notitle w linespoints lt 1 pt 6 ps 1 lc rgb 'green'

set out

I currently plot each plot with colors black, red, blue, and green for plots 1, 2, 3, and 4, respectively. How can I add a colorbar associated to the range [1,4], and have the colors of each plot correspond to their associated plot number (1, 2, 3, or 4)?

Edit: I specifically do not want to use a key for this purpose because in my actual example, I have 3 subplots stacked side-by-side, and 8 datasets in each (8 keys in each subplot makes the plot look really cluttered). Therefore, I would like to have a single colorbar that is common to all 3 subplots indicating the number of the dataset (1, 2, 3, etc.) in each subplot. See this screenshot taken from this journal article for an example of what I mean.


Solution

  • It's not fully clear to me what you mean with colorbar? Do you mean the legend or key? With notitle in the plot command you avoid having a key or legend.

    Check the following minimal example:

    Code:

    ### plots in a loop with key
    reset session
    
    set style line 1 pt 6 ps 1 lc rgb "black"
    set style line 2 pt 6 ps 1 lc rgb "red"
    set style line 3 pt 6 ps 1 lc rgb "blue"
    set style line 4 pt 6 ps 1 lc rgb "green"
    
    set key top center
    
    plot for [i=1:4] '+' u 1:($1*i) w lp ls i title sprintf("%d",i)
    
    ### end of code
    

    Result:

    enter image description here

    For your specific files add a function which defines your filenames and exchange the plot command accordingly.

    myFile(i) = sprintf("./Plot%d.out",i)
    
    plot for [i=1:4] myFile(i) u 1:3 w lp ls i title sprintf("%d",i)
    

    Addition:

    Then maybe something like this? This just shows the principle. Arranging the multiplot in a nice way with equal graph size and distances to each other and the colorbox is another topic.

    Code:

    ### plots in a loop without key but colorbox
    reset session
    
    set style line 1 pt 6 ps 1
    set style line 2 pt 6 ps 1
    set style line 3 pt 6 ps 1
    set style line 4 pt 6 ps 1
    
    set palette defined (1 "black", 2 "red", 3 "blue", 4 "green")
    
    set multiplot layout 2,2
    
        set cbtics 1
        set key out top right
        unset colorbox
        plot for [i=1:4] '+' u 1:($1*i):(i) w lp pt 6 palette notitle
    
        unset ytics
        set colorbox
        plot for [i=1:4] '+' u 1:($1*i):(i) w lp pt 6 palette notitle
    
        unset colorbox
        set ytics
        plot for [i=1:4] '+' u 1:($1*i):(i) w lp pt 6 palette notitle
    
        unset ytics
        set colorbox
        set palette maxcolors 4
        plot for [i=1:4] '+' u 1:($1*i):(i) w lp pt 6 palette notitle
    
    unset multiplot
    ### end of code
    

    Result:

    enter image description here