Help to plot in Julia

I love Julia, but I still do not figure out how to make pretty plots. The figure size is in pixels rather than inches, which complicates things when you want to set font sizes (in points). LaTeXStrings does not work in Windows. I’ve been trying to fix it without success.
I want to create a customized plot (see code below), but I get many warnings latex: failed to create a dvi file.

The problem boils down to:

  • I cannot insert latex tex
  • Cannot control the figure and font sizes in inches
  • The figure is cropped when I save it
  • The font does nothing when I try to use Euclid or CMU

I’ve been trying to fix all of this, but without success. I will really appreciate any help to understand how this works and how I could fix it.

Thanks in advance.

using Plots, LaTeXStrings

dpi = 300

w = 3.5 * dpi
h = 2 * dpi

font_size = Int(round(10 / 0.75 / 72 * 100))

font_name = "Times"

p = plot(sin, 0, 2π,

    title=L"y = x_t, Title",
    titlefont=font(font_size, "sans"),
    xlabel="X label",
    ylabel="Units",
    xguidefont=font(font_size, font_name), ## x-label
    yguidefont=font(font_size, font_name), ## y-label
    xtickfont=font(font_size, font_name),
    ytickfont=font(font_size, font_name),
    tickfont=font(font_size, font_name), # x,y-ticks font
    label="Sin function",
    legendfont=font(font_size, font_name),
    xlims=(0, 2π),
    ylims=(-1, 1),
)

plot!(size=(w, h), dpi=dpi)
savefig(p,"file.png")

Two suggestions for getting help:

  1. Simplify your problem. It’s easier to deal with issues one at a time.

  2. Titles like this are not only inaccurate but also potentially offputting to the people most likely to help you, i.e. people who generously contribute to Plots.jl and LateXStrings.jl and both let you use their work, and regularly help with issues in these forums. Edit: the title is friendlier now :slight_smile:

3 Likes

Inches is a rather obscure unit which is only used in a single country and nowhere else in the world :wink:

Pixels is a standard measurement of plot sizes of various plotting packages, including Matplotlib (Python) and (as far as I remember) Matlab. But I agree that it is rather un-intuitive.

4 Likes

Unfortunately Matplotlib uses inches by default, so I have to futz around with DPI conversion every time I use it! Even more fun when wrappers like Seaborn or Pandas are involved. Thankfully newer plotting libraries are smarter about this.

You are right, I mixed them up in Matplotlib.

Thank you for your reply and your suggestions, and you are right. The title is not accurate, but I did not intend to potentially offputting people who are doing an amazing job. Thanks again, this is the first time I post something and I’ll keep in mind your comments.

1 Like

I think you need to enter math mode. Like the example in the docs shows.
L"an equation: $1 + \alpha^2$"

Edit: This works for me:
julia> plot(title=L"x_{t}\textrm{, Title}")