如何在gnuplot中存储和访问矩阵元素?

时间:2015-02-17 08:30:53

标签: matrix gnuplot call store

我正在尝试制作19x19的六边形网格,每个网格都包含一个不同颜色的圆柱,描述为'hexagon.dat'。

2→气缸用红色着色

1→气缸用绿色着色

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

但是目前,由于我不知道如何存储和访问hexagon.dat的矩阵数据,只有绿色圆柱体 这是我的gnuplot脚本

脚本

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

我在等你的帮助。

2 个答案:

答案 0 :(得分:0)

您可以使用plot ... with circles绘制圆柱体。然后,您可以使用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

使用版本5.0,您可以使用stats来提取矩阵维度:

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

答案 1 :(得分:0)

NewHexData

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
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 1 1 2 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
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 1 1 2 2 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 1 1 2 2
2 1 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 1 2
2 2 2 1 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 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 2 2 1 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 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 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

脚本

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 size ratio 1.0
set xr [-11:11]
set yr [-11:11]

set style fill solid noborder
set linetype 1 lc rgb 'green'
set linetype 2 lc rgb 'red'
plot 'NewHexData' matrix using (($1-9)*P + ($2-9)*(-P/2)):(($2-9)*sqrt(3)/2 * P):(0.3275):3 with circles lc variable

由于我没有足够的声誉,我无法上传生成的图片。 无论如何我得到了这个数字。

http://imgur.com/vEpjdll

我非常感谢Christoph!