# Why do font sizes change with dpi in Plots?

**URL:** https://discourse.julialang.org/t/why-do-font-sizes-change-with-dpi-in-plots/86220
**Category:** Visualization
**Tags:** plotting, plots
**Created:** [August 23, 2022, 8:02pm UTC](https://discourse.julialang.org/t/why-do-font-sizes-change-with-dpi-in-plots/86220 "2022-08-23T20:02:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)
#### Post date: [August 23, 2022, 8:02pm UTC](https://discourse.julialang.org/t/why-do-font-sizes-change-with-dpi-in-plots/86220/1 "2022-08-23T20:02:07Z")

</div>

I would like to save plots as .png files at a certain dpi resolution, import into MS Word, then set the width of the image to the correct length according to the dpi setting. However, changing the dpi with Plots.jl in 3 tested backends also changes the font size (and other attributes).

If font sizes are in units of points, and a point is 0.3528 mm, then shouldn’t font size be constant regardless of dpi settings?

For example I have a function that takes the number of columns I want a figure to occupy and a fraction of the max size allowed in each dimension. It prints out dimensional info as a convenience:

```julia
using Plots
pyplot()
backend = "pyplot"

function fig_size_px(;n_columns=n_columns::Int, dpi::Int=300, w_frac::Float64=1.0, h_frac::Float64=1.0)
    MM_PER_IN = 25.4
    w_mm_max = 90 * n_columns
    w_mm = w_mm_max * w_frac
    w_in = w_mm/MM_PER_IN
    w_px::Int = round(Int32,w_in * dpi) 

    h_mm_max = 170
    h_mm = h_mm_max * h_frac
    h_in = h_mm/MM_PER_IN
    h_px::Int = round(Int32,h_in * dpi)

    println("DPI set to $(dpi).\n
        $(n_columns) column wide\n
        Figure width: $(w_px) pixels ($(round(Int32,w_mm_max/MM_PER_IN * dpi)) max), $(w_mm) mm ($(w_mm_max) max), $(round(w_in,digits=3)) in. ($(round(w_mm_max/MM_PER_IN,digits=3)) max)\n
        Figure height: $(h_px) pixels ($(round(h_mm_max/MM_PER_IN * dpi)) max), $(h_mm) mm ($(h_mm_max) max), $(round(h_in,digits=3)) in. ($(round(h_mm_max/MM_PER_IN,digits=3)) max)")

    return w_px, h_px
end

x = (0.0:0.01:2π)
y = sin.(x)

default_font_size = 12
title_font_size = 16
font_family = "Helvetica"

n_cols = 1
dpi_setting = 300
w_px, h_px = fig_size_px(n_columns=n_cols, dpi=dpi_setting, w_frac=0.5, h_frac=0.25)

f1 = plot(size=(w_px, h_px),
    xtickfont = font(default_font_size, font_family),
    ytickfont = font(default_font_size, font_family),
    xlabelfont = font(default_font_size, font_family),
    ylabelfont = font(default_font_size, font_family),
    titlefont = font(default_font_size, font_family),
    legendfont = font(default_font_size, font_family),
    xguidfontsize = default_font_size,
    yguidefontsize = default_font_size,
    margin=5mm,
    dpi=dpi_setting)

plot!(f1[1], x, y,
    xlabel="x",
    ylabel="sin(x)",
    title="y = sin(x)",
    label="y = sin(x)")

savefig("test_$(dpi_setting)-dpi_$(backend).png")

dpi_setting = 600
w_px, h_px = fig_size_px(n_columns=n_cols, dpi=dpi_setting, w_frac=0.5, h_frac=0.25)

f2 = plot(size=(w_px, h_px),
    xtickfont = font(default_font_size, font_family),
    ytickfont = font(default_font_size, font_family),
    xlabelfont = font(default_font_size, font_family),
    ylabelfont = font(default_font_size, font_family),
    titlefont = font(default_font_size, font_family),
    legendfont = font(default_font_size, font_family),
    xguidfontsize = default_font_size,
    yguidefontsize = default_font_size,
    margin=5mm,
    dpi=dpi_setting)

plot!(f2[1], x, y,
    xlabel="x",
    ylabel="sin(x)",
    title="y = sin(x)",
    label="y = sin(x)")

savefig("test_$(dpi_setting)-dpi_$(backend).png")

```

Testing with `pyplot()`, `gr()`, and `plotly()` (html → png) gives this when importing into Word and scalling the width to 45 mm where the backend labels are typed in Word with the font and font size specified.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/3/73ce840249140def558b5298a037db125703a027.png)

---

<div class="post-metadata">

### Author: ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)
#### Post date: [August 24, 2022, 1:29pm UTC](https://discourse.julialang.org/t/why-do-font-sizes-change-with-dpi-in-plots/86220/2 "2022-08-24T13:29:09Z")

</div>

… works as expected in plain GR.jl.

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [August 24, 2022, 2:18pm UTC](https://discourse.julialang.org/t/why-do-font-sizes-change-with-dpi-in-plots/86220/3 "2022-08-24T14:18:48Z")

</div>

There are a handful of open issues debating over what `dpi` should and should not do on the Plots repository (e.g. [dpi on backends · Issue #733 · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/issues/733), [Proposal: user-friendly DPI setting. · Issue #742 · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/issues/742))
