GADFLY Font Change Not Saving

When changing the label font and saving as a PDF, the saved pdf does not display the changed font style. Here’s an example:

xs = convert(Vector{Float64}, tsteps)  

layer2 = layer(x=xs1, y=Euler_Maruyama1,Geom.line,Theme(line_width = 1pt,default_color=colorant"#1E90FF"))
layer4 = layer(x=xs1, y=Milstein1,Geom.point, shape=[Gadfly.Shape.xcross],Theme(line_width = 1pt,default_color=colorant"#006400"))
layer3 = layer(x=xs1, y=GBM1, Geom.line,Theme(line_width = 1pt,default_color=colorant"#FF0000"))
p1 = Gadfly.plot(layer4,layer3,layer2, Guide.xticks(ticks = [0 0.25 0.5 0.75 1]),Theme(grid_line_width=0mm,panel_stroke=color("black"),major_label_font="times-new-roman",minor_label_font="times-new-roman",key_label_font="times-new-roman",key_label_font_size=6pt,major_label_font_size=8pt),Guide.yticks(ticks = [92    97   102   107   112]),Guide.xlabel("Time"), Guide.ylabel("Stock Price"),Guide.manual_color_key("", ["Analytical Solution","Euler-Maruyama Method","Milstein Method"],["#FF0000","#1E90FF","#006400"],pos=[0.05w,-0.45h],shape=[Gadfly.Shape.hline,Gadfly.Shape.hline,Gadfly.Shape.xcross]),Coord.cartesian(xmin=0, xmax=1, ymin=92, ymax=112))

layer5 = layer(x=xs, y=Milstein_Diff, Geom.line,Theme(line_width = 1pt,default_color=colorant"#006400"))
layer1 = layer(x=xs, y=Euler_Maruyama_Diff, Geom.line,Theme(line_width = 1pt,default_color=colorant"#1E90FF"))
p2 = Gadfly.plot(layer1,layer5,Guide.yticks(ticks = [-4.0 -3.0 -2.0 -1.0 0]), Guide.xticks(ticks = [0 0.25 0.5 0.75 1]),Theme(major_label_font="times-new-roman",minor_label_font="times-new-roman",key_label_font="times-new-roman",grid_line_width=0mm,panel_stroke=color("black"),key_label_font_size=6pt,major_label_font_size=8pt),Guide.xlabel("Time"),Guide.ylabel("log₁₀ Absolute Error"),Guide.manual_color_key("", ["Euler-Maruyama Error","Milstein Error"],["#1E90FF","#006400"],pos=[0.05w,-0.45h],shape=[Gadfly.Shape.hline,Gadfly.Shape.hline]))

plt = hstack(p1,p2)

This is how it is displayed with the changed font style in the notebook (which I want):

This is how it is saved, as you can see the font has not been changed:

How do I save it with the changed font?

@Mattriks have you come across this problem? A similar problem is unanswered from a few years ago draw - How to save plots with the correct theme (local font) using Gadfly in Julia language? - Stack Overflow .

On linux, all linux fonts work for me, both in Jupyter and saving Gadfly plots to pdf. The font in your example is a MS font (does it work if you change the font to “Times” or “TimesNewRoman”?). Gadfly is a program that “makes plots”, but the graphics output is handled by other packages (in the case of PDF, by Cairo.jl, check there). I also found the issue mentioned at Gadfly.jl: draw function removes specified font · Issue #621 · GiovineItalia/Gadfly.jl · GitHub.

1 Like

Hi @ChrisVL1 Sorry, I couldn’t run your code - it’s sometimes useful to provide a tiny example that shows the problem. Also, with anything to do with these pesky fonts, it might be important to know which OS you’re running, and whether you’ve installed any.

Anyway, if you’re not too fussy about the exact font, you could try specifying the ‘font name’ as "serif" - if you’re lucky Cairo and Fontconfig will find a suitable Times font that matches. In fact on my Mac it finds the Linotype version of Times Roman rather than the Monotype Times New Roman. I don’t know why…

It’s also possible to specify and get "Palatino" (or "Optima" or "Marker Felt" (because they’re Mac system fonts and always available). I’ve not been able to obtain “Times New Roman” in a PDF from Gadfly.jl (unless I take steps to install and activate it first). :man_shrugging:

1 Like

Hi @cormullion, @Mattriks response of using the exact font name (i.e. “Times” or “TimesNewRoman”) solved the issue, but thanks for the recommendations.

While you are here, what is the best way to save plots? The font comes out strange when saving as a PDF, specifically the spacing between letters.

Not sure about “spacing between letters”, but does your issue match anything in Overlapping labels for the top and bottom legend when drawing to PNG/PDF · Issue #688 · GiovineItalia/Gadfly.jl · GitHub or https://github.com/GiovineItalia/Gadfly.jl/issues/1587?

A mwe to try could be something like:

fnt1 = "TimesNewRoman"
p = plot(Geom.blank,
 Theme(grid_line_width=0mm, panel_stroke="black", major_label_font=fnt1, minor_label_font=fnt1, major_label_font_size=8pt,
        key_label_font=fnt1, key_label_font_size=6pt),
  Guide.xlabel("Time"), Guide.ylabel("Stock Price"),
  Guide.manual_color_key("", ["Analytical Solution", "Euler-Maruyama Method", "Milstein Method"],
  ["#FF0000","#1E90FF","#006400"], pos=[0.05w,-0.40h],
 shape=[Gadfly.Shape.hline, Gadfly.Shape.hline, Gadfly.Shape.xcross])
)

draw(PDF(string("font_", fnt1,".pdf"), 3.3inch, 3.3inch), p)
1 Like