6

I'm trying to change the axis background on a plot where several imshow() calls render images in various locations via the extent parameter.

When I save a pdf of the figure using savefig(), I lose the background color if the axis displays more than one image. Note that this doesn't happen when exporting a png of the same figure.

Here's a minimal script illustrating the problem:

import matplotlib.pyplot as plt
from numpy.random import rand

fig, ax = plt.subplots(nrows=3, ncols=1, sharex=True)

ax[0].imshow(rand(15,15), extent=[0, 2, 15, 0], \
            cmap=plt.cm.gray, aspect='auto', interpolation='Nearest')
ax[0].set_axis_bgcolor('k')

ax[1].imshow(rand(15,15), extent=[0, 2, 15, 0], \
            cmap=plt.cm.gray, aspect='auto', interpolation='Nearest')
ax[1].imshow(rand(15,15), extent=[4, 6, 15, 0], \
            cmap=plt.cm.gray, aspect='auto', interpolation='Nearest')
ax[1].set_axis_bgcolor('k')

ax[2].imshow(rand(15,15), extent=[0, 2, 15, 0], \
            cmap=plt.cm.gray, aspect='auto', interpolation='Nearest')
ax[2].imshow(rand(15,15), extent=[4, 6, 15, 0], \
            cmap=plt.cm.gray, aspect='auto', interpolation='Nearest')
ax[2].imshow(rand(15,15), extent=[8, 10, 15, 0], \
            cmap=plt.cm.gray, aspect='auto', interpolation='Nearest')
ax[2].set_axis_bgcolor('k')

ax[-1].set_xlim([0, 12])
fig.savefig('test.pdf', format='PDF')
fig.savefig('test.png', format='PNG')

This is the pdf output of the script (the eps output is the same):

test.pdf

And this is the expected output of the script (saved as a png):

test.png

Have I bumped into a matplotlib bug, or is there some command I'm missing that will fix the pdf output?

EDIT: I've re-plotted the figures with a default matplotlibrc.

fgb
  • 3,009
  • 1
  • 18
  • 23
  • I just tried running your script and the .pdf and .png versions both have the correct backgrounds. Which version of `matplotlib` are you using? Mine is `1.2.0`. – ali_m Apr 08 '13 at 18:12
  • I'm running 1.2.1 at the moment, but have tried it on an older version with the same result. I'm also running it on OS X. – fgb Apr 08 '13 at 18:54
  • 1
    It looks like you're using some fancy math-style serif fonts which are different to the default. I wonder if some setting defined in your `matplotlibrc` file might be messing up PDF rendering. Maybe try renaming the file so that you use `matplotlib` with the default parameters? And another thought - does it render correctly as an EPS file? – ali_m Apr 08 '13 at 19:11
  • @ali_m: Those fonts are rendered by LaTeX. I've renamed the `matplotlibrc` configuration file and uploaded updated figures. The EPS output is the same as the PDF output. I'm beginning to believe this might have to do with the `MacOSX` backend. – fgb Apr 08 '13 at 19:25
  • This question was due to a bug that has since been fixed. – Jon 'links in bio' Ericson Apr 19 '14 at 00:32

2 Answers2

6

This ended up being a matplotlib bug.

When rendering more than one image on the same axes, a composite image is created that does not have a transparent background when rendering to pdf, so the background color of the axes does not show through.

This got resolved as part of an issue I opened in the matplotlib's GitHub repo.

fgb
  • 3,009
  • 1
  • 18
  • 23
2

Look into your matplotlibrc. There is a section of options starting with savefig which define how your saved figure will look like. Even default matplotlibrc have this section.

There is also a similar question: matplotlib savefig() plots different from show()

Community
  • 1
  • 1
varepsilon
  • 488
  • 5
  • 8
  • This isn't the problem though. I'm not comparing the output of `show()` to the output of `savefig()`, but rather that of the latter to a choice of output formats. My problem ended up being a matplotlib bug. – fgb Apr 11 '13 at 21:28