如何在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:
Fixing the
rowstackedError:
The lineset style lines rowstackedwas invalid—gnuplot doesn't support this for line plots. Removing it eliminates the error.X-axis Date+Hour Grouping:
- We use a
prev_datevariable 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.
- We use a
Independent Line Plots:
Each sensor (a-f) is plotted as a separate line withw l(lines) and no stacking. Thelw 2option 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

