0

I was trying to learn plotting with gnu-plot trough a example with C programming but the following message keeps repeating when i try to plot:

"Warning: Skipping data file with no valid points"
"x rang is invalid"

I don't know what it meant with that. I compiled the file and it didn't give me any errors and this is from a already made example. Why exactly am i not able to plot?

The code is as follows:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double t = 0.0, x, y;
    FILE  *fp;
    fp = fopen("data6-7.txt","w");
    do
    {
        x = cos(2*M_PI*t);
        y = sin(2*M_PI*t);

        fprintf(fp, "%f %f %f\n", t, x, y);

        t = t + 0.01;
    } while (t <= 1.0);
    fclose(fp);

    return 0;
}
5
  • What c file did you try to plot, and why are there invalid characters in your code? And where are you calling gnuplot? Commented May 25, 2015 at 21:09
  • @iharob: that's the backslash character when using a shift-jis encoding Commented May 25, 2015 at 21:12
  • @iharob I don't know, i was using a example a teacher gave me, but it's not working. Commented May 25, 2015 at 21:17
  • Please post the example your teacher gave, and plotting with gnuplot from c is generally not necessary, since you can run the program and generate the data file and then invoke gnuplot from a bash script anyway. Commented May 25, 2015 at 21:19
  • Well....that's pretty much the whole example right there.after that it asks to type gnuplot in the terminal and plot the following: gnuplot> plot gdata6-7.txth using 1:2 Commented May 25, 2015 at 21:23

1 Answer 1

1

After you compile and run your program, you must create a plot file telling gnuplot how to read your data (much easier than cramming all option on the command line). Example for your case:

plot \
"data6-7.txt" using 1:2 title 'cos', \
"data6-7.txt" using 1:3 title 'sin'

Save it as myplot.plt. Then call gnuplot with:

gnuplot -p myplot.plt

The format of the .plt file should be fairly obvious. You can plot data from any file against data from any other file. In the case of only one data file (like here), the format is:

plot \
"filename" using (colum #):(column #) title 'sometitle', \
......

The plot command is one line (so line continuations are required). The (colum #):(column #) just says plot data from the first column # as the independent data against the next column # ad the dependent values.

There are many, many options/tweaks for plotting (plot to .png, axis labels, scaling, etc..). Take a look at the manual (and examples) online or usually in your /usr/share/gnuplot/4.X/gnuplot.pdf file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.