在 Gnuplot 中绘制代数生成的二维网格的最佳方法是什么?

What is the best way to plot an algebraically generated 2d grid in Gnuplot?

我生成了一个 x 值的二维数组和一个 y 值的数组。每个 i,j 索引对应一个 x,y 点。我想从这些点创建一个网格,但我不确定如何去做。我想用 fortran/GNUPLOT 来创建这个数字。输出应如下所示:

这是在 matlab 中创建的,我在其中分别绘制了每条垂直线和水平线。

我认为这个子例程应该可以满足您的需求:

    subroutine gnuplot_output(filename,ni,nj,x,y)
        character(len=*), intent(in) :: filename
        integer, intent(in) :: ni, nj
        real(wp), intent(in) :: x(ni,nj), y(ni,nj)
        
        integer :: unit, i, j

        open(newunit=unit,file=filename)

        do i = 1, ni-1
            do j = 1, nj-1
                write(unit,*) x(i,j), y(i,j)
                write(unit,*) x(i+1,j), y(i+1,j)
                write(unit,*) x(i+1,j+1), y(i+1,j+1)
                write(unit,*) x(i,j+1), y(i,j+1)
                write(unit,*) x(i,j), y(i,j)
                write(unit,*)
            end do
        end do

        close(unit)
    end subroutine

随意使用代码(任何许可证)。

为了生成闭合单元格,您需要输出 4 个单元格角坐标并重复第一个角坐标以使 gnuplot 绘制闭合线。对子例程的调用可能如下所示:

real(wp), allocatable :: x(:,:), y(:,:)
integer :: ni, nj

! ... allocate x and y and generate grids ...
! ... the shape(x) == [ni,nj], and shape(y) == [ni,nj]

call gnuplot_output("grid_cells.txt",ni,nj,x,y)

在 gnuplot 中你需要做的就是

plot "grid_cells.txt" w l

下图是在 gnuplot 中使用以下函数创建的:

这里是一个 gnuplot 版本。你的数据已经在数据文件中了吗?然后请显示一些示例数据线。代码需要一些更改。

代码:

### draw grid
reset session

# create some test data
set print $Data
    N=50.       # decimal point! otherwise gnuplot would do integer division
    base(x) = x<1 || x>2 ? 0 : -0.8*(x-1.5)**2+0.2
    do for [y=0:N] {
        y0=y/N
        do for [x=0:N] {
            x0=x/N*3
            y1=(1-base(x0))*y0+base(x0)
            print sprintf("%g %g",x0,y1)
        }
        print "\n"    # add empty line
    }
set print

set view map

splot $Data u 1:2:0 w l notitle
### end of code

结果: