2

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 test.png zoomed in

What I would expect instead is enter image description here 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

armando.sano
  • 345
  • 1
  • 3
  • 9

1 Answers1

1

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:

enter image description here

(32x magnified)

enter image description here

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

enter image description here

pngcairo ps 0.01

enter image description here

pngcairo ps 0.001 but if you check the exact color, there will be still pixels with #efefff and #0202ff.

enter image description here

However, if I set ps 0.0006 I will get just a white image.

pngcairo ps 0.0006

enter image description here

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.

theozh
  • 22,244
  • 5
  • 28
  • 72
  • Great find, thank you, I searched but didn't find this one. Lots of different workarounds - the accepted answer at the link is awkward, yours @theozh works a treat: replacing the plot command in my initial script with `plot '-' matrix using 1:2:3 with points pt 5 ps 0.001 lc palette notitle` does the job. (`ps` needs to be very small but not zero, `ps 0.1` still shows some bluish tones in the middle). Possibly `png` terminal is better? – armando.sano Jun 07 '23 at 05:44
  • 1
    @armando.sano thank you for the feedback. Well, `terminal pngcairo` does not work perfectly, there will be always some blueish/whiteish tones. I will add some examples. Whereas `terminal png` seems to work as expected as far as I can tell. – theozh Jun 07 '23 at 06:56