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

如何在Gnuplot中实现X轴日期与小时的双重命名并按日期分组绘制独立折线图

Let's fix your issues step by step:

First, the error undefined variable: rowstacked happens because rowstacked is not a valid option for set style lines in gnuplot. That parameter belongs to histogram styles, not line plots—so we can just delete that line entirely.

Now, let's address your core requirements: combining date + hour for the X-axis (grouped by date), and drawing independent line plots (no stacking) to check for intersections.

Corrected Gnuplot Code

reset session

# Track the previous date to handle empty date cells in your data
prev_date = ""

# Basic plot styling
set key left box
set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb"#ffffff" behind
set datafile separator tab
set grid x y my
set xlabel "Date & Time"
set ylabel "Sensor Values"
set title "Sensor Datas"

# Configure X-axis as a time axis
set xdata time
# Define the format of our combined date+time strings (matches your data)
set timefmt "%m/%d/%Y %H:%M %p"
# Format X-axis labels to show date on one line, hour on the next (groups by date)
set format x "%m/%d\n%I:%M %p"
# Rotate labels to prevent overlap
set xtics rotate by -45 right

# Plot each sensor's data as an independent line
plot "./Sensor Datas.txt" u (strcol(1) eq "" ? prev_date : prev_date=strcol(1), timecolumn(0, sprintf("%s %s", prev_date, strcol(2)))):3 t "a" w l lw 2, \
     "./Sensor Datas.txt" u (strcol(1) eq "" ? prev_date : prev_date=strcol(1), timecolumn(0, sprintf("%s %s", prev_date, strcol(2)))):4 t "b" w l lw 2, \
     "./Sensor Datas.txt" u (strcol(1) eq "" ? prev_date : prev_date=strcol(1), timecolumn(0, sprintf("%s %s", prev_date, strcol(2)))):5 t "c" w l lw 2, \
     "./Sensor Datas.txt" u (strcol(1) eq "" ? prev_date : prev_date=strcol(1), timecolumn(0, sprintf("%s %s", prev_date, strcol(2)))):6 t "d" w l lw 2, \
     "./Sensor Datas.txt" u (strcol(1) eq "" ? prev_date : prev_date=strcol(1), timecolumn(0, sprintf("%s %s", prev_date, strcol(2)))):7 t "e" w l lw 2, \
     "./Sensor Datas.txt" u (strcol(1) eq "" ? prev_date : prev_date=strcol(1), timecolumn(0, sprintf("%s %s", prev_date, strcol(2)))):8 t "f" w l lw 2

Key Explanations:

  1. Fixing the rowstacked Error:
    The line set style lines rowstacked was invalid—gnuplot doesn't support this for line plots. Removing it eliminates the error.

  2. X-axis Date+Hour Grouping:

    • We use a prev_date variable to "carry over" the date from the previous row when the current row's date cell is empty.
    • Combine the date and hour into a single time string, then convert it to a timestamp using timecolumn() so gnuplot can treat the X-axis as a continuous time scale.
    • set format x "%m/%d\n%I:%M %p" splits the label into two lines: date on top, hour below, creating a natural date-based grouping.
  3. Independent Line Plots:
    Each sensor (a-f) is plotted as a separate line with w l (lines) and no stacking. The lw 2 option makes lines thicker for easier visibility of intersections.

Notes on Your Data File:

Ensure your Sensor Datas.txt uses tab separators (matching set datafile separator tab), and that empty date cells are truly blank (not filled with spaces) so the strcol(1) eq "" check works correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 12:57:39