3

I have this very simple, working gnuplot code:

splot x * x

This plots a surface from -10 < x < 10 and -10 < y < 10.

plot of z = x^2

I want to plot the same surface from -10 < x < 10 and -10 < y < -9 HOWEVER I still want the graph's axes to use -10 < x < 10 and -10 < y < 10. The axes bounds should not change, but the range of values plotted should be smaller.

In other words, the surface should look like a thin band in one small section of the plot. The rest should be empty space. Below is a very poor effort at creating what I want using the graph I have.

enter image description here

I have tried manipulating yrange, but this changes BOTH the surface bounds and the axes bounds. I have also tried disabling autoscale but I'm unsure what to do next.

sudo rm -rf slash
  • 1,156
  • 2
  • 16
  • 27

4 Answers4

3

In current gnuplot (version 5.2.4) you can separate the sampling range from the axis range whether or not you enable parametric mode. See example in the on-line demo set: sampling.html (see plot #9)

Ethan
  • 13,715
  • 2
  • 12
  • 21
2

One way to do this is through parametric mode, which gives you a separate set of coordinates (u and v) to set ranges for

f(x,y) = x*x

set xrange [-10:10]
set yrange [-10:10]
set urange [-10:10]
set vrange [-10:-9]
set parametric
splot u, v, f(u,v)

enter image description here

Alternatively you can achieve the same effect by using the ++ special filename instead of parametric mode:

f(x,y) = x*x

set xrange [-10:10]
set yrange [-10:10]
set urange [-10:10]
set vrange [-10:-9]

splot "++" u 1:2:(f($1,$2)) w l
user8153
  • 4,049
  • 1
  • 9
  • 18
1

Using a programming language with gnuplot might give better control over plots.

Here is an example using c and a script.

plot.c

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

int main()
{
   int x, y;
   double z;

   for (y=-10; y <= 10; y++)
   {
      for (x=-10; x <= 10; x++)
      {
         z = x * 0.025;
         printf("%f %f %f\n", (double)x, (double)y, 100 - cos(z * M_PI*2) * 100);
      }
      printf("\n");
   }

   fflush(stdout);
   return 0;
}

plot.bat

gcc plot.c -o prog.exe -lm
prog > data.txt
echo splot "data.txt" using 1:2:3 with lines | gnuplot -p

plot.sh

gcc plot.c -o prog -lm
./prog > data.txt
echo 'splot "data.txt" using 1:2:3 with lines' | gnuplot -p

Here is an example using python and a script.
It will output:
Using python and gnuplot

plot.py

import math

print("\"Surface One\"")
for y in range(-10, 10+1):
   for x in range(-10, 10+1):
      z = x * 0.025
      print("%f %f %f 1" % (float(x), float(y), 100 - math.cos(z * math.pi*2) * 100))
   print("")
print("")

print("\"Surface Two\"")
for y in range(-10, -8+1):
   for x in range(-10, 10+1):
      z = x * 0.025
      print("%f %f %f 2" % (float(x), float(y), 100 - math.cos(z * math.pi*2) * 100))
   print("")
print("")

print("\"Surface Three\"")
for y in range(-10, 10+1):
   for x in range(-10, 10+1):
      z = x * 0.1
      print("%f %f %f 7" % (float(x)/2, float(y)/2, math.sin(z * math.pi*2) * 10))
   print("")
print("")

plot.bat

cd %~dp0
python plot.py > data.txt

echo ^
set terminal wxt size 570,420; ^
stats "data.txt" u 1:2 nooutput; ^
set border 0x7F linecolor rgb "#555555"; ^
set grid xtics linecolor rgb "#888888" linewidth 0.2 linetype 9; ^
set grid ytics linecolor rgb "#888888" linewidth 0.2 linetype 9; ^
set grid ztics linecolor rgb "#888888" linewidth 0.2 linetype 9; ^
set xrange [-10.0:10.0]; ^
set yrange [-10.0:10.0]; ^
set zrange [-10.0:100.0]; ^
splot for [IDX=0:STATS_blocks-1] "data.txt" i IDX using 1:2:3:4 with lines ^
lc variable title columnheader(1) | gnuplot -p

plot.sh

#!/bin/bash

python plot.py > data.txt

echo \
'set terminal wxt size 570,420; \
stats "data.txt" u 1:2 nooutput; \
set border 0x7F linecolor rgb "#555555"; \
set grid xtics linecolor rgb "#888888" linewidth 0.2 linetype 9; \
set grid ytics linecolor rgb "#888888" linewidth 0.2 linetype 9; \
set grid ztics linecolor rgb "#888888" linewidth 0.2 linetype 9; \
set xrange [-10.0:10.0]; \
set yrange [-10.0:10.0]; \
set zrange [-10.0:100.0]; \
splot for [IDX=0:STATS_blocks-1] "data.txt" i IDX using 1:2:3:4 with lines \
lc variable title columnheader(1)' | gnuplot -p
1

Example without explicit parametric

This was mentioned at: https://stackoverflow.com/a/51546029/895245 with a link to the docs, here is a minimal example:

set terminal png
set output 'splot-domain.png'

# Number of x and y samples.
set isosamples 10, 5

# Plotted domain.
set urange [-5.0 : 0.0]
set vrange [-5.0 : 5.0]

# Visible domain.
set xrange [-5.0 : 5.0]
set yrange [-5.0 : 5.0]

# Just to make plot look nicer.
set hidden3d
set xyplane at 0
set xlabel 'x'
set ylabel 'y'

splot '++' using 1:2:($2**2) with lines

GitHub upstream.

Output:

enter image description here

Tested on gnuplot 5.2 patchlevel 8.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985