# Plots.jl set figure size for LaTeX publications

**URL:** <https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960>\
**Category:** Visualization\
**Created:** [April 3, 2020, 1:52pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960 "2020-04-03T13:52:23Z")\
**Posts on this page:** 18\
**Page:** 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:** [April 3, 2020, 1:52pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/1 "2020-04-03T13:52:24Z")

</div>

Hi,  
I want to use Plots.jl to make figures for a publication. I want the width of the figure to be exactly 246.0 points (pt). The format should be pdf. I want to do this, so I can import the pdf figure into LaTeX later and have the fonts appearing the same as set for plotting.

I come from pyplot and there you can convert to inches and then set the size of the resulting figure. As far as I am aware of Plots.jl only lets you specifiy the size in pixels.  
I found [Use Measure when setting plot size · Issue #501 · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/issues/501) pointing this out.

But maybe my approach is wrong already. I would be thankful for help!

jamble

---

<div class="post-metadata">

**Author:** ![kmundnic](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kmundnic](https://discourse.julialang.org/u/kmundnic)\
**Post date:** [April 4, 2020, 11:57pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/2 "2020-04-04T23:57:46Z")

</div>

A way to achieve this is using the `pgfplots` backend to save the figure into a `tikzpicture`.

Instead of adjusting the size before you produce the plot, you can adjust the size in the generated .tex file. By using the package `pgfplots` in LaTeX, the plot will be drawn directly in the PDF, where you can adjust the size, fonts, colors, and any other property you can think of.

An example:

```julia
using Plots; pgfplots() # Need to add PGFPlots first
plot(1:3)
savefig("example.tex")

```

I personally do not like the output the juliaplots for the .tex files, as they seem a bit messy to me, but here the first line has the height and you can also specify a width:

```julia
\begin{tikzpicture}[]
\begin{axis}[height = {101.6mm}, ylabel = {}, xmin = {0.94}, xmax = {3.06}, ymax = {3.06}, xlabel = {}, unbounded coords=jump,scaled x ticks = false,xlabel style = {font = {\fontsize{11 pt}{14.3 pt}\selectfont}, color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000}, draw opacity = 1.0, rotate = 0.0},xmajorgrids = true,xtick = {1.0,1.5,2.0,2.5,3.0},xticklabels = {$1.0$,$1.5$,$2.0$,$2.5$,$3.0$},xtick align = inside,xticklabel style = {font = {\fontsize{8 pt}{10.4 pt}\selectfont}, color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000}, draw opacity = 1.0, rotate = 0.0},x grid style = {color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000},
draw opacity = 0.1,
line width = 0.5,
solid},axis x line* = left,x axis line style = {color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000},
draw opacity = 1.0,
line width = 1,
solid},scaled y ticks = false,ylabel style = {font = {\fontsize{11 pt}{14.3 pt}\selectfont}, color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000}, draw opacity = 1.0, rotate = 0.0},ymajorgrids = true,ytick = {1.0,1.5,2.0,2.5,3.0},yticklabels = {$1.0$,$1.5$,$2.0$,$2.5$,$3.0$},ytick align = inside,yticklabel style = {font = {\fontsize{8 pt}{10.4 pt}\selectfont}, color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000}, draw opacity = 1.0, rotate = 0.0},y grid style = {color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000},
draw opacity = 0.1,
line width = 0.5,
solid},axis y line* = left,y axis line style = {color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000},
draw opacity = 1.0,
line width = 1,
solid}, xshift = 0.0mm,
    yshift = 0.0mm,
    axis background/.style={fill={rgb,1:red,1.00000000;green,1.00000000;blue,1.00000000}}
,legend style = {color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000},
draw opacity = 1.0,
line width = 1,
solid,fill = {rgb,1:red,1.00000000;green,1.00000000;blue,1.00000000},fill opacity = 1.0,text opacity = 1.0,font = {\fontsize{8 pt}{10.4 pt}\selectfont}},colorbar style={title=}, ymin = {0.94}, width = {152.4mm}]\addplot+ [color = {rgb,1:red,0.00000000;green,0.60560316;blue,0.97868012},
draw opacity = 1.0,
line width = 1,
solid,mark = none,
mark size = 2.0,
mark options = {
            color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000}, draw opacity = 1.0,
            fill = {rgb,1:red,0.00000000;green,0.60560316;blue,0.97868012}, fill opacity = 1.0,
            line width = 1,
            rotate = 0,
            solid
        }]coordinates {
(1.0, 1.0)
(2.0, 2.0)
(3.0, 3.0)
};
\addlegendentry{y1}
\end{axis}

\end{tikzpicture}

```

Using the standalone class (for example), the code above generates this figure (which I converted from PDF to PNG to upload it here):  
 ![test__Copy_](https://global.discourse-cdn.com/julialang/original/3X/9/2/9250ee4a049a276dc64a198358bc8b5c38d535aa.png)

Finally, when using PGFPlots within a LaTeX document and you have settled on the size/look, you can use TikZ external library to print the figure into a standalone PDF with the size you have already selected, and that you can include directly into your document. This will make the compilation of the document a lot faster in many cases, specially when the figures need lots of points to be drawn.

Hope this helps!

---

<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:** [April 6, 2020, 11:39am UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/3 "2020-04-06T11:39:12Z")

</div>

Thanks for the detailed reply. I have some problems with this approach:

- it seems a lot of steps which have to be done by hand: light post processing of data in Julia, exporting to .tex file, changing stuff in .tex file, generating pdf. I liked my workflow so far: light post processing + plotting + exporting in Julia in one function call → recompiling (F5) latex document.
- the next issue would be what type of figure processing will I do in Julia with Plots.jl and what in tikz. If I once generated and edited the .tex file I can not change the data anymore without copying or change the image processing with Plots.jl without redoing by hand everything in the .tex file again.
- the .tex file blows up very quickly for my plots.
- There are still some features which are not translated by Plots.jl to pgfplots, like axis mirroring, boxes around graphs, legend placing. At least this did not work in my case when changing from pyplot to pgfplots. This might be fixable.

Actually, I do not really understand why there is an issue with specifiying the size of an image in inches/mm/pt. Shouldn’t this be one of the first features implemented in a (meta) plotting package? Please correct me, if I am wrong!

EDIT: I found some more links/people having issues with that: [Plotting for publications. · Issue #897 · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/issues/897)

---

<div class="post-metadata">

**Author:** ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)\
**Post date:** [April 6, 2020, 12:27pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/4 "2020-04-06T12:27:34Z")

</div>

What is the conversion between 246 points and a different unit? Is it 72 points per inch, i.e., you are looking for a figure to be 3.416 inches wide? If so, the only question is how many pixels per inch, i.e., dots per inch (dpi). Some journal require 300 dpi. If so, you would need to save a figure that is 1025 pixels wide. You can do that with the `size` argument in the Plots.jl `plot` function.

Would exporting to an SVG (or other vector graphics) be useful here? This way you can set the figure to any size without loss of resolution.

Also, could you provide the journal’s requirements on figures? That might be helpful.

---

<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:** [April 6, 2020, 12:50pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/5 "2020-04-06T12:50:52Z")

</div>

Hi [josephmarturano](https://discourse.julialang.org/u/josephmarturano),  
the Journal is two columns and the column width is exactly 246 pts. I used pyplot before and converted to inch and used something like `set_size_inches((width_inch,height_inch))` on the final figure object.

It is of course an option to specify the dpi and convert to pixels and store in a pixel format. But I like to save as vector format and do not have to think about dpi at all - let the journal specify this. I do not know by now what dpi they require for pixel graphics.

The reason I am worried a bit about the exact figure size is that I want the fontsizes set in the figure appear exactly as the font sizes in the document.

---

<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:** [April 6, 2020, 2:58pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/6 "2020-04-06T14:58:33Z")

</div>

> [@jamblejoe](#):
>
> There are still some features which are not translated by Plots.jl to pgfplots, like axis mirroring, boxes around graphs, legend placing. At least this did not work in my case when changing from pyplot to pgfplots. This might be fixable.

Most of this should be fixed in the `pgfplotsx`-backend and if not, please open an issue 🙂

---

<div class="post-metadata">

**Author:** ![jmert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmert/32/3161_2.png) [@jmert](https://discourse.julialang.org/u/jmert)\
**Post date:** [April 6, 2020, 4:56pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/7 "2020-04-06T16:56:29Z")

</div>

> [@josephmarturano](#):
>
> What is the conversion between 246 points and a different unit? Is it 72 points per inch, …

In the context of TeX documents, 1 inch is equivalent to 72.27 pt. [[ref](https://en.wikibooks.org/wiki/TeX/TeX_dimensions)]

---

<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:** [April 6, 2020, 5:15pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/8 "2020-04-06T17:15:24Z")

</div>

> [@BeastyBlacksmith](#):
>
> Most of this should be fixed in the `pgfplotsx`-backend and if not, please open an issue 🙂

Ah, I was not aware that there are 2 pfgplot wrappers. Is there a reason for that?

I like using matplots pyplot backend so far. Is there a possibility to set the size of the underlying pyplot object? I tried `plotjl_fig.o.set_size_inches` but it did not have any affect on the output image when I save with `savefig` from `Plots.jl`.

---

<div class="post-metadata">

**Author:** ![kmundnic](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kmundnic](https://discourse.julialang.org/u/kmundnic)\
**Post date:** [April 6, 2020, 6:45pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/9 "2020-04-06T18:45:45Z")

</div>

I didn’t add too many details in my initial answer so that it wouldn’t be too long. However:

- I use the `\input{}` command in LaTeX to load the file, so if it it saved in the right place, there’s no extra manual steps.
- In pgfplots you can load the data into your plot from a .dat or CSV file. Therefore, if you don’t want to change the figure every time (which is completely understandable, I don’t either), you can just export the data once you’re happy with the looks of the plot. Here [pgfplots - How to plot data from a CSV file using tikz and csvsimple? - TeX - LaTeX Stack Exchange](https://tex.stackexchange.com/a/83929) is an example on how to achieve that. Note that in that example they mock the CSV file.

I agree that there are still some more steps involved, but in my opinion, the quality of the plots is much better with this approach. For example, fonts are consistent with the document, which is hard to achieve when loading a file produced with any external library.

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [April 6, 2020, 7:53pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/10 "2020-04-06T19:53:53Z")

</div>

> [@jamblejoe](#):
>
> I like using matplots pyplot backend so far.

If you know the PyPlot commands already, why wouldn’t you use PyPlot.jl directly without Plots.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:** [April 6, 2020, 7:54pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/11 "2020-04-06T19:54:47Z")

</div>

> [@jamblejoe](#):
>
> Ah, I was not aware that there are 2 pfgplot wrappers. Is there a reason for that?

Mainly different tastes of interfaces.

---

<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:** [April 7, 2020, 9:35am UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/12 "2020-04-07T09:35:30Z")

</div>

I just want to summarize, so I am sure I understood correctly what you suggested:

- I export the data to e.g. csv file with Julia  
(from here no Julia)
- for every figure in my document I have a seperate .tex file, where I do the plotting with pgfplots and use e.g. standalone class to save as pdf
- then I import the .pdf figure, as I already do.

So, in other words, you suggest not using any Julia plotting library/wrapper at all, but instead use pgfplots directly in LaTeX, not the pgfplots or the pgfplotsx backend?

I asked people in my department, but very few are using Julia and none of them are doing their plots with Julia.

I hoped to get away with using Julia code only and not having to learn a new (complicated) plotting interface/language.

EDIT: I am still in the phase of finding what works best for me. I am always open for suggestions and ideas!

---

<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:** [April 7, 2020, 9:42am UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/13 "2020-04-07T09:42:41Z")

</div>

I am not an expert in PyPlot. I find the interface very unintuitive and complicated. Often I ended up trying 3,4 different ways of achieving something and only one of them worked and I could not figure out why. But this might just be because I never really learned how to use it properly.

Using `Plots.jl` made my code much cleaner and more readable. And it did what I thought it should do. Often the first time I tried. And I liked the idea of having different, but similar aliases for setting the same parameter, so you do not have to look up the exact spelling, e.g. underscore or not, shortcut or written out, etc.

---

<div class="post-metadata">

**Author:** ![kmundnic](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kmundnic](https://discourse.julialang.org/u/kmundnic)\
**Post date:** [April 7, 2020, 4:55pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/14 "2020-04-07T16:55:30Z")

</div>

This is the approach that I use in Julia (using the `pgfplots` backend), Python (using `tikzplotlib`), and MATLAB (using `matlab2tikz`):

- I generate a plot with any of the tools above. In the case of Julia, to the best of my knowledge, the data cannot be saved using `savefig` (unlike `matlab2tikz` for instance), so I export the data to CSV (using for example `CSV.jl` and call it from the .tex file. Here, changing the .tex file once is required so that it reads the data from the CSV file.
- I use the following in my main .tex file:

```julia
\usetikzlibrary{external} % Can be placed as a list with other packages for clarity
\tikzexternalize[prefix=figures/] % Folder in which files are saved

\begin{document}
\tikzexternaldisable % No figures are externalized unless I require it
.
.
.
\tikzexternalenable % Enable externalization
\begin{figure}
\centering
\tikzsetnextfilename{example_figure} % name with which the figure will be saved
\input{example_figure.tex} % This file has the code generating the figure using PGFPlots
% \includegraphics{figures/example_figure.pdf} % use after externalizing, if needed
\caption{}
\end{figure}
\tikzexternalenable % Disable externalization

```

- I tweak the file `example_figure.tex` for desired size and looks (just once). For example, for width I usually use:

```julia
width=\columnwidth % in example_figure.tex

```

- If I need to change the data in the plot, I re-export the data and compile the .tex file again, without needing to change the .tex file.
- Once I am happy with the plot, I convert it to PDF by compiling the file using shell escape:

```julia
$ pdflatex --shell-escape main.tex

```

This generates a PDF file called `example_figure.pdf` in the `figures/` folder (as selected above). Then, I uncomment the line `% \includegraphics{figures/example_figure.pdf}` and comment the line `\input{example_figure.tex}` above and my figure is loaded into the main document as a PDF file. Note that you _can_ use a standalone file to compile the figure, but this is not required (or needed).

For a bit of background, I converged to this method for a few reasons:

- It works well across languages, so if I am working in Julia, Python, or MATLAB I can use the same steps to generate a high-quality figure,
- If the data changes, I only need to export the data, but the plot stays the same (except for the externalization step, which needs to be re-run),
- Figures can be very easily shared between LaTeX documents (for example, generate figures for a paper and then use them in a poster or presentation), where the appropriate fonts and sizes are used in each case.

I understand that the method that I just described requires a few more manual steps than what you were originally describing and that it’s not Julia-specific, but it allows you to achieve what you intend.

Hopefully you find a workflow that works for you.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 8, 2020, 5:46am UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/15 "2020-04-08T05:46:11Z")

</div>

> [@jamblejoe](#):
>
> instead use pgfplots directly in LaTeX, not the pgfplots or the pgfplotsx backend?

There is also

> **[GitHub - KristofferC/PGFPlotsX.jl: Plots in Julia using the PGFPlots LaTeX...](https://github.com/KristofferC/PGFPlotsX.jl)**
>
> Plots in Julia using the PGFPlots LaTeX package. Contribute to KristofferC/PGFPlotsX.jl development by creating an account on GitHub.

if you like working with PGFPlots/LaTeX more directly.

---

<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:** [April 11, 2020, 2:46pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/16 "2020-04-11T14:46:08Z")

</div>

Hi @kmundnic,

Thank you for your very detailed answer. This thread is moving away from asking a simple question to organizing my figure creation work process! 😃

I try to understand how you do your plotting. I do not understand, how you use Julia’s `Plots.jl` library. What data do you export? Only the raw data or is there plotting data encoded in the .csv file? If the first is true, where does `Plots.jl`get used? If the latter is true, don’t you have to retweak the example\_figure.tex file everytime you change plotting settings in Julia’s `Plots.jl`?  
Maybe, if you have time and are willing to, you could explain a bit more the content of example\_figure.tex and when it needs changing.

Thanks, another time for your input!

---

<div class="post-metadata">

**Author:** ![kmundnic](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kmundnic](https://discourse.julialang.org/u/kmundnic)\
**Post date:** [April 11, 2020, 11:35pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/17 "2020-04-11T23:35:16Z")

</div>

@jamblejoe for convenience, I uploaded a full example in [GitHub - kmundnic/pgfplots-example: An example of PGFPlots in LaTeX](https://github.com/kmundnic/pgfplots-example). Feel free to download it and experiment with it, and ask questions here or add issues in the repo itself if needed.

---

<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:** [April 20, 2020, 4:43pm UTC](https://discourse.julialang.org/t/plots-jl-set-figure-size-for-latex-publications/36960/18 "2020-04-20T16:43:42Z")

</div>

Hi @kmundnic!

Thanks for this awesome answer! I had a look in the repository([GitHub - kmundnic/pgfplots-example: An example of PGFPlots in LaTeX](https://github.com/kmundnic/pgfplots-example)) already. Thanks a lot! I will open an issue there in case I have any further questions.

In case people are visiting this thread with a similar question, I will post the answert to my initial question. I digged in the source code a bit and worked out, how `Plots.jl` is setting thefigure size for the pyplot backend. They use an internal DPI value of 100, set by the constant `DPI`, found in … . The first line in the function `_create_backend_figure` in `pyplot.jl` tells the story:

```julia
w,h = map(px2inch, Tuple(s * plt[:dpi] / Plots.DPI for s in plt[:size]))

```

The size of the figure set by `Plots.jl` `size` argument is in pixels and then converted to inch using the DPI value of `dpi`( which can be set via `plot(dpi=100)`).  
If you do not want to safe in a pixelformat, you do not have to care about the DPI value. Converting from pts to the correct inches works as follows:

```julia
width_pts = 246.0 # or any other value
inches_per_points = 1.0/72.27
width_inches = width_pts *inches_per_points
width_px= width_inches*100 # or width_inches*DPI

```

Setting `plot(size=(width_px, width_px))` now creates a square plot with exactly 246 points.
