如何用GnuPlot从数据特定区域生成热图?解决splot/every/using兼容性问题
Hey there! Let's tackle this GnuPlot heatmap problem step by step. Since you're on Windows (so we can skip Linux pipe tricks) and dealing with batches of 16 values that need to become 4×4 heatmaps, we've got two solid approaches—either reformatting your data for easier handling, or using GnuPlot's built-in row indexing to map your raw data to coordinates.
Option 1: Reformat Your Data with Coordinates (Recommended)
This is the most straightforward way because it lets GnuPlot directly recognize each data point's position in the 4×4 grid. Here's how to structure each batch:
For each frame's 16 values, add explicit x/y coordinates (x from 0-3, y from 0-3), and separate batches with a blank line:
0 0 val1 0 1 val2 0 2 val3 0 3 val4 1 0 val5 1 1 val6 1 2 val7 1 3 val8 2 0 val9 2 1 val10 2 2 val11 2 3 val12 3 0 val13 3 1 val14 3 2 val15 3 3 val16 0 0 val17 0 1 val18 ... 3 3 val32
GnuPlot Script for This Format
This script will loop through each batch (separated by blank lines, detected via index) and generate individual heatmap frames:
# Set up terminal and output pattern set terminal pngcairo size 400,400 enhanced set output "frame_%d.png" # Configure heatmap settings set pm3d map # Enable heatmap mode set xrange [-0.5:3.5] # Match 4x4 grid (add padding for cell borders) set yrange [-0.5:3.5] set cbrange [0:100] # Replace with your actual data value range set palette defined (0 "blue", 50 "yellow", 100 "red") # Customize colors as needed # Loop through all 4 frames (adjust the range [0:3] to match your total frames) do for [frame_idx=0:3] { set output sprintf("frame_%d.png", frame_idx) plot "SortedData.txt" index frame_idx using 1:2:3 with image notitle }
Option 2: Use Raw Data with Calculated Coordinates
If you don't want to reformat your data, you can use GnuPlot's row number variable ($0) to calculate x/y positions on the fly. The key fix here is that plot ... with image requires three columns (x, y, value)—your earlier commands failed because you only provided the value column.
GnuPlot Script for Raw One-Dimensional Data
This script uses every to select each 16-value batch, then computes x/y from the row index within the batch:
set terminal pngcairo size 400,400 enhanced set pm3d map set xrange [-0.5:3.5] set yrange [-0.5:3.5] set cbrange [0:100] # Update to your data range set palette defined (0 "blue", 50 "yellow", 100 "red") # Loop through 4 frames (adjust [0:3] to your total number of batches) do for [frame_idx=0:3] { start_row = frame_idx * 16 end_row = start_row + 15 set output sprintf("frame_%d.png", frame_idx) # Calculate x = (position in batch) %4, y = (position in batch) //4 plot "SortedData.txt" every ::start_row::end_row \ using (int(($0 - start_row) % 4)) : (int(($0 - start_row) / 4)) : 1 \ with image notitle }
Turning Frames into Animation (Windows)
Once you have all frame_0.png to frame_3.png, you can use a tool like ffmpeg (download the Windows version from its official site) to create a GIF or video. Open Command Prompt in the folder with your frames and run:
ffmpeg -framerate 2 -i frame_%d.png -loop 0 output.gif
-framerate 2: Sets animation speed to 2 frames per second-loop 0: Makes the GIF loop indefinitely
内容的提问来源于stack exchange,提问作者six02

