I am trying to create a png from a matrix using the pngcairo terminal, where each entry in the matrix is the colour of a single pixel in the image. For that purpose, I remove the border, tics, labels etc.
However, it seems that terminal always assumes the border takes the space of the first layer of pixels around the image and doesn't plot the data there, or does something funny there (antialiasing? I don't think so because it doesn't do the same blue smudge inside the image).
Here is a MWE (using transparent to highlight the issue):
set term pngcairo transparent size 7, 7
set margins 0,0,0,0
unset border
unset xtics
unset ytics
unset xlabel
unset ylabel
unset colorbox
unset key
set palette defined (0 "white", 1 "blue") maxcolors 2
set output 'test.png'
set xrange [0:6]
set yrange [0:6]
plot '-' matrix with image notitle
1 1 1 1 1 1 1
1 1 1 0 1 1 1
1 1 0 0 0 1 1
1 0 0 0 0 0 1
1 1 0 0 0 1 1
1 1 1 0 1 1 1
1 1 1 1 1 1 1
e
unset output
The result is a 7x7 pixel image whose magnification (using an image viewer with checkerboard background) is
What I would expect instead is
which is obtained by creating a 70x70 pixel image, offsetting the origin by 0.5 along each axis
using (0.5+$1):(0.5+$2):3
, and extending the xrange
and yrange
to [0:7]
. You will note that there is still a 1 pixel layer of background around the image, where the data is not plotted.
Is there any way to use the data from the matrix to plot on the border using this terminal?
I am using Gnuplot 5.4 patchlevel 2
This is almost identical to this: gnuplot: how to plot one 2D array element per pixel with no margins, however, the input data source is different.
So, my insight from there: it will not work with terminal pngcairo
, but it will work with terminal png
.
Script:
### plot png pixel graph without borders
reset session
OUTPUT = "SO76419284.png"
$Data <<EOD
1 1 1 1 1 1 1
1 1 1 0 1 1 1
1 1 0 0 0 1 1
1 0 0 0 0 0 1
1 1 0 0 0 1 1
1 1 1 0 1 1 1
1 1 1 1 1 1 1
EOD
stats $Data u 0 nooutput
sizeX = STATS_columns
sizeY = STATS_records
set term png transparent size sizeX, sizeY
set output OUTPUT
set margins 0,0,0,0
unset border
unset colorbox
unset key
unset tics
unset xlabel
unset ylabel
set xrange [0:sizeX-1]
set yrange [0:sizeY-1]
set palette defined (0 "white", 1 "blue") maxcolors 2
plot $Data matrix u 1:2:3 w p pt 5 ps 0.1 lc palette
unset output
### end of script
Result:
(32x magnified)
Addition:
Thanks to @armando.sano, I got some more insight.
The terminal pngcairo
doesn't really work even when using a smaller pointsize. I tested different pointsizes.
pngcairo
ps 0.1
pngcairo
ps 0.01
pngcairo
ps 0.001
but if you check the exact color, there will be still pixels with #efefff
and #0202ff
.
However, if I set ps 0.0006
I will get just a white image.
pngcairo
ps 0.0006
So, for me (Win10, gnuplot 5.4.0) terminal pngcairo
doesn't work.
In contrast, if you check the above output from terminal png
and ps 0.1
you clearly have only #ffffff
and #0000ff
pixels.