0

Plot features, such as background hatching, line width and font sizes are lost when saving as a PDF as opposed to saving as a PNG. I would like it to look like the PNG version, but as a PDF for better image resolution. I'm using Python 2.7.

Here is an example saved as PNG: enter image description here

And an example saved as a PDF: enter image description here

My code:

fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(10, 5)), sharex=True)

# SET A
axes[0].plot(daysA.loc[0].values, color=colours[0], lw=1.5, zorder=2)

axes[0].axvspan(0, 288, alpha=0.3, color='grey', hatch='/', zorder=1)
axes[0].axvspan(288, 576, alpha=0.3, color='grey', hatch='o', zorder=1)
axes[0].axvspan(576, 864, alpha=0.3, color='grey', hatch='-', zorder=1)

axes[0].set_ylabel('Label A')

# SET B
axes[1].plot(daysB.loc[0].values, color=colours[0], lw=1.5)

axes[1].axvspan(0, 288, alpha=0.3, color='grey', hatch='/')
axes[1].axvspan(288, 576, alpha=0.3, color='grey', hatch='o')
axes[1].axvspan(576, 864, alpha=0.3, color='grey', hatch='-')

axes[1].set_ylabel('Label B')

# SET C
axes[2].plot(dayC.loc[0].values, color=colours[0], lw=1.5)

axes[2].axvspan(0, 288, alpha=0.3, color='grey', hatch='/')
axes[2].axvspan(288, 576, alpha=0.3, color='grey', hatch='o')
axes[2].axvspan(576, 864, alpha=0.3, color='grey', hatch='-')

axes[2].set_ylabel('Label C')

for ax in axes:
    ax.tick_params(axis='both', which='major', labelsize=10)
    ax.tick_params(axis='both', which='minor', labelsize=8)
    start, end = ax.get_ylim()
    stepsize = (end - start) / 4
    ax.yaxis.set_ticks(np.arange(start, end, stepsize))

plt.xticks(range(0, 864, 72), ['00:00', '12:00', '00:00', '12:00', '00:00', '12:00',
                               '00:00', '12:00', '00:00', '12:00', '00:00', '12:00'],
           rotation=45, fontsize=12)
plt.xlabel('Time of day')
plt.xlim(0, 864)
plt.subplots_adjust(hspace=0, bottom=0.22)
plt.setp([a.get_xticklabels() for a in fig.axes[:-1]], visible=False)

plt.savefig('/Users/ME/Desktop/PosterPlots/pdfexample.pdf')
plt.savefig('/Users/ME/Desktop/PosterPlots/pngexample.png')

plt.show()

What could be causing this and how can I fix it?

I have tried using zorder as suggested here. This didn't work, though I may have used it incorrectly.

MoMiJi
  • 93
  • 1
  • 1
  • 9
  • Possible duplicate of [Matplotlib Plots Lose Transparency When Saving as .ps/.eps](https://stackoverflow.com/questions/19638773/matplotlib-plots-lose-transparency-when-saving-as-ps-eps) – DavidG Jul 27 '18 at 12:08
  • 1
    Specifically [this](https://stackoverflow.com/a/19640319/5851928) answer also works for pdf – DavidG Jul 27 '18 at 12:09

0 Answers0