Plots.jl icon indicating copy to clipboard operation
Plots.jl copied to clipboard

[BUG] Issues with saving plot as pdf

Open bhache opened this issue 4 years ago • 7 comments

Details

I was trying to save plots generated using gr() backend in pdf format and this messes up the negative values along x & y-axes. This doesn’t seem to happen when the figure is saved as png though. I isolated this issue to the plot() argument, fontfamily = font(48, "helvetica"). I am attaching the MWE I used below

using Plots

gr()
plot(-5:5, -5:5, title = "title", xlabel = "x", ylabel = "y", label = "1", fontfamily = font(48, "helvetica"))
savefig("./test.pdf")
savefig("./test.png")
  1. Figure saved as png test

  2. Figure saved as pdf test.pdf

Backends

This bug occurs on ( insert x below )

Backend yes no untested
gr (default) x
pyplot x
plotly x
plotlyjs x
pgfplotsx x
inspectdr x

Versions

Plots.jl version: Plots v1.6.6 Backend version (]st -m): GR v0.52.0 Output of versioninfo(): Julia Version 1.5.2 Commit 539f3ce943 (2020-09-23 23:17 UTC) Platform Info: OS: macOS (x86_64-apple-darwin18.7.0) CPU: Intel(R) Core(TM) i5-5287U CPU @ 2.90GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-9.0.1 (ORCJIT, broadwell) Environment: JULIA_NUM_THREADS = 2

bhache avatar Sep 24 '20 22:09 bhache

It's possible that this is caused by an issue the underlying GR backend's pdf driver: https://gr-framework.org/encoding.html

dapplegate avatar Feb 08 '21 18:02 dapplegate

I just wanted to point out that I am having the same issue with the Times Roman font. Minimum working example below:

using Plots
gr()

x = -10:10
p = plot(x,x, ytickfont = Plots.font("times"))
savefig(p,"asPNG.png")
savefig(p,"asPDF.pdf")

The error does not occur with certain other fonts, e.g. Computer Modern.

nfernandez-arias avatar Aug 27 '21 22:08 nfernandez-arias

I find this issue still holds for the "-" sign. It holds for many fonts, except Computer Modern, sans-serif, and serif-roman. The pdf savefig() also exports/renders fonts in a strange scale/shape, such as Bookman or Palatino does not even look like Palatino.

Is there a way to setup the encoding or how the pdf render works for Plots ?

This is a code I run on MacOS, but it holds years now.

using Plots
# _ Fonts
ftb = ["times", "Helvetica", "courier", "Computer Modern", "bookman", "newcenturyschlbk", "avantgarde", "Palatino", "serif-roman", "sans-serif"]

# _ different backends, tested only gr() in Plots
backend_x = "plots_gr"
gr()

x = [randn(50) for i in 1:3]
xt = LinRange(-2,2,50)

plots_array = []
for (i, fi) in enumerate(ftb)
    # label tests - sign in other texts as labels or legend
    p1 = scatter(xt, x, label=["x1 (-1,1)" "x2 +4" "x3 -98"], title="font $fi", framestyle=:box, fontfamily=fi, size=(400,400), ylabel="-3 -text", xlabel="-3 -text")
    push!(plots_array, p1)
    display(p1)
    savefig(p1, "plots/problem/plot_b_$(backend_x)_f_$fi.pdf")
end

[display(p) for p in plots_array]

luboshanus avatar Jan 27 '23 10:01 luboshanus

It does not seem to be resolved yet. I am using fontfamily="Palatino" and I am getting ? in place of - when I am saving plots as PDF. Has anyone found a solution or a workaround? Thanks!

moshialam avatar Aug 25 '23 03:08 moshialam

I'm getting the same error. I'm also on Mac and trying to save a pdf of the plot with Palatino as the fontface. It would be great if anyone has a solution.

mauricioabur avatar Sep 03 '23 22:09 mauricioabur

Same error here, font is Helvetica.

marvinlenk avatar Apr 30 '24 12:04 marvinlenk

When on macOS, “Arial” works ok.

luboshanus avatar Apr 30 '24 12:04 luboshanus