You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何增大簇状直方图Bin宽度且无簇列重叠?并调整簇间空白放大标签

Gnuplot簇状直方图调整方案

需求

对现有簇状直方图做以下调整:

  • 增大每个簇内Bin的宽度,保证同一簇内Bin尺寸一致且无重叠;
  • 增大簇与簇之间的空白间距,以便放大Bin顶部的标签尺寸。

原代码

reset session
$Data <<EOD
subject     2018/19 2021/22     
M       31.46   28.96   -2.5    
It      34.83   36.61   +1.8    
In      33.71   33.33   -0.4    
G       63.79   77.46   +13.7   
P       92.86   56.52   -36.3   
PS      67.86   60.87   -7.0    
A       21.05   65.00   +43.9   
D       53.33   54.35   +1.0    
EOD
set term svg 
set output "prova.svg"
set title "{/Arial:Bold=14 MEAN}\n {/Arial:Bold=14 2018/19 vs 2021/2022}" 
set yrange [0:100]
set ytics (0, '10' 10, '20' 20, '30' 30,'40' 40, '50' 50,'60' 60, '70' 70,'80' 80, '90' 90,'100' 100)
set grid y
set ylabel "% TOT" font ',14'
set tics scale 0
set tics font ',14'
set style fill pattern 1 border lt -1
set boxwidth 1 absolute
set key noautotitle
set key font ',14'
set xzeroaxis ls 1 lc "black"
PosX(row,col) = 0
#myColors   = "red  orange      yellow      green       cyan        blue        violet      deeppink"
myColors   = "0xff0000  0xffa500    0xffff00    0x80ff00    0x00ffff    0x0000ff    0x7f00ff    0xff1493"
myColor(i) = int(word(myColors,int(i+1)))
stats $Data u 0 nooutput   # get number of columns
N = STATS_columns
plot for [col=2:N] $Data u ($0*N+col):col:(myColor($0)) skip 1 w boxes lc rgb var, \
for [col=2:N]    '' u ($0*N+N/2.+1):(NaN):xtic(1) w boxes fill pattern col -1 lc "grey" ti columnheader(col),\
col=2    '' u ($0*N+col-3):col:(sprintf("%2.1f%%",$2))  with labels center offset 0, character 0.5 font ',9' notitle,\
col=3    '' u ($0*N+col-3):col:(sprintf("%+-.1f%%",$4)) with labels center offset 0, character 0.5 font ',9' tc lt 7 notitle

当前效果

簇内Bin宽度较窄,簇与簇之间间距紧凑,顶部标签尺寸偏小且拥挤。

修改后代码

reset session
$Data <<EOD
subject     2018/19 2021/22     
M       31.46   28.96   -2.5    
It      34.83   36.61   +1.8    
In      33.71   33.33   -0.4    
G       63.79   77.46   +13.7   
P       92.86   56.52   -36.3   
PS      67.86   60.87   -7.0    
A       21.05   65.00   +43.9   
D       53.33   54.35   +1.0    
EOD
set term svg 
set output "prova.svg"
set title "{/Arial:Bold=14 MEAN}\n {/Arial:Bold=14 2018/19 vs 2021/2022}" 
set yrange [0:100]
set ytics (0, '10' 10, '20' 20, '30' 30,'40' 40, '50' 50,'60' 60, '70' 70,'80' 80, '90' 90,'100' 100)
set grid y
set ylabel "% TOT" font ',14'
set tics scale 0
set tics font ',14'
set style fill pattern 1 border lt -1
# 增大Bin宽度,同时避免重叠
set boxwidth 0.8 absolute
set key noautotitle
set key font ',14'
set xzeroaxis ls 1 lc "black"
PosX(row,col) = 0
myColors   = "0xff0000  0xffa500    0xffff00    0x80ff00    0x00ffff    0x0000ff    0x7f00ff    0xff1493"
myColor(i) = int(word(myColors,int(i+1)))
stats $Data u 0 nooutput   # get number of columns
N = STATS_columns
# 调整x轴位置,增大簇间距(用$0*(N+1)代替$0*N)
plot for [col=2:N] $Data u ($0*(N+1)+col):col:(myColor($0)) skip 1 w boxes lc rgb var, \
for [col=2:N]    '' u ($0*(N+1)+N/2.+1):(NaN):xtic(1) w boxes fill pattern col -1 lc "grey" ti columnheader(col),\
col=2    '' u ($0*(N+1)+col-3):col:(sprintf("%2.1f%%",$2))  with labels center offset 0, character 0.5 font ',12' notitle,\
col=3    '' u ($0*(N+1)+col-3):col:(sprintf("%+-.1f%%",$4)) with labels center offset 0, character 0.5 font ',12' tc lt 7 notitle

关键修改点

  • 增大簇间距:将x坐标计算从$0*N+col改为$0*(N+1)+col,每个簇之间额外留出1个单位的空白;
  • 调整Bin宽度:把set boxwidth 1 absolute改为0.8 absolute,既加宽了Bin,又保证同一簇内两个Bin不会重叠;
  • 放大标签尺寸:将标签字体从,9调整为,12,配合增大的簇间距,标签显示更清晰;
  • 同步坐标对齐:xtic和标签的x坐标计算同步更新,确保与新的簇位置匹配。

内容的提问来源于stack exchange,提问作者zara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 05:50:37