如何基于非规则Y值的(x,y,z)汽车数据生成gnuplot栅栏图
解决Gnuplot实心栅栏图(非规则Y值)的实现方案
数据格式调整要求
要实现垂直于X轴(年份)的实心栅栏效果,核心是把数据按年份分组,每组内的里程(Y值)保持有序(你已完成排序,只需保留该状态),并且组与组之间用空行分隔。最终数据文件格式示例:
2015 5000 15000 2015 12000 13500 2015 20000 12000 2016 6000 16000 2016 15000 14200 2016 22000 12500 2017 4500 17000 2017 18000 14800
如果用Python处理数据,可在分组后添加空行,代码片段:
import itertools # sorted_data为已按(year, mileage)排序的列表,元素格式(year, mileage, price) with open('car_data.dat', 'w') as f: for year, group in itertools.groupby(sorted_data, key=lambda x: x[0]): for item in group: f.write(f"{item[0]} {item[1]} {item[2]}\n") f.write("\n") # 年份组间添加空行
Gnuplot脚本配置
使用pm3d绘制实心面板,配合深度排序避免遮挡,脚本示例:
set terminal pngcairo enhanced size 1000,700 set output 'car_price_fence.png' set title 'Car Price by Year & Mileage' set xlabel 'Year' set ylabel 'Mileage' set zlabel 'Price' # 启用实心填充与深度排序,确保栅栏层级正确 set pm3d depthorder set style fill solid 1.0 set pm3d at b # 将填充面绘制在数据点底部,也可改用'at s'绑定到数据点高度 # 设置X轴为离散年份(根据你的数据范围调整) set xtics 2015,1,2017 set mxtics 0 # 绘制实心栅栏图 splot 'car_data.dat' using 1:2:3 with pm3d title 'Price'
备选:柱状栅栏效果
如果需要每个(year, mileage)对应独立的实心柱,可改用boxes样式,无需调整数据格式(保持每行一个数据点即可),脚本核心配置:
set style fill solid 0.8 border lt -1 set boxwidth 0.8 relative set style data boxes splot 'car_data.dat' using 1:2:3 with boxes title 'Price'
内容的提问来源于stack exchange,提问作者bstpierre
相关产品推荐
相关产品推荐

