gnuplot绘图异常:数据定义半径与实际绘图半径不符
问题描述
原本可正常生成饼图切片的gnuplot脚本出现异常,执行脚本如下:
$DATA<<EOD 0.00,0.00,0.7000,90.0000,114.0000 0.00,0.00,1.0000,114.0000,162.0000 0.00,0.00,1.0000,162.0000,234.0000 0.00,0.00,1.0000,234.0000,330.0000 0.00,0.00,1.0000,330.0000,450.0000 EOD set datafile separator comma set style data circles set term windows unset xtics; unset ytics; unset x2tics; unset y2tics set grid noxtics set grid noytics set xrange [-1.5:1.5] set yrange [-1.5:1.5] set border 0 set colorsequence classic set title "{/Tahoma=22 Plotting with Pies}" textcolor rgb "0xE25822" rotate by 0 set key inside top right opaque fc rgb "0xFFFFFF" set label 1 "Alpha\n1" at -0.27,1.27 center font "Tahoma:Bold,12" front set label 2 "Bravo\n2" at -0.97,0.87 center font "Tahoma:Bold,12" front set label 3 "Charlie\n3" at -1.24,-0.40 center font "Tahoma:Bold,12" front set label 4 "Delta\n4" at 0.27,-1.27 center font "Tahoma:Bold,12" front set label 5 "Echo\n5" at 1.13,0.65 center font "Tahoma:Bold,12" front plot $DATA every ::0::0 using 1:2:3:4:5 with circles fillstyle solid fc rgb "0x007FFF" title "Alpha (6.67%)",\ $DATA every ::1::1 using 1:2:3:4:5 with circles fillstyle solid fc rgb "0x3CD070" title "Bravo (13.33%)",\ $DATA every ::2::2 using 1:2:3:4:5 with circles fillstyle solid fc rgb "0x738678" title "Charlie (20.00%)",\ $DATA every ::3::3 using 1:2:3:4:5 with circles fillstyle solid fc rgb "0xE25822" title "Delta (26.67%)",\ $DATA every ::4::4 using 1:2:3:4:5 with circles fillstyle pattern 4 title "Echo (33.33%)"
异常表现:
- 数据中Alpha切片的半径定义为0.7,其余切片为1.0
- 但生成的图表里,Alpha实际显示半径为1,其余切片为1.4,与设定值完全不符
异常原因
核心问题是误用了with circles样式绘制扇形切片:
with circles的设计初衷是绘制完整圆形,不支持通过起始角度:结束角度参数生成扇形- 当传入5列数据给
with circles时,gnuplot不会将第3列识别为绝对半径,而是把它当作散点的大小缩放因子,这个值会结合当前坐标轴范围自动调整,导致实际显示的半径和设定值偏差 - 之前脚本可能因旧版gnuplot的兼容逻辑偶然正常工作,版本更新后参数解读严格化,导致异常显现
修复方案
把所有with circles替换为with ellipses arc,同时调整using列的对应关系(椭圆需要x/y两个半轴,这里是圆形,所以重复半径列即可):
修改后的plot命令如下:
plot $DATA every ::0::0 using 1:2:3:3:4:5 with ellipses arc fillstyle solid fc rgb "0x007FFF" title "Alpha (6.67%)",\ $DATA every ::1::1 using 1:2:3:3:4:5 with ellipses arc fillstyle solid fc rgb "0x3CD070" title "Bravo (13.33%)",\ $DATA every ::2::2 using 1:2:3:3:4:5 with ellipses arc fillstyle solid fc rgb "0x738678" title "Charlie (20.00%)",\ $DATA every ::3::3 using 1:2:3:3:4:5 with ellipses arc fillstyle solid fc rgb "0xE25822" title "Delta (26.67%)",\ $DATA every ::4::4 using 1:2:3:3:4:5 with ellipses arc fillstyle pattern 4 title "Echo (33.33%)"
using 1:2:3:3:4:5对应:x坐标:y坐标:x半轴长度:y半轴长度:起始角度:结束角度arc参数指定只绘制椭圆的对应角度区间,从而生成扇形切片- 调整后,第3列的半径值会被当作绝对长度,和你设定的0.7、1.0完全匹配
内容的提问来源于stack exchange,提问作者Marionette_66
相关产品推荐
相关产品推荐

