Python Matplotlib: Remove black background when rasterizing part of plot in EPS?
Question: This question is related to [a comment on another
question](http://stackoverflow.com/questions/19638773/matplotlib-plots-lose-
transparency-when-saving-as-ps-eps#comment29161375_19640319).

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](http://imgur.com/1PMd9e4.png)

The `testing.eps` image ends up looks like this (in converted pdf versions and
the figure-rasterized png): ![testing.eps](http://imgur.com/aunozUx.png)

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.


Answer: 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.

