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

如何在gnuplot中为月度自动化脚本设置固定x轴范围[1:31]?

问题:Gnuplot固定月度图表X轴为1-31日期范围

我有一个处理多月份数据的自动化脚本,希望每个月度图表的x轴始终显示[1:31]范围,即对应当月的YYYY-MM-01至YYYY-MM-31,即便部分月份(如二月)无月末数据也无妨。但因数据为时间格式,直接设置[1:31]无效,且无法在脚本中写死具体日期(每月日期不同)。

数据为每月单独文件,格式示例:

2022-07-01,00:00:16,27.3,3,28.0,9.0,995.6
2022-07-01,00:05:16,27.3,3,28.0,9.0,995.5
...
2022-07-31,23:58:16,27.1,3,27.9,11.9,995.0

当前用于修改x轴的gnuplot代码:

set xdata time
set timefmt '%Y-%m-%d,%H:%M:%S'
set format x "%d"
#set xrange ["01":"31"]
set xtics 21600*4*7
set mxtics 7
set grid xtics
set grid mxtics

希望x轴仅显示日期,且固定为1-31以方便月度对比,请问该如何正确设置x轴范围?


解决方案

核心思路是从数据文件中提取当月年份和月份,动态生成当月1号和下月1号的时间戳,以此作为x轴范围(下月1号的前一刻就是当月最后一天的23:59:59,刚好覆盖到31号的显示范围)。

步骤1:提取当月的年和月

通过stats命令读取数据文件第一行,截取日期中的年、月信息:

# 读取数据文件第一行,提取年、月
stats 'data_file.csv' using (strcol(1)) nooutput
first_date = strcol(1)
year = substr(first_date, 1, 4)
month = substr(first_date, 6, 2)

步骤2:动态生成x轴范围

利用提取的年、月构造当月1号和下月1号的时间字符串,转成时间戳后设置xrange:

# 构造当月1号0点的时间字符串
start_time = sprintf("%s-%s-01,00:00:00", year, month)
# 计算下月日期:处理12月跨年情况
if (month == "12") {
    next_year = sprintf("%d", int(year)+1)
    next_month = "01"
} else {
    next_year = year
    next_month = sprintf("%02d", int(month)+1)
}
# 构造下月1号0点的时间字符串作为x轴结束边界
end_time = sprintf("%s-%s-01,00:00:00", next_year, next_month)

# 设置x轴范围
set xrange [start_time:end_time]

步骤3:调整刻度显示(可选)

如果需要让刻度精准对应1、5、10...31号,可以手动指定刻度位置,避免自动刻度偏移:

# 手动设置X轴刻度为1-31号,间隔5天
set xtics ("1" timefmt("%Y-%m-%d,00:00:00", start_time), \
           "5" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-05,00:00:00", year, month)), \
           "10" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-10,00:00:00", year, month)), \
           "15" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-15,00:00:00", year, month)), \
           "20" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-20,00:00:00", year, month)), \
           "25" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-25,00:00:00", year, month)), \
           "31" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-31,00:00:00", year, month)))

完整示例代码

整合上述逻辑后的完整脚本:

set xdata time
set timefmt '%Y-%m-%d,%H:%M:%S'
set format x "%d"
set grid xtics
set grid mxtics

# 替换为你的数据文件名
data_file = "2022-07-data.csv"

# 提取当月年、月
stats data_file using (strcol(1)) nooutput
first_date = strcol(1)
year = substr(first_date, 1, 4)
month = substr(first_date, 6, 2)

# 生成x轴范围
start_time = sprintf("%s-%s-01,00:00:00", year, month)
if (month == "12") {
    next_year = sprintf("%d", int(year)+1)
    next_month = "01"
} else {
    next_year = year
    next_month = sprintf("%02d", int(month)+1)
}
end_time = sprintf("%s-%s-01,00:00:00", next_year, next_month)
set xrange [start_time:end_time]

# 调整刻度(可选)
set xtics ("1" timefmt("%Y-%m-%d,00:00:00", start_time), \
           "5" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-05,00:00:00", year, month)), \
           "10" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-10,00:00:00", year, month)), \
           "15" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-15,00:00:00", year, month)), \
           "20" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-20,00:00:00", year, month)), \
           "25" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-25,00:00:00", year, month)), \
           "31" timefmt("%Y-%m-%d,00:00:00", sprintf("%s-%s-31,00:00:00", year, month)))

# 绘图命令
plot data_file using 1:3 with lines title "Temperature"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 14:50:31