GNUplot无图像绘制故障:铝板温度扩散动态热力图问题排查
1. Fix Data Format for pm3d
GNUplot's pm3d requires structured grid data to render correctly. Your current code outputs all points in a single continuous list, which pm3d can't interpret as a valid grid. Here's how to resolve this:
If your data is a regular grid:
If datax and datay form a regular grid (e.g., nx points along the x-axis, ny points along the y-axis, with n = nx*ny), insert blank lines between each row of the grid. For example, if rows are grouped by constant y values:
int nx = ...; // Number of x points per row for (int i = 0; i < n; i++) { // Compute result and update U (see section 2 for critical fix here) fprintf(fp, "%f %f %f\n", datax[i], datay[i], U[i]); // Add blank line after each row to define grid structure if ((i + 1) % nx == 0) { fprintf(fp, "\n"); } } fprintf(fp, "e\n"); fflush(fp);
If your data is unstructured:
If points are scattered (not arranged in a regular grid), enable GNUplot's dgrid3d to interpolate the data into a grid before rendering. Add this command to your GNUplot setup:
fprintf(fp, "set dgrid3d 50,50 spline\n"); // Adjust 50,50 to your desired grid resolution
This creates a 50x50 grid and uses spline interpolation to fill in missing values, allowing pm3d to render the heatmap.
2. Fix Explicit Euler Simulation Bug
Your current update step modifies U[i] immediately after computing result[i], which leads to using mixed old/new values in subsequent calculations (violating explicit Euler's requirement to use only the previous time step's state). Fix this by computing all results first, then updating all U values:
for (double k = 0.0; k < 10; k = k + 0.01) { // First compute all result values using the current state of U for (int i = 0; i < n; i++) { result[i] = 0.0; for (int j = ia[i]; j < ia[i + 1]; j++) { result[i] += (-D) * U[j] * a[j]; } } // Then update all U values using the precomputed results for (int i = 0; i < n; i++) { U[i] += h * result[i]; } // Output the updated data to GNUplot fprintf(fp, "splot '-' with pm3d\n"); for (int i = 0; i < n; i++) { fprintf(fp, "%f %f %f\n", datax[i], datay[i], U[i]); // Add blank lines here if using a regular grid (as shown in section 1) } fprintf(fp, "e\n"); fflush(fp); }
3. Additional GNUplot Tweaks
- Verify your pm3d configuration is correct:
fprintf(fp, "set pm3d interpolate 10,10\n"); fprintf(fp, "set view map\n"); fprintf(fp, "unset key\n"); - If the plot still fails to render, force a redraw (some terminals require this):
fprintf(fp, "refresh\n");
By fixing the data format and simulation logic, your heatmap should render correctly and show the temperature diffusion over time.
内容的提问来源于stack exchange,提问作者Noah Dbc

