Gnuplot绘制小提琴图:X轴未显示且与Y轴底部未对齐
Gnuplot小提琴图问题修复
问题描述
绘制小提琴图时遇到两个问题:
- 仅显示X轴刻度,未显示X轴轴线
- X轴未与Y轴范围的底部对齐
解决方案
只需修改代码末尾的几个设置项即可解决问题:
- 调整
border参数启用X轴对应的下边框 - 将X轴刻度和轴线定位到Y轴范围的底部
修改后的完整代码
set terminal svg size 1920,1080 set output "pic.svg" set print $viol0 y = 0.24 print sprintf(" 0.0 %8.5g", y) y = 0.48 print sprintf(" 0.0 %8.5g", y) y = 0.28 print sprintf(" 0.0 %8.5g", y) y = 0.56 print sprintf(" 0.0 %8.5g", y) y = 0.52 print sprintf(" 0.0 %8.5g", y) y = 0.32 print sprintf(" 0.0 %8.5g", y) unset print set print $viol1 y = -0.24 print sprintf(" 0.0 %8.5g", y) y = -0.16 print sprintf(" 0.0 %8.5g", y) y = -0.44 print sprintf(" 0.0 %8.5g", y) y = -0.28 print sprintf(" 0.0 %8.5g", y) y = -0.28 print sprintf(" 0.0 %8.5g", y) y = -0.56 print sprintf(" 0.0 %8.5g", y) unset print set print $viol2 y = 0.0 print sprintf(" 0.0 %8.5g", y) y = 1.0 print sprintf(" 0.0 %8.5g", y) y = 0.5833333333333344 print sprintf(" 0.0 %8.5g", y) y = 0.5000000000000009 print sprintf(" 0.0 %8.5g", y) y = 0.7499999999999996 print sprintf(" 0.0 %8.5g", y) y = 0.08333333333333348 print sprintf(" 0.0 %8.5g", y) unset print set print $viol3 y = 0.3333333333333336 print sprintf(" 0.0 %8.5g", y) y = 0.5999999999999998 print sprintf(" 0.0 %8.5g", y) y = 0.5999999999999998 print sprintf(" 0.0 %8.5g", y) y = 0.6666666666666664 print sprintf(" 0.0 %8.5g", y) y = 0.6999999999999997 print sprintf(" 0.0 %8.5g", y) y = 0.5666666666666671 print sprintf(" 0.0 %8.5g", y) unset print set print $viol4 y = 1.2999999999999996 print sprintf(" 0.0 %8.5g", y) y = 0.8499999999999999 print sprintf(" 0.0 %8.5g", y) y = 0.975 print sprintf(" 0.0 %8.5g", y) y = 0.7999999999999999 print sprintf(" 0.0 %8.5g", y) y = 0.8499999999999999 print sprintf(" 0.0 %8.5g", y) y = 0.7749999999999999 print sprintf(" 0.0 %8.5g", y) unset print set print $viol5 y = 2.8666666666666694 print sprintf(" 0.0 %8.5g", y) y = 0.6000000000000024 print sprintf(" 0.0 %8.5g", y) y = 1.0666666666666682 print sprintf(" 0.0 %8.5g", y) y = 0.9333333333333347 print sprintf(" 0.0 %8.5g", y) y = 0.8666666666666665 print sprintf(" 0.0 %8.5g", y) y = 0.733333333333333 print sprintf(" 0.0 %8.5g", y) unset print set title 'Overview' set border 2 set xrange [33:36] set xtics ("A" 34, "B" 35) set xtics nomirror scale 0 set ytics nomirror rangelimited unset key set style data points set linetype 9 lc "#800072b2" ps 0.5 pt 5 set linetype 10 lc "#8033bbbb" ps 0.5 pt 5 # set title "Same data - kernel density" set style data filledcurves below set auto x unset ytics set border 3 set margins screen .15, screen .85, screen .15, screen .85 set key set table $kdensity0 plot $viol0 using 2:(1) smooth kdensity bandwidth 0.15 with filledcurves above y lt 9 unset table set table $kdensity1 plot $viol1 using 2:(1) smooth kdensity bandwidth 0.15 with filledcurves above y lt 9 unset table set table $kdensity2 plot $viol2 using 2:(1) smooth kdensity bandwidth 0.15 with filledcurves above y lt 9 unset table set table $kdensity3 plot $viol3 using 2:(1) smooth kdensity bandwidth 0.15 with filledcurves above y lt 9 unset table set table $kdensity4 plot $viol4 using 2:(1) smooth kdensity bandwidth 0.15 with filledcurves above y lt 9 unset table set table $kdensity5 plot $viol5 using 2:(1) smooth kdensity bandwidth 0.15 with filledcurves above y lt 9 unset table unset key # --- 修改部分开始 --- set border 1 | 2 # 同时启用下边框(X轴)和右边框(Y轴) unset margins set ytics nomirror rangelimited set xtics nomirror scale 0 at y=-1 # 将X轴刻度定位到Y轴范围底部(y=-1) # --- 修改部分结束 --- set style boxplot nooutliers set style fill solid bo -1 set boxwidth 0.075 set errorbars lt black lw 1 set xrange [-0.5 :(6-0.5)] set xtics autofreq 1 set yrange [-1:2] plot \ $kdensity0 using (0 + $2/200.):1 with filledcurve above x=0 lt 9,\ '' using (0 - $2/200.):1 with filledcurve below x=0 lt 9,\ $viol0 using (0):2 with boxplot fc "white" lw 2,\ $kdensity1 using (1 + $2/200.):1 with filledcurve above x=1 lt 9,\ '' using (1 - $2/200.):1 with filledcurve below x=1 lt 9,\ $viol1 using (1):2 with boxplot fc "white" lw 2,\ $kdensity2 using (2 + $2/200.):1 with filledcurve above x=2 lt 9,\ '' using (2 - $2/200.):1 with filledcurve below x=2 lt 9,\ $viol2 using (2):2 with boxplot fc "white" lw 2,\ $kdensity3 using (3 + $2/200.):1 with filledcurve above x=3 lt 9,\ '' using (3 - $2/200.):1 with filledcurve below x=3 lt 9,\ $viol3 using (3):2 with boxplot fc "white" lw 2,\ $kdensity4 using (4 + $2/200.):1 with filledcurve above x=4 lt 9,\ '' using (4 - $2/200.):1 with filledcurve below x=4 lt 9,\ $viol4 using (4):2 with boxplot fc "white" lw 2,\ $kdensity5 using (5 + $2/200.):1 with filledcurve above x=5 lt 9,\ '' using (5 - $2/200.):1 with filledcurve below x=5 lt 9,\ $viol5 using (5):2 with boxplot fc "white" lw 2
修改说明
set border 1 | 2:Gnuplot的border参数为位掩码,1对应下边框(X轴),2对应右边框(Y轴),组合后可同时显示X轴和Y轴轴线set xtics ... at y=-1:强制X轴刻度和轴线对齐到Y轴范围的底部(即yrange最小值-1),解决X轴未对齐的问题
内容的提问来源于stack exchange,提问作者Andrea Manghi
相关产品推荐
相关产品推荐

