0

This question is related to a comment on another question.

In matplotlib/python, I am trying to rasterize a specific element in my image and save it to eps. The issue is that when saving to EPS (but not SVG or PDF), a black background appears behind the rasterized element.

Saving to PDF and converting to EPS does not seem to be a reasonable solution, as there are weird pdf2ps and pdftops conversion issues that make understanding bounding boxes very ... scary (or worse, seemingly inconsistent). My current work around involves a convoluted process of saving in svg and export in Inkscape, but this should also not be required.

Here is the sample code needed to reproduce the problem. Matplotlib and Numpy will be needed. If the file is saved to mpl-issue.py, then it can be run with:

python mpl-issue.py

#!/usr/bin/env python
# mpl-issue.py

import numpy as np
import matplotlib as mpl

# change backend to agg
# must be done prior to importing pyplot
mpl.use('agg')

import matplotlib.pyplot as plt

def transparencytest():
    # create a figure and some axes
    f = plt.figure()
    a = {
        'top': f.add_subplot(211),
        'bottom': f.add_subplot(212),
    }

    # create some test data
    # obviously different data on the subfigures
    # just for demonstration
    x = np.arange(100)
    y = np.random.rand(len(x))
    y_lower = y - 0.1
    y_upper = y + 0.1

    # a rasterized version with alpha
    a['top'].fill_between(x, y_lower, y_upper, facecolor='yellow', alpha=0.5, rasterized=True)

    # a rasterized whole axis, just for comparison
    a['bottom'].set_rasterized(True)
    a['bottom'].plot(x, y)

    # save the figure, with the rasterized part at 300 dpi
    f.savefig('testing.eps', dpi=300)
    f.savefig('testing.png', dpi=300)
    plt.close(f)

if __name__ == '__main__':
    print plt.get_backend()
    transparencytest()

The testing.png image looks like this: testing.png

The testing.eps image ends up looks like this (in converted pdf versions and the figure-rasterized png): testing.eps

The black backgrounds behind the rasterized elements are not supposed to be there. How can I remove the black backgrounds when saving an eps figure with rasterized elements in it?

This has been tested with a bunch of other mpl backends, so it does not appear to be a specific problem with agg. Mac OS X 10.9.4, Python 2.7.8 built from MacPorts, Matplotlib 1.3.1.

Community
  • 1
  • 1
rocking_ellipse
  • 275
  • 1
  • 2
  • 13

1 Answers1

0

This was a known bug which has been fixed.

This is due to the fact that eps does not know about transparency and the default background color for the rasterization was (0, 0, 0, 0) (black which is fully transparent).

I also had this problem (https://github.com/matplotlib/matplotlib/issues/2473) and it is fixed (https://github.com/matplotlib/matplotlib/pull/2479) in matplotlib 1.4.0 which was released last night.

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • That is sort of great timing. I will be happy to accept this as a correct answer as soon as I can get ahold of the new version and test. Is there a change I can make locally to my mpl 1.3.1, or would it need to be recompiled? (It appears to be in source.) – rocking_ellipse Aug 27 '14 at 20:02
  • If I recall correctly the change was in the c extension so you would bed to re compile. What platform are you on? – tacaswell Aug 27 '14 at 20:40