9

I have a data-set that consist of edges and colors, and I want to plot them on a web-like manner, with lines and circles such as the picture below, and possibly with cluster coloring.

Graph example

The data is organized like this:

point1a_x point1a_y color
point1b_x point1b_y color

point2a_x point2a_y color
point2b_x point2b_y color
(...)

point2n_x point2n_y color
point2n_x point2n_y color

How would I go about doing it on gnuplot?

rgcalsaverini
  • 626
  • 6
  • 16

3 Answers3

9

Okay, so I figured it out myself and I'll leave the details here to help anyone with the same questions.

Single color graph with labels on the nodes:

This will generate a graph much like the one on the question, with lines connecting circles with labels inside.

Single color graph with labels on the nodes

plot 'edges.dat' u 1:2 with lines lc rgb "black" lw 2 notitle,\
'edges.dat' u 1:2:(0.6) with circles fill solid lc rgb "black" notitle,\
'edges.dat' using 1:2:($0) with labels tc rgb "white" offset (0,0) font 'Arial Bold' notitle

With little changes it can exaclty match the one on the question picture.

enter image description here

plot 'edges.dat' u 1:2 with lines lc rgb "black" lw 2 notitle,\
'edges.dat' u 1:2:(0.8) with circles linecolor rgb "white" lw 2 fill solid border lc lt 0 notitle, \
'edges.dat' using 1:2:($0) with labels offset (0,0) font 'Arial Bold' notitle

Cluster-colored graph:

Cluster-colored graph

unset colorbox

set palette model RGB defined ( 0 0 0 0 , 1 1 0 0 , 2 1 0.9 0, 3 0 1 0, 4 0 1 1 , 5 0 0 1 , 6 1 0 1 )

plot 'edges.dat' u 1:2:3 with lines lc palette notitle,\
'edges.dat' u 1:2:(0.15):3 with circles fill solid palette notitle

The data used on all plots follow this structure:

21.53 9.55 0
24.26 7.92 0

5.63 3.23 1
2.65 1.77 1

5.63 3.23 0
4.27 7.04 0

(...)
rgcalsaverini
  • 626
  • 6
  • 16
  • There is only one problem tho... If the point have multiple lines, it gets plotted twice and with different labels... Any workarounds? – rgcalsaverini Dec 05 '13 at 17:44
  • I managed to find an workaround; See my other answer. The tldr is that you specify the labels in the input file instead of telling gnuplot to compute them. – hugomg Oct 16 '15 at 15:10
4

The accepted answer didn't quite work out for me. Here is how I had to change it:

The format of the input file

# A vertex has 3 fields: x coordinate, y coordnate and the label
# An edge consists of two points in consecutive lines
# There must be one or more blank lines between each edge.

21.53 9.55 A
24.26 7.92 B

5.63 3.23 C
2.65 1.77 D

5.63 3.23 C
4.27 7.04 E

#...

The big difference compared to the other answer is that the labels belong to vertices, not edges.

Also note that I changed the labels to letters instead of numbers. Labels can be any string and this makes it clearer that they are not sequential indexes in the example.

The plotting command

plot \
  'edges.dat' using 1:2       with lines lc rgb "black" lw 2 notitle,\
  'edges.dat' using 1:2:(0.6) with circles fill solid lc rgb "black" notitle,\
  'edges.dat' using 1:2:3     with labels tc rgb "white" offset (0,0) font 'Arial Bold' notitle

Big change here is that now when plotting the labels we plot the 3rd field instead of the $0 field, which is a sequential number.

Community
  • 1
  • 1
hugomg
  • 68,213
  • 24
  • 160
  • 246
  • Though the user did not include a field for the node name, this should IMO be the accepted answer - for the sake of completeness. By the way, is there any way to turn this into a directed graph, and would it also be possible to choose the line style for each edge? – wtf8_decode Dec 28 '15 at 23:10
  • @wtf8_decode yes, you could also individually choose the linestyle for each edge, but this would blow up the script a "little". If you are interested, I can adapt the script in my answer. – theozh Nov 16 '22 at 09:05
1

I came across this question and thought the data input can be made more user-friendly. In case you need to change x,y coordinates of a certain point you probably don't want to search through all connectors and change the coordinates everywhere accordingly. So, instead the example below is working with node IDs and connecting lines between two IDs.

The x,y coordinates and colors are stored in a string and the function strstrt() is used to create a lookup. The script below is a starting point and can be expanded, e.g. dashed lines, different point shapes, string labels, etc.

The datafile looks like this:

  • first block: IDs with x,y coordinates and color
  • second block, separated by two(!) blank lines: IDs to be connected and color of the line.

Data: SO20406346.dat

# ID  x     y     PointColor
  1   2.0   8.0   0xffaaaa
  2   6.0   9.0   0xaaffaa
  3   9.0   6.0   0xaaaaff
  4   5.0   5.0   0xffaaff
  5   6.0   2.0   0xffffaa
  6   1.0   1.0   0xaaffff
 73   9.2   1.3   0xcccccc
  A   8.0   8.0   0xcccccc
 XY   2.0   4.0   0xcccccc


# ID1 ID2   LineColor
  1   4     0x0000ff
  2   4     0x000000
  3   4     0x00ff00
  4   4     0x000000
  5   4     0x000000
  6   5     0xff0000
 73   3     0xcccccc
 73   4     0xcccccc
 73   5     0xcccccc
  A   2     0xcccccc
  A   3     0xcccccc
 XY   1     0xcccccc
 XY   4     0xcccccc
 XY   6     0xcccccc

Edit: changed to work with string "IDs" as well.

Script: (works for gnuplot>=4.6.0, March 2012)

### plot a node graph
reset

FILE = "SO20406346.dat"

IdIdxs = XYs = ' '
stats FILE u (IdIdxs=IdIdxs.sprintf("%s:%d ",strcol(1),$0), \
              XYs=XYs.sprintf("%g %g ",$2,$3)) index 0 nooutput
Px(i) = real(word(XYs,2*i+1))
Py(i) = real(word(XYs,2*i+2))

getIdx(col) = (c0=strstrt(IdIdxs,sprintf(" %s:",strcol(col))), \
              c1=strstrt(IdIdxs[c0+1:],' ')+c0, \
              s0=IdIdxs[c0:c1], c2=strstrt(s0,':'), int(s0[c2+1:]))

set size ratio 1
set key noautotitle
set offsets 0.25,0.25,0.25,0.25

plot FILE index 1 u (idx0=getIdx(1),x0=Px(idx0)):(y0=Py(idx0)): \
         (idx1=getIdx(2),Px(idx1)-x0):(Py(idx1)-y0):3 w vec lw 2 lc rgb var nohead, \
       '' index 0 u 2:3:4 w p pt 7 ps 6 lc rgb var, \
       '' index 0 u 2:3 w p pt 6 ps 6 lc rgb "black", \
       '' index 0 u 2:3:1 w labels
### end of script

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72