在gnuplot绘图时,能否直接从原始数据中减去平滑后的数据?
Absolutely—you don’t need external tools to do this! Gnuplot has built-in functionality to handle this operation directly. Here’s how to modify your existing command to plot the difference between your original sensor data and its acsplines-smoothed version:
Approach 1: Use a Temporary File for Smoothed Data
This is the most reliable method, compatible with most gnuplot versions. First, we’ll generate the smoothed data and save it to a temporary file, then calculate the difference by matching x-values between the original and smoothed datasets:
# First, capture the smoothed data to a temporary file set table 'temp_smooth.dat' plot ["17.12.2020 08:00:00":"18.12.2020 20:00:00"] 'data3-17-28.csv1' using 4:5 smooth acsplines unset table # Configure time formatting for your x-axis (since your x is a timestamp) set xdata time set timefmt "%d.%m.%Y %H:%M:%S" set format x "%d.%m %H:%M" # Adjust this to your preferred display format # Plot original, smoothed, and their difference plot ["17.12.2020 08:00:00":"18.12.2020 20:00:00"] \ 'data3-17-28.csv1' using 4:5 title 'Sensor 3' with lines lc rgb '#0066ff', \ 'temp_smooth.dat' using 1:2 title 'Smoothed' with lines lc rgb '#ff3333', \ '< join data3-17-28.csv1 temp_smooth.dat' using 1:($2 - $4) title 'Original - Smoothed' with lines lc rgb '#00cc66'
The join command merges the original and smoothed files by their x-values (assuming both are sorted chronologically, which your timestamp data should be). The $2 - $4 calculates the difference between the original y-value (column 2 of the merged output) and the smoothed y-value (column 4).
Approach 2: Use an In-Memory Data Block (Gnuplot 5.0+)
If you’re using gnuplot 5.0 or later, you can avoid creating a physical temporary file by storing the smoothed data in an in-memory data block:
# Save smoothed data to an in-memory block named $SMOOTHED set table $SMOOTHED plot ["17.12.2020 08:00:00":"18.12.2020 20:00:00"] 'data3-17-28.csv1' using 4:5 smooth acsplines unset table # Configure time formatting set xdata time set timefmt "%d.%m.%Y %H:%M:%S" set format x "%d.%m %H:%M" # Plot the datasets and their difference plot ["17.12.2020 08:00:00":"18.12.2020 20:00:00"] \ 'data3-17-28.csv1' using 4:5 title 'Sensor 3' with lines, \ $SMOOTHED using 1:2 title 'Smoothed' with lines, \ '< join data3-17-28.csv1 <(echo "$SMOOTHED")' using 1:($2 - $4) title 'Original - Smoothed' with lines
This works similarly to the first method, but uses a data block instead of a file, keeping your workspace clean.
Key Notes:
- Ensure your original data is sorted by the x-axis (timestamp) for
jointo work correctly. - If you don’t need to plot the original and smoothed datasets separately, you can omit those lines and only plot the difference.
内容的提问来源于stack exchange,提问作者Sergei

