Matplotlib Backend Differences between Agg and Cairo

前端 未结 1 1247
陌清茗
陌清茗 2020-12-31 03:12

Hej,

I\'d like to produce high quality PDFs from matplotlib plots. Using other code, I have produced a large array of numbers, which I plot in a figure using plt.ims

相关标签:
1条回答
  • 2020-12-31 03:35

    As suggested by steabert in the comments above, a workaround is exporting the graphics in a different format and then convert it to PDF afterwards. Adjusting my example from above, the workflow could look something like this:

    import os
    import matplotlib as mpl
    mpl.use("Agg")
    
    import numpy as np
    import matplotlib.pyplot as plt
    plt.rcParams['text.usetex'] = True
    
    data = np.random.rand(50, 50)
    
    plt.imshow(data, interpolation='nearest')
    plt.xlabel('X Label')
    plt.savefig('agg.eps')
    
    os.system('epspdf agg.eps agg.pdf')
    

    producing a file of 16 Kb which looks good. There is still one difference to the examples presented above: Using the (E)PS pipeline seems to ignore the interpolation='nearest' option, i.e. the image appears blurry in the final PDF. Luckily, I can live with that, but it might be interesting to look into this issue.

    0 讨论(0)
提交回复
热议问题