user1544337
user1544337

Reputation:

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.

Upvotes: 1

Views: 1541

Answers (3)

theozh
theozh

Reputation: 25734

Using a platform-dependent system command as in @Cimbali's solution for such a task wouldn't be my perferred solution.

Here is a gnuplot-only solution where you don't have to worry about portability. The OP doesn't want to tweak the sampling when the xrange changes, but if you want to change the xrange you have to change your script at some point in any case. So, simply use variables for the xrange and let gnuplot do the "tweaking" of the sampling which is fairly easy since you want only natural (or integer) numbers. Assumption is that xmin is an integer as well, if not, you could use ceil() for the samples and in the plotting command.

Script: (works with gnuplot>=4.4.0, March 2010)

### plot function only for integer numbers
reset

set xrange[xmin=0:xmax=10]
set samples (xmax-xmin)+1

plot '+' u 1:($1**2) w lp pt 7
### end of script

Result:

enter image description here

Upvotes: 0

sweber
sweber

Reputation: 2976

If you want points, you can use the pseudo-file "+". It is like a file containing a series of numbers in a single column. You have to set the xrange. The number of points is determined by set samples :

set xrange[-5:5]
set samples 11 # need 11 points: 0, 1, 2, ..., 10
plot "+" u 1:($1**2) with linespoints

EDIT:

First, it is true: you do not have to use '+' just for linespoints...

OK, what's about this:

plot '+' u (floor($1)):(floor($1)**2) smooth unique w lp

Still using the pseudo-file '+', it rounds down its values to the next integer. As log ans you ensure that the number of sample points is high enough that there's alway one number generated beween each integer (let's say twice the full yrange), it works. The smooth unique prevents multible points at the same coordinates. enter image description here

Upvotes: 1

Cimbali
Cimbali

Reputation: 11395

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

Upvotes: 1

Related Questions