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

前端 未结 2 1769
梦毁少年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:00

    I would use the boxxyerrorbars plotting style. Despite the name, this is what you should use, when you want to plot "different" boxes.

    set key off
    unset border
    unset xtics
    unset ytics
    unset colorbox
    set style fill solid 1.0 border -1
    
    set palette model RGB defined ( 0 'orange', 1 'green', 2 'white')
    set cbrange [0:2]
    set style data boxxyerrorbars
    plot 'test.data' u 0:(0.5*$2):(0.5):(0.5*$2):4 lc palette,\
        '' u 0:($2 + 0.5*$3):(0.5):(0.5*$3):5 lc palette
    

    The boxxyerrorbars plotting style itself takes four columns, x, y, dx, and dy, and the lc palette uses the values in a fifth column to determine the color based on the current palette. To make the values in the palette absolute values I additionally set the cbrange to the same range which is covered by the palette.

    The expression using 0:(0.5*$2):(0.5):(0.5*$2):4 means:

    • Use the zeroth column (the row number) as x-value (box center)
    • the value in the second column multiplied by 0.5 as y-value (box center)
    • the number 0.5 as dx (half of the box width)
    • the value in the second column multiplied by 0.5 as dy-value (half of the box height)

    For the second plot part the y-value is the value in the second column plus half of the value in the third column.

    This solution can also easily be written to allow increasing the number of stacked boxes:

    set palette model RGB defined ( 0 'orange', 1 'green', 2 'white')
    set cbrange [0:2]
    set style data boxxyerrorbars
    
    last_column = 3
    plot for [i=2:last_column] 'test.data' \
        u 0:(0.5*column(i) + sum[c=2:(i-1)] column(c)):(0.5):(0.5*column(i)):(column(last_column + i - 1)) lc palette
    

提交回复
热议问题