Search code examples
mathgnuplot

gnuplot: plot points only on natural x values


I'm using this minimal sample:

set xrange [0:10]
plot x**2

This produces a nice smooth graph. However, I'd like that there are only points plotted on natural values for x (0, 1, 2, ..., 10), making the chart 'rigid', something like this:

enter image description here

Is there a possibility to do this without a data file?

I know about Gnuplot x-axis resolution, however, if I would use that, I would need to tweak the sample resolution every time I change the xrange.


Solution

  • using shell

    Instead of a file, you can pipe a shell command to give you the points you want :

    plot "<seq 0 10" using 1:($1**2) w lp

    portability ?

    See this documentation : http://www.chemie.fu-berlin.de/chemnet/use/info/gnuplot/gnuplot_13.html#SEC53, I quote :

    On some computer systems with a popen function (UNIX), the datafile can be piped through a shell command by starting the file name with a '<'.

    Apparently this also works on some windows systems (with cygwin), though other windows platform fail with a miserable error (see below). This seems to be in that case because gnuplot opens a windows batch shell.

    If however you try to call a shell command through the system("command") way, you get a more explicit error (at least on my windows) :

    gnuplot> plot "<echo 0 1 2 3 4 5 6 7 8 9 10" using 1:($1**2) w lp  \
             # fails, implying the < syntax doesn't even exist
         warning: Skipping unreadable file "<echo 0 1 2 3 4 5 6 7 8 9 10"
         No data in plot
    
    gnuplot> plot system("echo 0 1 2 3 4 5 6 7 8 9 10") using 1:($1**2) w lp \
             # fails with proper warning, then tries to reuse previous file
         warning: system() requires support for pipes
         warning: Skipping unreadable file "<echo 0 1 2 3 4 5 6 7 8 9 10" 
         No data in plot
    
    gnuplot> !pause # shows a windows batch window