You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用gnuplot为单个箱线图设置独立填充颜色?

为每个箱线图单独设置颜色的gnuplot解决方案

问题说明

需要为每个独立箱线图单独设置颜色,原gnuplot脚本及数据文件如下:

原脚本

set terminal pngcairo enhanced font 'Arial,12' size 800,800
set output 'quartiles_candlestick.png'

set style data boxplot
set key off  # Remove the legend

# Set the color and outline style for boxplots
set style line 1 lc rgb "black" lt 1 lw 2  # Black outline with line type 1 and line width 2
set boxwidth 0.5  # Adjust the width of the boxes
set logscale y

# Set boxplot fill color
set style fill solid 0.5 border lc rgb "black"  # Fill with blue and black borders

# Set custom x-axis labels
set xrange [0.5:5.5]  # Adjust the range slightly to provide more space around the boxplots

plot 'output_statistics.txt' using 1:3:2:6:5:xticlabels(8) with candlesticks title 'Min-Max Whiskers' whiskerbars, \
     '' using 1:4:4:4:4 with candlesticks lt -1 notitle

数据文件output_statistics.txt结构

每行包含对应箱线图的颜色值:

#x min     Q1          median      q3          max     width   label   color
1 132.5 151.34 317.48 733.92 1314.00 582.57 ALC2 #440154
2 142.2 201.04 808.40 1485.33 2297.41 1284.29 ALC3 #31688E
3 2562.0 3073.71 4340.40 6080.67 6781.62 3006.96 ALC4 #1F9E89
4 121.3 166.85 400.00 778.06 2949.24 611.21 ALC5 #6CCE59
5 69.4 112.68 125.49 146.99 271.52 34.31 ALC6 #FDE725

修改后的脚本

set terminal pngcairo enhanced font 'Arial,12' size 800,800
set output 'quartiles_candlestick.png'

set style data boxplot
set key off
set boxwidth 0.5
set logscale y
set xrange [0.5:5.5]

# 取消全局填充样式,改为在plot命令中逐行指定
unset style fill

# 绘制带自定义颜色的箱体、须线和中位数线
plot 'output_statistics.txt' using 1:3:2:6:5:9:xticlabels(8) with candlesticks \
     lw 2 lc rgb variable fill solid 0.5 border lc rgb "black" whiskerbars notitle, \
     '' using 1:4:4:4:4:9 with candlesticks lw 2 lc rgb variable notitle

核心修改点

  • 取消全局填充样式:删除set style fill的全局设置,避免统一颜色覆盖自定义颜色。
  • 调用颜色列:在using语句中加入第9列(颜色列),通过lc rgb variable让gnuplot读取每行的颜色值。
  • 同步颜色设置:对箱体和中位数线都指定lc rgb variable,确保颜色匹配;同时保留黑色边框和指定线宽。

修改后,每个箱线图会自动使用数据文件中对应行的颜色填充,中位数线也会匹配对应颜色,实现独立着色效果。


内容的提问来源于stack exchange,提问作者BLaubach

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 17:03:23