# gnuplot setting image width with pixels for specific DPI

I need to produce image with gnuplot with following guide given

width=90 mm (255 pt), pixels at 300 dpi=1063, pixels at 500 dpi=1772 , pixels at 1000 dpi=3543

How can i produce image with width=90mm and pixels=1063 for 300 dpi In gnuplot i use

``````set terminal pngcairo size 420,768
``````

But how translate x and y into required width and pixels

## Solution 1

I will assume that you have already created a figure that is just the right size of 420x768. To generate a PNG image with a width of 1063 pixels (90mm at 300dpi) without changing the look of the figure, set it up as follows,

``````scale = 1063.0/420.0

set terminal pngcairo size 420*scale,768*scale fontscale scale linewidth scale pointscale scale

# PLOT HERE
``````

In this setting, the width and height of the figure are multiplied by 1063.0/420.0 = 2.53, and the font size, line width, and point size are scaled by the same factor.

## Solution 2

If you want to build a figure from scratch with a given DPI, how about the following terminal settings,

``````dpi = 300 ## dpi (variable)
width = 90 ## mm (variable)
height = 164.5 ## mm (variable)

in2mm = 25.4 # mm (fixed)
pt2mm = 0.3528 # mm (fixed)

mm2px = dpi/in2mm
ptscale = pt2mm*mm2px
round(x) = x - floor(x) < 0.5 ? floor(x) : ceil(x)
wpx = round(width * mm2px)
hpx = round(height * mm2px)

set terminal pngcairo size wpx,hpx fontscale ptscale linewidth ptscale pointscale ptscale

# PLOT HERE
``````

`height` is calculated using 764.0/420.0*90 to keep aspect ratio of original figure.

• Why height has value of 164.5, is it to keep proper aspect ratio for 90mm width Nov 4, 2020 at 18:12
• Yes, I calculated it from the 420,768 aspect ratio of your sample. If you need a different size, you can specify the width and height values in mm. Nov 4, 2020 at 22:12
• What formula you use to get value of 164.5 Nov 5, 2020 at 12:28
• 768.0/420.0*90.0 Nov 6, 2020 at 10:01
• Does my problem setting match your question? Nov 7, 2020 at 8:18