GNUplot: how to plot rowstacked bar chart using color codes in data?

前端 未结 2 1771
梦毁少年i
梦毁少年i 2021-01-22 22:16

I have the following sample data:

col1 2 0 1 1
col2 1 1 0 0
col3 1 1 1 0
col4 1 1 2 1
col5 1 1 1 1
col6 2 0 1 1
col7 1 1 2 2
col8 1 1 2 1

colum

2条回答
  •  滥情空心
    2021-01-22 23:04

    The colors are used to identify the corresponding parts in different stacks. Therefore they must follow the same order in all stacks which belong to the same histogram.

    But Gnuplot has a newhistogram command after which the colors can be "reset". And there is the multiplot command which can be used to add new histograms in a loop.

    Here is the part of the script which replaces the original plot command:

    set style line 1 lt 1 lc rgb "white"
    set style line 2 lt 1 lc rgb "green"
    set style line 3 lt 1 lc rgb "orange"
    
    stats 'test.data' u 2
    n = STATS_records
    
    set multiplot
    
    do for [i=0:n-1] {
       plot "test.data" u (0) ,\
            newhistogram "" at i, "" every ::i::i u (style = $5 + 1, 0), \
                                  "" every ::i::i u 2 ls style,          \
                                  "" every ::i::i u (style = $4 + 1, 0), \
                                  "" every ::i::i u 3 ls style
    }
    
    unset multiplot
    

    And this is how it works:

    • set style ...: Define the styles (colors) which will be used.
    • stats ...: Find the number of rows. We are going to plot an independent histogram for each row.
    • set multiplot: We will plot again and again on the same area.
    • "test.data" u (0): Plot nothing, but reserve space for all histograms.
    • new histogram "" at i: Initialize a new histogram without a name with offset i on the x axis. We are going to plot a single stack at this offset.
    • "" every ::i::i: Plot row i.
    • u (style = $5 + 1, 0): Plot nothing, but read the color of the current row from file.
    • u 2 ls style: Plot column 2 of the current row with the previously read style.

    I'm not sure whether this version is shorter than yours :)

提交回复
热议问题