Gnuplot如何为双列多行文本添加黑色边框并解决对齐问题
Gnuplot文本框边框与对齐问题
我想给绘图上的文本信息添加黑色边框,但尝试后未成功。文本分为两列,第一列文本右对齐,第二列数值左对齐,且数值在文本框末端未完全左对齐。
原代码
reset session mean = 1.85 variance = 961.67 median = 23.12 lowQuartile = .23 upQuartile = 2.25 set yrange[0:1] set xrange[0:1] infostat = sprintf("Mean : \nVariance : \nMedian : \nFirst Quartile : \nThird Quartile : ") infonumber = sprintf(" %6.2f\n %6.2f\n %6.2f\n %6.2f\n %6.2f", mean, variance, median, lowQuartile, upQuartile) set style textbox 2 opaque fc rgb 0xffff00 noborder set style textbox 3 transparent fc rgb 0xffff00 border lc "black" set label 10 infostat at graph .6,.8 right boxed bs 2 front set label 11 infonumber at graph .6,.8 left boxed bs 2 front plot NaN notitle
原输出效果

解决方案
方法:合并文本块实现统一边框与对齐
原问题核心是用了两个独立文本框,无法形成统一边框,且坐标定位式的对齐容易错位。将两列内容合并为单个格式化字符串,再添加统一边框即可解决问题。
修改后的代码:
reset session mean = 1.85 variance = 961.67 median = 23.12 lowQuartile = .23 upQuartile = 2.25 set yrange[0:1] set xrange[0:1] # 合并两列内容,用固定宽度格式化实现精准对齐 infoText = sprintf("%20s %6.2f\n%20s %6.2f\n%20s %6.2f\n%20s %6.2f\n%20s %6.2f", \ "Mean :", mean, \ "Variance :", variance, \ "Median :", median, \ "First Quartile :", lowQuartile, \ "Third Quartile :", upQuartile) # 设置带黑色边框的文本框样式 set style textbox opaque fc rgb 0xffff00 border lc "black" # 添加单个标签,指定位置并居中 set label 10 infoText at graph .7,.8 center boxed front plot NaN notitle
关键修改说明
- 合并文本块:用
sprintf将左列文本(%20s实现右对齐)和右列数值(%6.2f固定格式)合并为一个字符串,确保两列自动对齐。 - 统一边框:使用单个文本框标签,直接应用带黑色边框的样式,避免多个文本框的边框错位问题。
- 位置调整:将标签位置设为
graph .7,.8并居中,可根据需求修改坐标适配绘图布局。
内容的提问来源于stack exchange,提问作者Guy B
相关产品推荐
相关产品推荐

