Search code examples
gnuplot

How to plot network by gnuplot


I have a list of more than 100 points. I'd like to plot a figure like this picture. The lines connect any two points whose distance is less than 3.

1.53   2.40
5.39   3.02
4.35   1.29
9.58   8.34
6.59   1.45
3.44   3.45
7.22   0.43
0.23   8.09
4.38   3.49

https://www.codeproject.com/Articles/1237026/Simple-MLP-Backpropagation-Artificial-Neural-Netwo enter image description here


Solution

  • You probably have to check every point against every other point whether the distance is less than your threshold. So, create a table with all these points, the vector between them and plot them with vectors. The following example creates some random points with random sizes and random colors.

    Code:

    ### Plot connections between points which are closer than a threshold
    reset session
    set size square
    
    # create some random test data
    set print $Data
        myColors = "0xe71840 0x4d76c3 0xf04092 0x47c0ad 0xf58b1e 0xe6eb18 0x59268e 0x36b64c"
        myColor(n) = int(word(myColors,n))
        do for [i=1:100] {
            print sprintf("%g %g %g %d", rand(0), rand(0), rand(0)*2+1, myColor(int(rand(0)*8)+1))
        }
    set print
    
    d(x1,y1,x2,y2) = sqrt((x2-x1)**2 + (y2-y1)**2)
    myDist = 0.2
    
    set print $Connect
        do for [i=1:|$Data|-1] {
            x1=real(word($Data[i],1))
            y1=real(word($Data[i],2))
            do for [j=i+1:|$Data|] {
                x2=real(word($Data[j],1))
                y2=real(word($Data[j],2))
                if (d(x1,y1,x2,y2)<myDist) { print sprintf("%g %g %g %g", x1, y1, x2-x1, y2-y1) }
            }
        }
    set print
    
    set key noautotitle
    plot $Connect u 1:2:3:4 w vec lc "grey" nohead, \
            $Data u 1:2:3:4 w p pt 7 ps var lc rgb var
    ### end of code
    

    Result:

    enter image description here