简体   繁体   English

gnuplot -- 绘图范围与轴范围不同

[英]gnuplot -- plot range different from axes range

I have this very simple, working gnuplot code:我有这个非常简单、有效的 gnuplot 代码:

splot x * x

This plots a surface from -10 < x < 10 and -10 < y < 10.这绘制了 -10 < x < 10 和 -10 < y < 10 的曲面。

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.我想从 -10 < x < 10 和 -10 < y < -9 绘制相同的表面,但是我仍然希望图形的轴使用 -10 < x < 10 和 -10 < y < 10。轴边界不应该变化,但绘制的值范围应该更小。

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.下面是使用我拥有的图表创建我想要的东西的一个非常糟糕的努力。

在此处输入图片说明

I have tried manipulating yrange , but this changes BOTH the surface bounds and the axes bounds.我试过操纵yrange ,但这会改变表面边界和轴边界。 I have also tried disabling autoscale but I'm unsure what to do next.我也试过禁用autoscale但我不确定下一步该怎么做。

In current gnuplot (version 5.2.4) you can separate the sampling range from the axis range whether or not you enable parametric mode.在当前的 gnuplot(版本 5.2.4)中,无论是否启用参数模式,您都可以将采样范围与轴范围分开。 See example in the on-line demo set: sampling.html (see plot #9)请参见在线演示集中的示例: sampling.html(参见图 #9)

One way to do this is through parametric mode, which gives you a separate set of coordinates ( u and v ) to set ranges for一种方法是通过parametric模式,它为您提供一组单独的坐标( uv )来设置范围

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)

在此处输入图片说明

Alternatively you can achieve the same effect by using the ++ special filename instead of parametric mode:或者,您可以通过使用++特殊文件名而不是parametric模式来实现相同的效果:

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

Using a programming language with gnuplot might give better control over plots.使用带有 gnuplot 的编程语言可能会更好地控制绘图。

Here is an example using c and a script.这是一个使用 c 和脚本的示例。

plot.c情节.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情节.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绘图.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.这是一个使用 python 和脚本的示例。
It will output:它会输出:
使用 python 和 gnuplot

plot.py绘图.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情节.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绘图.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

Example without explicit parametric没有显式parametric示例

This was mentioned at: https://stackoverflow.com/a/51546029/895245 with a link to the docs, here is a minimal example:这是在: https : //stackoverflow.com/a/51546029/895245中提到的,带有指向文档的链接,这是一个最小的示例:

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 . GitHub 上游.

Output:输出:

在此处输入图片说明

Tested on gnuplot 5.2 patchlevel 8.在 gnuplot 5.2 补丁级别 8 上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM