Search code examples
pythonmatplotlibpdfdpi

Is dpi relevant when figure is saved as PDF?


If I am saving figures as PDF, is dpi still relevant? For example, in the following code

import pylab

pylab.savefig('./test_200.pdf', dpi = 200)
pylab.savefig('./test_2000.pdf', dpi = 2000)

does dpi make a difference?

To me it does not make a difference at least in resolution, I have zoomed in as much as I can and the two figures look the same

dpi

Is it possible that there are any underlying difference or simply no difference? Thank you in advance!


Solution

  • Well, it will make a difference as soon as anything gets converted into a raster image, e.g.:

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot()
    ax.set_rasterization_zorder(0)
    
    ax.scatter([0,1,2],[3,4,5], zorder=-1)
    ax.scatter([0,1,2],[4,5,6], zorder=2)
    
    plt.savefig('/tmp/test_20.pdf', dpi=20)
    plt.savefig('/tmp/test_2000.pdf', dpi=2000)
    

    Note how the lower row of scatter points is rasterized (due to its zorder being kept below the "rasterization threshold", as defined via ax.set_rasterization_zorder. The figure saved with 20 dpi now looks like this:

    Low rasterization example