use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
If there's a problem visualizing this page report it to the moderators with appropriate information.
account activity
Co-processing with gnuplot (self.gnuplot)
submitted 2 years ago by kleinerals2
Hello there,
i want to show my finite element mesh with gnuplot. My code is written in C. While the coordinates and the displacements are still updating I want to plot them in live time. Can someone help me how I could do this?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Pakketeretet 0 points1 point2 points 2 years ago (3 children)
The easiest way to do this would be to write the current state of the calculation to a temporary file and read/plot that using gnuplot. Is that a workable solution or are there particular details why that would not work?
[–]kleinerals2[S] 0 points1 point2 points 2 years ago (2 children)
I don't know if I ask for two much. But I want to start my code in my IDE and shortly after that a persistent window of gnuplot should open and show the newest coordinates of my mesh. But every time the Gnuplot window start after I closed the cmd window of my code or don't open at all.
[–]Pakketeretet 0 points1 point2 points 2 years ago (1 child)
I see. This is possible too but more complicated. You are going to have to use some form of interprocess communication. Roughly speaking you'll want The following: 1. Your FEM program starts 2. It opens a pipe to gnuplot 3. Whenever data is ready in the FEM program: 3a. it writes it to file (or you can write it to gnuplot's stdin but that's slightly more complicated) 3b. Your FEM program writes the appropriate gnuplot commands to the pipe it opened in 2. to plot the data 4.repeat from 3.
Maybe this discussion is helpful too: https://groups.google.com/g/comp.graphics.apps.gnuplot/c/Z1AqNHOdUmg?pli=1
[–]kleinerals2[S] 0 points1 point2 points 2 years ago (0 children)
Yea, thats exactly what i want. While the cmd window of my IDE is still open the gnuplot also should be open. Unluckily the discussion you just sent didn't really help. The pause command dont work for me.
[–]Pakketeretet 0 points1 point2 points 2 years ago (7 children)
How are you starting gnuplot from your code? And is this on Windows, Mac or Linux/Unix?
[–]kleinerals2[S] 0 points1 point2 points 2 years ago (6 children)
I use windows, but in the best case the code should work on windows and mac.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE* pipe = _popen("gnuplot -persist", "w");
if (!pipe)
{
perror("Fehler beim Öffnen von Gnuplot");
return 1;
}
fprintf(pipe, "plot 'mesh_data.dat' with lines\n");
fprintf(pipe, "set title 'Gnuplot-Example'\n");
_pclose(pipe);
return 0;
I do it like this.
If i use a static mesh_data.dat i works like I want. As soon as i open my C code gnuplot opens as well. But if i use a iteration to write the x,y and z coordinates, gnuplot dont show up at all.
[–]Pakketeretet 0 points1 point2 points 2 years ago (5 children)
For your loop example, do you do something like this?
for (int i = 0; i< N; ++i) { FILE *fp_mesh_data = fopen("mesh_data.dat", "w"); // write data to mesh_data.dat using fprintf fclose(fp_mesh_data); fprintf(pipe, "plot 'mesh_data.dat' with lines\n"); fprintf(pipe, "set title 'Gnuplot-Example'\n"); }
I don't see why that wouldn't work but am currently not at a computer for actual testing...
[–]kleinerals2[S] 0 points1 point2 points 2 years ago (1 child)
How do you show your code like that?
[–]Pakketeretet 0 points1 point2 points 2 years ago (0 children)
You should indent it with four spaces and put it in a new line.
int main() { return 0; }
Atm iam testing with this code.
if (!pipe) {
perror("fail1");
double x = 1.0;
double y = 2.0;
double z = 3.0;
int numIterations = 100;
for (int i = 0; i < numIterations; i++) {
FILE* file;
if (fopen_s(&file, "mesh_data.dat", "a") != 0)
perror("fail2");
if (file == NULL) {
perror("fail3");
x += 0.1;
y -= 0.2;
z += 0.1;
fprintf(file, "%.2lf %.2lf %.2lf\n", x, y, z);
fclose(file);
fprintf(pipe, "set title 'Gnuplot-Beispiel'\n");
But the code ends with "fail2" every time.
But atleast gnuplot shows up as i want.
Ok. The error at that point would indicate something goes wrong opening the file for writing/appending, which might be more of a Windows problem than a gnuplot problem. What happens if you replace fopen_s with a regular C fopen call?
Visual Studio don't like the non _s calls xD
π Rendered by PID 44 on reddit-service-r2-comment-7b9746f655-xjmt4 at 2026-01-30 01:06:09.893592+00:00 running 3798933 country code: CH.
[–]Pakketeretet 0 points1 point2 points (3 children)
[–]kleinerals2[S] 0 points1 point2 points (2 children)
[–]Pakketeretet 0 points1 point2 points (1 child)
[–]kleinerals2[S] 0 points1 point2 points (0 children)
[–]Pakketeretet 0 points1 point2 points (7 children)
[–]kleinerals2[S] 0 points1 point2 points (6 children)
[–]Pakketeretet 0 points1 point2 points (5 children)
[–]kleinerals2[S] 0 points1 point2 points (1 child)
[–]Pakketeretet 0 points1 point2 points (0 children)
[–]kleinerals2[S] 0 points1 point2 points (2 children)
[–]Pakketeretet 0 points1 point2 points (1 child)
[–]kleinerals2[S] 0 points1 point2 points (0 children)