Search code examples
imagepnggnuplotjpegdpi

How to save a GNUPLOT to the correct size and resolution in mm?


I use GNUPLOT to generate a plot, the plot is like a grid with horizontal and vertical lines. the light blue lines are 1mm apart and dark blue are 5mm (a total of 250mm wide x 195mm height)

So I want to save the image as png or jpg so that it prints with the correct size and don't lose line resolution.

for the plot I use an axis of 5000x3900 printing a light blue line each 20 and the dark blue lines each 100.

As you can see when I save the image on png the grid degrades a lot.

How do I have to save it to get a proper 1mm grid?

rasterized plot


Solution

  • If you want high-resolution anything, you should not choose jpeg as an output format. It is a "lossy" compression scheme that intrinsically loses resolution to reduce the file size.

    To use png in gnuplot you can give an explicit resolution in pixels. That does not translate directly into a physical measurement like millimeters or inches, however. That involves an additional level of pixels-per-mm or pixels-per-inch scaling that is done by the printer or viewing program.

    To produce output with a specific resolution or object placement in mm you should use a vector output format - postscript, pdf, or svg. For example you can set the total output page size to 25cm x 19.5cm so that it is large enough to hold your plot, and then require there be no margins between the grid and the edge of the page. Thus the grid should fill the entire page and therefore the x and y axis units come out in millimeters. You may want to further adjust the line widths, e.g. set term pdf linewidth 0.5 size 25.0cm, 19.5cm.

    set term pdf size 25.0cm, 19.5cm
    set output 'grid.pdf'
    
    set lmargin screen 0.0; set rmargin screen 1.0
    set bmargin screen 0.0; set tmargin screen 1.0
    
    set xrange [0:5000]
    set yrange [0:3900]
    
    set tics format ""       # no tic labels
    set tics scale 0.01, 0.01 # very small tic marks
    
    set xtics 100
    set mxtics 5      # 5 minor tics per major tic
    set ytics 100
    set mytics 5      # 5 minor tics per major tic
    
    set linetype 101 lc "blue" lw 1
    set linetype 102 lc "blue" lw 0.5
    set grid x mx y my lt 101, lt 102
    
    
    plot x*sin(x/500.) with lines lc "red"
    

    I will attach a png image at 600 dpi converted from the pdf output, but this conversion will obviously lose the original resolution so this is mostly just to confirm that the script works.enter image description here