直方图 Gnuplot 混合 Clustered/Stacked
Histogram Gnuplot mixing Clustered/Stacked
我有一个包含 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 (+offset):(++) title "par3" with boxes, \
'' using (+offset):(+) title "par2" with boxes, \
'' using (+offset):() title "par1" with boxes, \
'data2.dat' using (+offset+dx):(++) title "par3" with boxes, \
'' using (+offset+dx):(+) title "par2" with boxes, \
'' using (+offset+dx):() 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
这是结果
以下可能是进一步调整的起点。
我尽量保持通用性,以便您可以轻松添加更多列或行。
在您的代码中,您对列值 "manually" ++
求和。当然有一些方法可以总结列 "automatically"。
但是,我允许自己转置您的数据,因为我发现它更容易。如果你能忍受它,请告诉我。由于 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([=10=])?sum:sum=0):\
(BoxL(g,i)):(BoxR(g,i)):(sum):(sum=sum+column(i)):(myColors([=10=])) \
skip 1 w boxxy lc rgb var not, \
for [i=2:Cols+1] $Data2 u (g=2,i-1):(int([=10=])?sum:sum=0):\
(BoxL(g,i)):(BoxR(g,i)):(sum):(sum=sum+column(i)):(myColors([=10=])) \
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
结果:
我有一个包含 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 (+offset):(++) title "par3" with boxes, \
'' using (+offset):(+) title "par2" with boxes, \
'' using (+offset):() title "par1" with boxes, \
'data2.dat' using (+offset+dx):(++) title "par3" with boxes, \
'' using (+offset+dx):(+) title "par2" with boxes, \
'' using (+offset+dx):() 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
这是结果
以下可能是进一步调整的起点。 我尽量保持通用性,以便您可以轻松添加更多列或行。
在您的代码中,您对列值 "manually" ++
求和。当然有一些方法可以总结列 "automatically"。
但是,我允许自己转置您的数据,因为我发现它更容易。如果你能忍受它,请告诉我。由于 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([=10=])?sum:sum=0):\
(BoxL(g,i)):(BoxR(g,i)):(sum):(sum=sum+column(i)):(myColors([=10=])) \
skip 1 w boxxy lc rgb var not, \
for [i=2:Cols+1] $Data2 u (g=2,i-1):(int([=10=])?sum:sum=0):\
(BoxL(g,i)):(BoxR(g,i)):(sum):(sum=sum+column(i)):(myColors([=10=])) \
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
结果: