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

White lines in saved pdf-file

Open PhilippAumann opened this issue 7 years ago • 13 comments

Hi, thank you for developing this great tool! I am using Plots with the pyplot backend. When I save a Plot with heatmaps as subplots, the saved figure shows white lines in the pdf-viewer. This is a native pyplot problem, as it is discussed here. I was facing the same problem when using pyplot's pcolor in Python. The workaround there is something like setting rasterized = true. Is there a workaround in Plots.jl?

PhilippAumann avatar Jan 09 '18 09:01 PhilippAumann

Hi, thanks a lot for reporting! Unfortunately I cannot reproduce this, which makes it hard to fix or check if it is fixed. Since you already proposed a solution it would be great if you could check locally if adding rasterized = true, somewhere here https://github.com/JuliaPlots/Plots.jl/blob/master/src/backends/pyplot.jl#L767-L774 fixes the issue for you. If it does, a PR is very welcome!

daschw avatar Jan 09 '18 12:01 daschw

I have the same problem. @PhilippAumann did you find a solution?

SebastianAment avatar Oct 03 '20 16:10 SebastianAment

@SebastianAment could you provide a MWE to reproduce the issue?

daschw avatar Oct 03 '20 21:10 daschw

@daschw the issue appears to be with savefig and the PDF format.

using Plots
pyplot()
n = 64
z = ones(n, n)
f = heatmap(z)
gui() # this shows the heatmap without the lines
savefig(f, "test.pdf") # the pdf has white lines between cells

I tested several output formats on two machines, one on Linux and one on MacOS. PNG works as expected, but PDF and others like PS include lines*. I have attached the images which result from the example on my machine:

test.pdf test.png

*PS only includes horizontal lines, while the PDF has both horizontal and vertical lines.

SebastianAment avatar Oct 13 '20 15:10 SebastianAment

On Windows with pyplot() and heatmap, I get the same problem with savefig in pdf (and not in png). Does someone have a solution for this issue?

dmetivie avatar May 28 '21 10:05 dmetivie

I have the same issue, in Linux and Windows.

jamblejoe avatar Oct 12 '22 13:10 jamblejoe

According to the documentation of matplotlib the rasterization can be controlled by the zorder by setting set_rasterization_zorder(...) on the axis. In Julia:

const plt = import PyPlot
plt.pcolormesh(rand(10,10); zorder=-1)
plt.title("title")
plt.gca()."set_rasterization_zorder"(0)
plt.savefig("pyplot.pdf")

correctly rasterizes the heatmap but not the ticks, labels, title etc pyplot.pdf

The same can be achieved by

plt.pcolormesh(rand(10,10); rasterized=true)
plt.title("title")
plt.savefig("pyplot2.pdf")

pyplot2.pdf

I do not know how to implement in Plots.jl. I guess the latter approach is the easier one to implement. To former has the advantage that a multiple parts of a plot can be rasterized into one image, see this stackexchange post. Maybe @daschw or @BeastyBlacksmith can help?

jamblejoe avatar Oct 12 '22 15:10 jamblejoe

CC: @isentropic

BeastyBlacksmith avatar Oct 13 '22 07:10 BeastyBlacksmith

It would be no problem to add rasterized = True extra kw (https://docs.juliaplots.org/stable/backends/#Fine-tuning-3) flag if that would solve it. I personally encountered this issue before but its hard to tackle as the rendering is completely handled by matplotlib. Would this solve the issue at least partially?

isentropic avatar Oct 17 '22 10:10 isentropic

Having this flag True by default might break some plots I'm afraid (esp. for people who render pdfs for papers). So we can make it optional by default

isentropic avatar Oct 17 '22 10:10 isentropic

@isentropic How does this work?

heatmap(rand(10,10); extra_kwargs=Dict(:subplot=>Dict("rasterized"=>true)))

does not seem to have any effect in vscode plotting pane or in saved pdf.

Julia-1.9
Plots v1.39.0
PythonPlot v1.0.2

Here is an example output in the vscode plotting pane: Bildschirm­foto 2023-11-03 um 11 45 10

jamblejoe avatar Nov 03 '23 10:11 jamblejoe

Setting fmt=:png does not have an effect on the output figure in the vscode plotting pane nor on the saved pdf file.

It has an effect of the output in Pluto.jl: Bildschirm­foto 2023-11-03 um 11 48 24

jamblejoe avatar Nov 03 '23 10:11 jamblejoe

This has not been implemented yet. I wanted to know if this is any useful

isentropic avatar Nov 06 '23 05:11 isentropic