【问题标题】:Histogram Gnuplot mixing Clustered/Stacked直方图 Gnuplot 混合集群/堆叠
【发布时间】:2019-12-02 20:52:46
【问题描述】:

我有一个包含 6 列的直方图。我想将它们成对聚集在一起,以使两个聚集组由两个堆叠的列组成。我几乎做到了,除了图例是我想要的条目的两倍并且图表没有居中

set terminal epslatex standalone color size 4.0in,3.0in background rgb "white"
set output 'massFlowSection.tex'

set xtics("ex1" 1, "ex2" 2, "ex3" 3)
#set yrange [0:100]

set style fill solid border -1
#set key invert
#set grid

num_of_ksptypes=2
set boxwidth 0.5/num_of_ksptypes
dx=0.5/num_of_ksptypes
offset=-0.12

plot 'data1.dat' using ($1+offset):($2+$3+$4) title "par3"  with boxes, \
         ''                   using ($1+offset):($3+$4) title "par2" with boxes, \
         ''                   using ($1+offset):($4) title "par1" with boxes, \
         'data2.dat' using ($1+offset+dx):($2+$3+$4) title "par3"  with boxes, \
         ''                   using ($1+offset+dx):($2+$3) title "par2" with boxes, \
         ''                   using ($1+offset+dx):($4)  title "par1" with boxes 

入口数据为文件data1.dat

area  par3     par2    par1  
1     0.0078   0.0211  0     
2     0.0139   0.0302  0    
3     0.0169   0       0.119 

和文件 data2.dat

nr  par3     par2    par1 
1   0.0083   0.0233  0     
2   0.0151   0.0302  0    
3   0.0173   0       0.211  

这是结果

【问题讨论】:

  • to clustered them in pairs to have two clustered group made of two stacked colums - 我不明白。你能画出它的样子吗?或者在网上找一个例子它应该是什么样子?对于提供的数据,充其量是什么?
  • 对不起,我可能不是很清楚,因为我第一次使用 gnuplot 探索直方图,所以我不太了解热学。我上传一个例子
  • 第二个示例的基础数据是如何组织的?看起来不像你上面显示的数据。
  • 没错,这是我通过谷歌搜索找到的一个例子。
  • 好吧,为了帮助我们,我们需要知道您的输入是什么以及所需的输出是什么。目前,您提供了一些输出错误的数据和没有相应数据的所需输出。我们应该如何提供帮助?

标签: gnuplot histogram


【解决方案1】:

以下内容可能是进一步调整的起点。 我尽量保持通用,以便您可以轻松添加更多列或行。

在您的代码中,您正在“手动”汇总列值$2+$3+$4。当然有一些方法可以“自动”总结列。 但是,我允许自己转置您的数据,因为我发现它更容易。让我知道你是否可以忍受它。由于 gnuplot 没有转置功能,您要么必须自己实现它,要么使用外部软件。后者,由于平台无关,我通常会尽量避免。我想还有一种方法可以为您的原始数据绘制相同的图。

我更喜欢with boxxyerror 的绘图风格,而不是总是从零开始的with boxes。 实际上,最后一个 plot 命令只是在绘制图例。这仍然不是最理想的。可以添加一个额外的声明,告诉您哪个堆栈是 Data1,哪个是 Data2。

代码:

### Histogram stacked and grouped
reset session

$Data1 <<EOD
area  "ex 1"  "ex 2"  "ex 3"
par1  0       0       0.119
par2  0.0211  0.0302  0
par3  0.0078  0.0139  0.0169
EOD

$Data2 <<EOD
nr    "ex 1"  "ex 2"  "ex 3"
par1  0       0       0.211
par2  0.0233  0.0302  0
par3  0.0083  0.0151  0.0173
EOD

set key top left
set style fill solid border -1

Cols = 3
Groups = 2
GroupGap = 0.2
BoxScale = 0.9
BoxWidth = (1.0-GroupGap)/Groups
Offset(g,i) = i-1.5+GroupGap/2.0 + BoxWidth/2 +(g-1)*BoxWidth

myXtic(i) = columnhead(i)
BoxL(g,i) = Offset(g,i)-BoxWidth/2.*BoxScale
BoxR(g,i) = Offset(g,i)+BoxWidth/2.*BoxScale
set xrange [0.5:Cols+0.5]

array myPalette[6] = [0xff0000, 0x00ff00, 0x0000ff, 0xffaaaa, 0xaaffaa, 0xaaaaff]
myColors(n) = myPalette[n+1+(g-1)*3]

set table $Legend
    plot $Data1 u (strcol(1)) w table
unset table

plot \
    for [i=2:Cols+1] $Data1 u (g=1,i-1):(int($0)?sum:sum=0):\
      (BoxL(g,i)):(BoxR(g,i)):(sum):(sum=sum+column(i)):(myColors($0)) \
      skip 1 w boxxy lc rgb var not, \
    for [i=2:Cols+1] $Data2 u (g=2,i-1):(int($0)?sum:sum=0):\
      (BoxL(g,i)):(BoxR(g,i)):(sum):(sum=sum+column(i)):(myColors($0)) \
      skip 1 w boxxy lc rgb var not, \
    for [i=2:|$Legend|] $Data1 u (g=1,i-1):(NaN):(myColors(i-2)): \
      xtic(columnhead(i)) w boxes lc rgb var ti word($Legend[i],1)
### end of code

结果:

【讨论】: