Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gnuplot plot 2D matrix with image, want to draw borders for each cell

Tags:

plot

gnuplot

I want to plot a 18x18 matrix with gnuplot. Here is my codes:

set size ratio 1
set palette gray negative
set xrange[-0.5:17.5]
set yrange[-0.5:17.5]
set cbrange[-0.2:0.8]
set xtics 0,1,17
set ytics 0,1,17
set xtics offset -0.5,0
set title "Resolusition Matrix for E"
plot "Mat" matrix w image noti

Then I got a fig like this:

enter image description here

Now I would like to add borders to each cell, which will look like this:

enter image description here

Thank you.

like image 735
Kai Avatar asked Oct 05 '13 18:10

Kai


1 Answers

For your case you can set one minor tic, which then lies on the border between two pixels, and draw a grid on them:

set size ratio 1
set palette gray negative
set autoscale xfix
set autoscale yfix
set xtics 1
set ytics 1
set title "Resolution Matrix for E"

set tics scale 0,0.001
set mxtics 2
set mytics 2
set grid front mxtics mytics lw 1.5 lt -1 lc rgb 'white'
plot "Mat" matrix w image noti

Note, that set grid front also brings the tics to the front. To avoid that you can scale the tics to 0. For the minor tics you must use a very small number, 0 would omit the grid lines on the minor tics.

The result with 4.6.3 is:

enter image description here

EDIT: In order to control the grid lines and tic labels independently, you can use the unused x2 and y2 to draw the grid (inspired by an answer to How do I draw a vertical line in gnuplot?):

set size ratio 1
set palette gray negative
# grid lines
set x2tics 1 format '' scale 0,0.001
set y2tics 1 format '' scale 0,0.001
set mx2tics 2
set my2tics 2

# labeling
set xtics 5 out nomirror
set ytics 5 out nomirror

set grid front mx2tics my2tics lw 1.5 lt -1 lc rgb 'white'

set xrange[-0.5:39.5]
set yrange[-0.5:39.5]
set x2range[-0.5:39.5]
set y2range[-0.5:39.5]

plot "Mat" matrix w image notitle

With gnuplot version 4.6, this requires setting explicit ranges, so that the x and x2 (unused!) are equal. The information might be extracted with stats from the data file.

Using version 5 allows you to use set link. Instead of all the set *range stuff. You could use:

set autoscale fix
set link x
set link y

Result:

enter image description here

like image 109
Christoph Avatar answered Oct 08 '22 05:10

Christoph