# Figure and font size when using \`Plots\`

**URL:** <https://discourse.julialang.org/t/figure-and-font-size-when-using-plots/117611>\
**Category:** Visualization\
**Tags:** plots, makie\
**Created:** [July 29, 2024, 8:02pm UTC](https://discourse.julialang.org/t/figure-and-font-size-when-using-plots/117611 "2024-07-29T20:02:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmichelenstrofer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmichelenstrofer/32/44392_2.png) [@cmichelenstrofer](https://discourse.julialang.org/u/cmichelenstrofer)\
**Post date:** [July 29, 2024, 8:02pm UTC](https://discourse.julialang.org/t/figure-and-font-size-when-using-plots/117611/1 "2024-07-29T20:02:56Z")

</div>

I am trying to save a plot at a specific size (in physical length units, like inches) and have the correct font size. The idea is that then I could just insert it into my LaTeX document without scaling and have the same font and font size as the rest of my document. I can’t seem to get this right and am extremely confused by the behavior of `size` and `dpi`. Any help understanding what is going on is appreciated!

Example:

```julia
using Plots

x = 0:0.01:2π;
y = cos.(x); 

p1 = plot(x,y;size=(600,600), dpi=600)
p2 = plot(x,y;size=(600,600), dpi=300)

savefig(p1, "test1.png")
savefig(p2, "test2.png")

```

I would expect the first image to be 1 inch by 1 inch, and the second to be 2 inches by 2 inches. But when I put them into my LaTeX document they are both larger than that, and even more confusing the second image is smaller than the first.

For reference, [this workflow works in matplotlib](https://pythonforthelab.com/blog/python-tip-ready-publish-matplotlib-figures/).

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 29, 2024, 9:23pm UTC](https://discourse.julialang.org/t/figure-and-font-size-when-using-plots/117611/2 "2024-07-29T21:23:26Z")

</div>

Please check if [this thread](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/18) can help you.

---

<div class="post-metadata">

**Author:** ![cmichelenstrofer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmichelenstrofer/32/44392_2.png) [@cmichelenstrofer](https://discourse.julialang.org/u/cmichelenstrofer)\
**Post date:** [July 29, 2024, 9:37pm UTC](https://discourse.julialang.org/t/figure-and-font-size-when-using-plots/117611/3 "2024-07-29T21:37:39Z")

</div>

No, I don’t think so. Unless I’m completely misunderstanding the fundamental concepts. I tried:

```julia
using Plots
x = 0:0.01:2π;
y = cos.(x); 
DPI = 600
width_inches = 3.4
width_px = width_inches * DPI
p = plot(x,y;size=(width_px, width_px), dpi=DPI)
savefig(p, "test.png")

```

and get this image with tiny text

 ![test](https://global.discourse-cdn.com/julialang/original/3X/0/a/0ac03eb731b0963ed834480dba3c40320ec45526.jpeg)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 29, 2024, 9:49pm UTC](https://discourse.julialang.org/t/figure-and-font-size-when-using-plots/117611/4 "2024-07-29T21:49:04Z")

</div>

I would also try using the pyplot backend with a fixed dpi=100: `pyplot(dpi=100)` and then changing only the `size` keyword as per linked post.

---

<div class="post-metadata">

**Author:** ![cmichelenstrofer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmichelenstrofer/32/44392_2.png) [@cmichelenstrofer](https://discourse.julialang.org/u/cmichelenstrofer)\
**Post date:** [July 29, 2024, 10:18pm UTC](https://discourse.julialang.org/t/figure-and-font-size-when-using-plots/117611/5 "2024-07-29T22:18:16Z")

</div>

I can’t get that to work either ☹

It seems like `Makie` can do this and is well documented: [Figures | Makie](https://docs.makie.org/v0.21/explanations/figure#Matching-figure-and-font-sizes-to-documents). I will try this!

> ### Matching figure and font sizes to documents
> 
> Academic journals usually demand that figures you submit adhere to specific physical dimensions. How can you render Makie figures at exactly the right sizes?

But if anyone knows how to do it in `Plots` let me know!

---

<div class="post-metadata">

**Author:** ![cmichelenstrofer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmichelenstrofer/32/44392_2.png) [@cmichelenstrofer](https://discourse.julialang.org/u/cmichelenstrofer)\
**Post date:** [July 30, 2024, 2:42pm UTC](https://discourse.julialang.org/t/figure-and-font-size-when-using-plots/117611/6 "2024-07-30T14:42:51Z")

</div>

Solution using `Makie`:

Julia:

```julia
using CairoMakie

x = 0:0.01:2π
y = cos.(x);

inches_2_pt = 72
pt_per_unit = 1

height = 4 # inches
width = 6 # inches
fontsize = 10 # pt
fonts = (; regular = "Computer Modern")

size = Int.(round.((width, height) .* (inches_2_pt / pt_per_unit)))

f = Figure(;size, pt_per_unit, fonts, fontsize)
ax = Axis(f[1,1], xlabel="x label", ylabel="y label")
lines!(ax, x, y, label="cosine", color=:tomato)
axislegend(position = :rb)

save("cosine.pdf", f)

```

LaTeX:

```tex
\documentclass{article}
\usepackage{graphicx} 
\usepackage{lipsum} 

\title{Title}
\author{Author Author}
\date{\today}

\begin{document}
\maketitle

\lipsum[2]

\includegraphics{cosine.pdf}

x label cosine \lipsum[2]

\end{document}

```

and here is the final product:

 ![Screenshot 2024-07-30 at 8.40.53 AM](https://global.discourse-cdn.com/julialang/original/3X/a/b/abe56a1130c8e618607a4f6e884defffddb3ded5.png)

The font size doesn’t look identical (not sure why) but close enough.

---

<div class="post-metadata">

**Author:** ![p\_f](https://avatars.discourse-cdn.com/v4/letter/p/45deac/32.png) [@p\_f](https://discourse.julialang.org/u/p_f)\
**Post date:** [July 30, 2024, 3:05pm UTC](https://discourse.julialang.org/t/figure-and-font-size-when-using-plots/117611/7 "2024-07-30T15:05:09Z")

</div>

If you put `\the\textwidth` in somewhere in your latex source, you can see that your column is 345pt wide.

Then with Makie you can just match the width of the figure with your latex by setting `size=(345,300)` (only the width matters there), and you don’t have to worry about inches conversions etc

Use `\includegraphics[width=\textwidth]{cosine.pdf}`  
edit: or set `pt_per_unit = 1` in Makie so the pdf is the correct size

This matches fontsize perfectly with your latex document, confirmed by advanced inspection in paint:

![image](https://global.discourse-cdn.com/julialang/original/3X/0/4/049414abaf004e2437d1abc9115bb72755d9fe17.png)

---

<div class="post-metadata">

**Author:** ![cmichelenstrofer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmichelenstrofer/32/44392_2.png) [@cmichelenstrofer](https://discourse.julialang.org/u/cmichelenstrofer)\
**Post date:** [July 30, 2024, 3:55pm UTC](https://discourse.julialang.org/t/figure-and-font-size-when-using-plots/117611/8 "2024-07-30T15:55:09Z")

</div>

Ah, my mistake was using `pt_per_unit` in the wrong place… it should be used when saving the PDF. So it was using the default value (0.75). I can also use one of the default themes instead of specifying the font myself. Here is the solution that works.

```julia
using CairoMakie

x = 0:0.01:2π
y = cos.(x);

pt_per_unit = 1
figsizeinches(width, height) = (width, height) .* (72/pt_per_unit)

height = 2.5 # inches
width = 4.5 # inches
fontsize = 10

set_theme!(theme_latexfonts(); fontsize)

f = Figure(; size=figsizeinches(width, height))
ax = Axis(f[1,1], xlabel="x label", ylabel="y label")
lines!(ax, x, y, label="cosine", color=:tomato)
axislegend(position = :rb)

save("cosine.pdf", f; pt_per_unit)

```

As @p_f mentioned, maybe a better way than inches is basing the size of the figure on the `\textwidth` of the LaTeX document, and using `pt_per_unit=1`

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 31, 2024, 7:34pm UTC](https://discourse.julialang.org/t/figure-and-font-size-when-using-plots/117611/9 "2024-07-31T19:34:10Z")

</div>

The trick is to realize that there is an internal dpi value in `Plots.jl` and the actual dpi value of the saved figure. The internal value is hardcoded as 100. The actual dpi value of the saved figure is set via `plots(...; dpi=DPI)`. This can be reflected in your code snippet as

```julia
using Plots
x = 0:0.01:2π;
y = cos.(x); 
DPI = 600
width_inches = 3.4
width_px = width_inches * 100 # internal dpi=100
p = plot(x,y;size=(width_px, width_px), dpi=DPI)
savefig(p, "test.png")

```

This gives me the following png file:

 ![test](https://global.discourse-cdn.com/julialang/original/3X/c/d/cd4828077c7826fb2b6f4790d4bfbcd7a575efa3.png)

Hope this helps!
