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

如何在gnuplot中为特定时间段添加灰色背景标记?

在Gnuplot中自动标记时间序列图的冬季月份灰色区域

方法1:用set object rectangle循环生成(适合已知年份范围)

如果你的数据覆盖的年份范围确定,可通过循环创建矩形对象标记冬季区域。假设冬季定义为前一年12月至当年2月(北半球通用定义),示例代码如下:

# 配置时间格式(适配你的数据时间列,示例为YYYY-MM-DD)
set xdata time
set timefmt "%Y-%m-%d"
set format x "%Y"

# 定义数据覆盖的年份区间
start_year = 2018
end_year = 2023

# 设置灰色填充样式
set style fill solid 0.3 noborder
set object fillstyle solid 0.3 fillcolor rgb "#cccccc"

# 循环生成每个冬季的矩形区域
do for [y=start_year:end_year-1] {
    # 计算冬季起止时间:y年12月1日 到 y+1年3月1日(覆盖12、1、2月)
    winter_start = strptime("%Y-%m-%d", sprintf("%d-12-01", y))
    winter_end = strptime("%Y-%m-%d", sprintf("%d-03-01", y+1))
    # 创建矩形,Y轴范围自动获取(先绘制一次数据得到GPVAL_Y_MIN/MAX)
    set object rectangle from winter_start, GPVAL_Y_MIN to winter_end, GPVAL_Y_MAX
}

# 首次绘制获取Y轴自动范围(若Y范围固定,可直接写死数值跳过此步)
plot "your_data.csv" using 1:2 with lines notitle
# 重新绘制,叠加数据线条与灰色区域
replot "your_data.csv" using 1:2 with lines title "数据集"

方法2:用辅助数据文件生成填充区域(适合动态年份/复杂范围)

如果数据年份不固定,或冬季定义更灵活,可先准备一个辅助文件winters.dat,每行存储一组冬季起止时间:

2018-12-01 2019-03-01
2019-12-01 2020-03-01
2020-12-01 2021-03-01

再通过filledcurves样式绘制填充区域:

set xdata time
set timefmt "%Y-%m-%d"
set format x "%Y"

set style fill solid 0.3 noborder

# 先绘制灰色填充区域,再叠加数据线条
plot "winters.dat" using 1:(GPVAL_Y_MIN):2:(GPVAL_Y_MAX) with filledcurves x1x2 fillcolor rgb "#cccccc" notitle, \
     "your_data.csv" using 1:2 with lines title "数据集"

细节调整

  • 冬季月份:若为南半球冬季(6-8月),只需修改时间字符串的月份部分即可。
  • 填充效果:调整solid后的数值(0为全透明,1为不透明),fillcolor可替换为其他灰色代码(如#eeeeee为浅灰)。
  • 图层顺序:若灰色区域遮挡数据线条,把填充区域的plot命令移到数据线条命令之后即可(Gnuplot按绘制顺序叠加图层)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.11 18:42:10