colors - GNUplot:如何使用数据中的颜色代码绘制行堆叠条形图?

标签 colors gnuplot histogram rgb stacked

我有以下示例数据:

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

第 4 列和第 5 列是第 2 列和第 3 列的颜色代码。例如,我想要“1”表示绿色,2 表示白色,0 表示橙色。

这是我尝试绘制它的方法:

set key off
unset border
unset xtics
unset ytics
set style data histograms
set style histogram rowstacked
set boxwidth 1 relative
set style fill solid 1.0 border -1
set yrange [0:2]
set lmargin 0
set rmargin 0
set tmargin 0
set bmargin 0.1
set terminal pngcairo truecolor size 100,65
set output "/export/test.png"
set palette model RGB defined ( 0 'orange', 1 'green', 2 'white')
plot 'test.data' u 2 linecolor rgb "orange",  '' u 3 linecolor rgb "green"

这是我在无法根据实际数据实现颜色的情况下得到的结果:

Sample Image

那么如何使用数据文件中指定的实际颜色?

[如果有人想知道并且如果这背后的理性鼓励您的努力;-):我正在尝试绘制 HP Blade c7000 机箱,使用颜色编码来显示空的机箱插槽、带有已通电 Blade 的插槽以及离开。 #2 和 #3 列中的实际值指示 Blade 是全高 Blade (值为 2)还是半高 Blade (值为 1)。如果没有 Blade ,它的值仍然为 1,默认为半高。]

## 更新

有了这个,我就能得到我需要的东西......有人有更短的版本吗?

plot 'test.data' u ($5==0?$2:sqrt(-1)) linecolor rgb "orange",\
    '' u ($5==1?$2:sqrt(-1)) linecolor rgb "green",\
    '' u ($5==2?$2:sqrt(-1)) linecolor rgb "white",\
    '' u ($4==0?$3:sqrt(-1)) linecolor rgb "orange",\
    '' u ($4==1?$3:sqrt(-1)) linecolor rgb "green",\
    '' u ($4==2?$3:sqrt(-1)) linecolor rgb "white"

new sample image

进一步细化

如何向 block 添加编号,如下图所示:

blocks with numbering

更新的问题:

我注意到有时调色板的颜色会出错,除非调色板中定义的所有颜色都存在于数据中。例如,使用以下数据:

1  2 0 1 1
2  1 1 0 0
3  2 0 1 1
4  2 0 1 1
5  1 1 1 1
6  1 1 1 0
7  1 1 1 1
8  1 1 1 1

正如您从第 4-5 栏看到的,我只有“绿色”(1) 和“橙色”(0)。因为我没有任何空槽,所以我没有“白色”颜色。我得到这个图像:

enter image description here

这张图片显然是不正确的,因为白色表明我有一个空槽。我期待这张图片:

enter image description here

那么“白色”和“绿色”的颜色是如何翻转的呢?我注意到如果我将其中任何一种颜色代码更改为白色,那么调色板就会正常工作。

最佳答案

我会使用boxxyerrorbars绘图风格。尽管有这个名称,但当您想要绘制“不同”的框时,您应该使用它。

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

boxxyerrorbars绘图样式本身需要四列,x , y , dx ,和dy ,以及 lc palette使用第五列中的值来根据当前调色板确定颜色。为了使调色板中的值成为绝对值,我另外将 cbrange 设置为调色板覆盖的相同范围。

表达式using 0:(0.5*$2):(0.5):(0.5*$2):4意思是:

  • 使用第零列(行号)作为 x -值(框中心)
  • 第二列中的值乘以 0.5,即 y -值(框中心)
  • 号码0.5dx (框宽度的一半)
  • 第二列中的值乘以 0.5,即 dy -value(盒子高度的一半)

对于第二个情节部分 y -value 是第二列中的值加上第三列中值的一半。

enter image description here

这个解决方案也可以轻松编写以允许增加堆叠盒子的数量:

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

关于colors - GNUplot:如何使用数据中的颜色代码绘制行堆叠条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40986627/

相关文章:

css - 如何在 Ionic 2 组件模板中使用 SASS color()?

PHP While - 每次都改变颜色

R:ggplot2:使用密度叠加向直方图添加计数标签

colors - gnuplot 背景颜色

Python:由数据点列表制作的 3D 绘图可以有渐变配色方案吗?

gnuplot - 如何在gnuplot处理的任何时间获取特定行中特定列的值?

gnuplot - 使用不同颜色在 gnuplot 中绘制文件数据

Gnuplot 从文件中绘制垂直线

r - R 中的叠加直方图(首选 ggplot2)

c++ - OpenCV 加载/保存直方图数据