如何在gnuplot中绘制指定样式的多个体蜘蛛图?
How to Create a Spider Plot in Gnuplot (Matching the Target Style)
没问题!要在gnuplot里做出你想要的那种多个体蜘蛛图(和你提到的中间样式一致),咱们一步步来操作:
Step 1: Organize Your Data First
你给出的原始数据是一行格式,得调整成gnuplot更容易处理的结构。把每个原子的数据单独成行,第一行放上指标名称(作为蜘蛛图的轴标签),保存成data.txt文件:
# a b c 43 1.2 1.3 1.4 44 1.2 1.4 1.8 45 1.3 1.8 1.9 46 2.0 2.3 2.4 47 1.5 1.6 1.8
Step 2: Gnuplot Script for the Spider Chart
下面是完整的gnuplot脚本,我会逐段解释关键部分:
# 1. 设置输出终端和文件,这里用SVG(矢量图清晰),也可以换成pngcairo输出PNG set terminal svg enhanced background rgb 'white' size 800,600 set output 'spider_plot.svg' # 2. 启用极坐标模式(蜘蛛图的核心是极坐标变种) set polar set angles degrees # 用角度计算,比弧度更直观 set rrange [0:2.5] # 径向范围,根据你的数据最大值(2.4)调整 # 3. 自定义网格和刻度 set grid polar 0.5,1,1.5,2,2.5 # 环形网格线,对应数值刻度 set format r '' # 隐藏默认的径向数值标签 set xtics axis # 把指标标签放在轴上 # 给每个指标分配角度:3个指标,每个间隔120度(360/3) set xtics ( 'a' 0, 'b' 120, 'c' 240 ) # 4. 设置标题 set title "Spider Chart for Atoms" font ",14" # 5. 绘制蜘蛛图:先画每个点,再连接成闭合多边形(不同原子用不同颜色) plot 'data.txt' using (0):1 title '' with linespoints lc rgb '#1f77b4' pt 7, \ '' using (120):2 title '' with linespoints lc rgb '#ff7f0e' pt 7, \ '' using (240):3 title '' with linespoints lc rgb '#2ca02c' pt 7, \ # 连接Atom 43的点成闭合图形 '' using ($0==0?NaN:0):1 title 'Atom 43' with lines lc rgb '#1f77b4' lw 2, \ '' using ($0==0?NaN:120):2 title '' with lines lc rgb '#1f77b4' lw 2, \ '' using ($0==0?NaN:240):3 title '' with lines lc rgb '#1f77b4' lw 2, \ '' using ($0==0?NaN:0):1 title '' with lines lc rgb '#1f77b4' lw 2, \ # 连接Atom 44的点 '' using ($0==1?NaN:0):1 title 'Atom 44' with lines lc rgb '#ff7f0e' lw 2, \ '' using ($0==1?NaN:120):2 title '' with lines lc rgb '#ff7f0e' lw 2, \ '' using ($0==1?NaN:240):3 title '' with lines lc rgb '#ff7f0e' lw 2, \ '' using ($0==1?NaN:0):1 title '' with lines lc rgb '#ff7f0e' lw 2, \ # 连接Atom 45的点 '' using ($0==2?NaN:0):1 title 'Atom 45' with lines lc rgb '#2ca02c' lw 2, \ '' using ($0==2?NaN:120):2 title '' with lines lc rgb '#2ca02c' lw 2, \ '' using ($0==2?NaN:240):3 title '' with lines lc rgb '#2ca02c' lw 2, \ '' using ($0==2?NaN:0):1 title '' with lines lc rgb '#2ca02c' lw 2, \ # 连接Atom 46的点 '' using ($0==3?NaN:0):1 title 'Atom 46' with lines lc rgb '#d62728' lw 2, \ '' using ($0==3?NaN:120):2 title '' with lines lc rgb '#d62728' lw 2, \ '' using ($0==3?NaN:240):3 title '' with lines lc rgb '#d62728' lw 2, \ '' using ($0==3?NaN:0):1 title '' with lines lc rgb '#d62728' lw 2, \ # 连接Atom 47的点 '' using ($0==4?NaN:0):1 title 'Atom 47' with lines lc rgb '#9467bd' lw 2, \ '' using ($0==4?NaN:120):2 title '' with lines lc rgb '#9467bd' lw 2, \ '' using ($0==4?NaN:240):3 title '' with lines lc rgb '#9467bd' lw 2, \ '' using ($0==4?NaN:0):1 title '' with lines lc rgb '#9467bd' lw 2
Key Script Explanations:
- 极坐标基础:
set polar开启极坐标模式,这是绘制蜘蛛图的核心,每个指标对应一个固定角度。 - 刻度与网格:自定义
xtics把a/b/c放在0°/120°/240°的位置,环形网格线用来展示数值层级。 - 绘制逻辑:先在每个指标角度上画出所有原子的数值点,再单独把每个原子的三个点连接成闭合多边形,用不同颜色区分,同时通过
$0==N?NaN:X的方式确保只绘制对应原子的连线。
Step 3: Run the Script
把上面的脚本保存成spider_plot.gp,然后在终端运行:
gnuplot spider_plot.gp
就能得到你想要的蜘蛛图啦!如果需要调整样式(比如颜色、线宽、点样式),直接修改脚本里的lc rgb(颜色)、lw(线宽)、pt(点样式)参数即可。
内容的提问来源于stack exchange,提问作者murpholinox
相关产品推荐
相关产品推荐

