Search code examples
matrixgnuplotpixel

gnuplot matrix increased pixel size


I am having trouble visualizing a large matrix in gnuplot, seemingly due to pixel sizes. As a minimal working example, I try to plot a 5000 x 5000 identity matrix with the following commands:

set term pngcairo enh col
set out "plot.png"    
unset key
set datafile commentschars "%#"
set xrange [0:5000]
set yrange [5000:0] reverse
plot 'A' matrix w image

Data is here. The resulting plot looks like this: enter image description here

You can see some of the diagonal elements but it looks quite bad and much of the diagonal is missing. When I go to 10000 x 10000, none of the diagonal is visible. It seems the pixels sizes aren't large enough. In contrast, the following matlab code produces a nice figure:

A = eye(5000,5000);
imagesc(A);

The figure looks like this:

enter image description here

Here we can see a nice diagonal in the image. How can I make gnuplot produce a similar figure?


Solution

  • Option 1: Make your plot resolution big enough that it can actually represent 5000 pixels per side. If the resulting image file is too large for your purpose you can shrink it with an external tool such as ImageMagick. A likely problem is that the diagonal line is so thin that it will become invisible when you shrink the figure.

     set size square
     unset key
     set datafile commentschars "%#"
     set xrange [0:5000]
     set yrange [5000:0] reverse
    
     set term pngcairo size 6000,6000 fontscale 10.
     set output 'bigplot.png'
     plot 'A' matrix w image
     unset output
    

    Option 2: Down-sample the matrix so that it fits in a smaller plot resolution. E.g.

     set size square
     unset key
     set datafile commentschars "%#"
     set xrange [0:5000]
     set yrange [5000:0] reverse
    
     set term pngcairo size 600,600
     set output 'smallplot.png'
     plot 'A' matrix every 10:10 with image
    

    Shown below is the output from option 2 enter image description here