1

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.

Community
  • 1
  • 1

3 Answers3

1

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

sweber
  • 2,916
  • 2
  • 15
  • 22
  • I already mentioned `set samples` (see the link at the bottom), and why I don't want to use it. –  Nov 29 '14 at 12:20
  • Well that has stritcly the same effect as `plot x**2 with linespoints` on the last line, the + file isn't useful here. – Cimbali Nov 29 '14 at 12:21
1

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
Community
  • 1
  • 1
Cimbali
  • 11,012
  • 1
  • 39
  • 68
  • This works great, thanks! For future visitors, could you maybe add an explanation on how you built this command, or a reference to the documentation? :) –  Nov 29 '14 at 12:35
  • I'm afraid I looked for trickier ways to this, with conditions and such, which then led to me to this question : http://stackoverflow.com/questions/11187691/gnuplot-conditional-plotting-2-15-2-1-0-with-lines, which uses shell calls but no references – Cimbali Nov 29 '14 at 12:36
  • Hm, okay. By the way, do you know if this also works on windows? It works fine on _my_ windows, but I have MinGW and Cygwin installed. If not, a small note should be added to the answer. –  Nov 29 '14 at 12:38
  • Hah, it doesn't work on my windows, I had to test it on linux. I'll edit, elaborating what I can. – Cimbali Nov 29 '14 at 12:39
  • Thanks! I'll wait a bit to see if others come up with an other solution, and then probably accept your answer! –  Nov 29 '14 at 12:41
0

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

theozh
  • 22,244
  • 5
  • 28
  • 72