Search code examples
matrixgnuplotcallstore

How can I store and access matrix elements in gnuplot?


I'm trying to make 19x19 hexagonal lattice and each lattice contains a cylinder with different colors described as 'hexagon.dat'.

2-> Cylinder colored with red

1-> Cylinder colored with green

hexagon.dat

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 2
2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2
2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 2
2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 2
2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2
2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2
2 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2
2 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2
2 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2
2 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

But currently, since I don't know how to store and access the matrix data of hexagon.dat, There are only green cylinders This is my script for gnuplot

script

set term X11 persist title "test" size 1000, 1000

P = 0.78
pin_id = 0

do for [pin_ix=-9:9]{
do for [pin_iy=-9:9]{

cx = pin_ix*P + pin_iy*(-P/2)
cy = pin_iy*sqrt(3)/2*P
pin_id = pin_id + 1
set object pin_id poly from cx-P/2, cy+P/2/sqrt(3) \
                       to cx, cy+P/sqrt(3) \
                       to cx+P/2, cy+P/2/sqrt(3) \
                       to cx+P/2, cy-P/2/sqrt(3) \
                       to cx, cy-P/sqrt(3) \
                       to cx-P/2, cy-P/2/sqrt(3) \
                       to cx-P/2, cy+P/2/sqrt(3) \
fs solid fc rgb "red"


pin_id = pin_id + 1
set object pin_id circle at cx, cy size 0.3275 \
fs solid fc rgb "green"
}
}

set size ratio 1.0
set xr [-11:11]
set yr [-11:11]


plot 1/0

I'm waiting for your helps.


Solution

  • You can use plot ... with circles to plot the cylinders. Then you can select the color with lc variable:

    P = 0.78
    pin_id = 0
    
    do for [pin_ix=-9:9]{
    do for [pin_iy=-9:9]{
    
    cx = pin_ix*P + pin_iy*(-P/2)
    cy = pin_iy*sqrt(3)/2*P
    pin_id = pin_id + 1
    set object pin_id poly from cx-P/2, cy+P/2/sqrt(3) \
                           to cx, cy+P/sqrt(3) \
                           to cx+P/2, cy+P/2/sqrt(3) \
                           to cx+P/2, cy-P/2/sqrt(3) \
                           to cx, cy-P/sqrt(3) \
                           to cx-P/2, cy-P/2/sqrt(3) \
                           to cx-P/2, cy+P/2/sqrt(3) \
    fs solid fc rgb "red"
    
    
    pin_id = pin_id + 1
    }
    }
    
    set size ratio 1.0
    set xr [-11:11]
    set yr [-11:11]
    
    set style fill solid noborder
    set linetype 1 lc rgb 'red'
    set linetype 2 lc rgb 'green'
    plot 'hexagon.dat' matrix using (($1-9)*P + ($2-9)*(-P/2)):(($2-9)*sqrt(3)/2 * P):(0.3275):3 with circles lc variable 
    

    enter image description here

    With version 5.0 you could use stats to extract the matrix dimension:

    stats 'hexagon.dat' matrix
    N = int(STATS_size_x)
    do for [pin_ix=-(N/2):((N-1)/2)] {
    ...