<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong></p>

<p dir="auto">Colours in saved matplotlib svg files not the same as displayed originally.</p>
<p dir="auto"><strong>Code for reproduction</strong></p>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

cdict0 = {'red':   ((0.0, 1.0, 1.0),
                    (0.015, 1.0, 1.0),
                    (1.0, 1.0, 0.0)),

         'green':  ((0.0, 0.0, 1.0),
                    (0.015, 1.0, 1.0),
                    (1.0, 0.0, 0.0)),

         'blue':   ((0.0, 0.0, 1.0),
                    (0.015, 1.0, 1.0),
                    (1.0, 0.0, 0.0))
                   
         }


cdict1 = {'red':   ((0.0, 1.0, 1.0),
                   (1.0, 0.0, 0.0)),

         'green': ((0.0, 1.0, 1.0),
                   (1.0, 1.0, 0.0)),

         'blue':  ((0.0, 1.0, 1.0),
                   (1.0, 1.0, 0.0))
                   
         }

red = mpl.colors.LinearSegmentedColormap('Red Map', cdict0, N = 1000000)

cyan = mpl.colors.LinearSegmentedColormap('Cyan Map', cdict1, N = 1000000)

rng = np.random.default_rng(42)
data = rng.random((256, 256))
data_1 = rng.random((256, 256))
plt.imshow(data, alpha = 1, cmap = red)
plt.imshow(data_1, alpha = .5, cmap = cyan)
plt.axis('off')
plt.savefig('fig.svg')
plt.show()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span> <span class="pl-k">as</span> <span class="pl-s1">mpl</span>

<span class="pl-s1">cdict0</span> <span class="pl-c1">=</span> {<span class="pl-s">'red'</span>:   ((<span class="pl-c1">0.0</span>, <span class="pl-c1">1.0</span>, <span class="pl-c1">1.0</span>),
                    (<span class="pl-c1">0.015</span>, <span class="pl-c1">1.0</span>, <span class="pl-c1">1.0</span>),
                    (<span class="pl-c1">1.0</span>, <span class="pl-c1">1.0</span>, <span class="pl-c1">0.0</span>)),

         <span class="pl-s">'green'</span>:  ((<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>, <span class="pl-c1">1.0</span>),
                    (<span class="pl-c1">0.015</span>, <span class="pl-c1">1.0</span>, <span class="pl-c1">1.0</span>),
                    (<span class="pl-c1">1.0</span>, <span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>)),

         <span class="pl-s">'blue'</span>:   ((<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>, <span class="pl-c1">1.0</span>),
                    (<span class="pl-c1">0.015</span>, <span class="pl-c1">1.0</span>, <span class="pl-c1">1.0</span>),
                    (<span class="pl-c1">1.0</span>, <span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>))
                   
         }


<span class="pl-s1">cdict1</span> <span class="pl-c1">=</span> {<span class="pl-s">'red'</span>:   ((<span class="pl-c1">0.0</span>, <span class="pl-c1">1.0</span>, <span class="pl-c1">1.0</span>),
                   (<span class="pl-c1">1.0</span>, <span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>)),

         <span class="pl-s">'green'</span>: ((<span class="pl-c1">0.0</span>, <span class="pl-c1">1.0</span>, <span class="pl-c1">1.0</span>),
                   (<span class="pl-c1">1.0</span>, <span class="pl-c1">1.0</span>, <span class="pl-c1">0.0</span>)),

         <span class="pl-s">'blue'</span>:  ((<span class="pl-c1">0.0</span>, <span class="pl-c1">1.0</span>, <span class="pl-c1">1.0</span>),
                   (<span class="pl-c1">1.0</span>, <span class="pl-c1">1.0</span>, <span class="pl-c1">0.0</span>))
                   
         }

<span class="pl-s1">red</span> <span class="pl-c1">=</span> <span class="pl-s1">mpl</span>.<span class="pl-s1">colors</span>.<span class="pl-v">LinearSegmentedColormap</span>(<span class="pl-s">'Red Map'</span>, <span class="pl-s1">cdict0</span>, <span class="pl-v">N</span> <span class="pl-c1">=</span> <span class="pl-c1">1000000</span>)

<span class="pl-s1">cyan</span> <span class="pl-c1">=</span> <span class="pl-s1">mpl</span>.<span class="pl-s1">colors</span>.<span class="pl-v">LinearSegmentedColormap</span>(<span class="pl-s">'Cyan Map'</span>, <span class="pl-s1">cdict1</span>, <span class="pl-v">N</span> <span class="pl-c1">=</span> <span class="pl-c1">1000000</span>)

<span class="pl-s1">rng</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">default_rng</span>(<span class="pl-c1">42</span>)
<span class="pl-s1">data</span> <span class="pl-c1">=</span> <span class="pl-s1">rng</span>.<span class="pl-en">random</span>((<span class="pl-c1">256</span>, <span class="pl-c1">256</span>))
<span class="pl-s1">data_1</span> <span class="pl-c1">=</span> <span class="pl-s1">rng</span>.<span class="pl-en">random</span>((<span class="pl-c1">256</span>, <span class="pl-c1">256</span>))
<span class="pl-s1">plt</span>.<span class="pl-en">imshow</span>(<span class="pl-s1">data</span>, <span class="pl-s1">alpha</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span>, <span class="pl-s1">cmap</span> <span class="pl-c1">=</span> <span class="pl-s1">red</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">imshow</span>(<span class="pl-s1">data_1</span>, <span class="pl-s1">alpha</span> <span class="pl-c1">=</span> <span class="pl-c1">.5</span>, <span class="pl-s1">cmap</span> <span class="pl-c1">=</span> <span class="pl-s1">cyan</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">axis</span>(<span class="pl-s">'off'</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'fig.svg'</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">show</span>()</pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>

<p dir="auto">An SVG file containing:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/64583949/82458993-4e30d980-9aaf-11ea-8d4f-8fa83ace29b9.png"><img src="https://user-images.githubusercontent.com/64583949/82458993-4e30d980-9aaf-11ea-8d4f-8fa83ace29b9.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Expected outcome</strong><br>
An SVG file containing:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/64583949/82458856-1e81d180-9aaf-11ea-894d-d6e324525407.png"><img src="https://user-images.githubusercontent.com/64583949/82458856-1e81d180-9aaf-11ea-894d-d6e324525407.png" alt="download" style="max-width: 100%;"></a></p>


<p dir="auto"><strong>Matplotlib version</strong></p>

<ul dir="auto">
<li>Operating system: Windows 10</li>
<li>Matplotlib version: 3.2.1</li>
<li>Matplotlib backend: module://ipykernel.pylab.backend_inline</li>
<li>Python version: 3.7.7</li>
<li>Jupyter version (if applicable): jupyter core: 4.6.3, jupyter-notebook: 6.0.3, jupyter client: 6.1.3, jupyter lab: 2.1.1</li>
<li>Other libraries:<br>
installed matplotlib with anaconda then pip to mpl 3.2.1</li>
</ul>
<p dir="auto">On master.</p>
<p dir="auto">The following test is taken from test_images.test_image_composite_alpha:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from pylab import *

fig = plt.figure()
ax = fig.add_subplot(111)
arr = np.zeros((11, 21, 4))
arr[:, :, 0] = 1
arr[:, :, 3] = np.concatenate((np.arange(0, 1.1, 0.1), np.arange(0, 1, 0.1)[::-1]))
arr2 = np.zeros((21, 11, 4))
arr2[:, :, 0] = 1
arr2[:, :, 1] = 1
arr2[:, :, 3] = np.concatenate((np.arange(0, 1.1, 0.1), np.arange(0, 1, 0.1)[::-1]))[:, np.newaxis]
ax.imshow(arr, extent=[1, 2, 5, 0], alpha=0.3)
ax.imshow(arr, extent=[2, 3, 5, 0], alpha=0.6)
ax.imshow(arr, extent=[3, 4, 5, 0])
ax.imshow(arr2, extent=[0, 5, 1, 2])
ax.imshow(arr2, extent=[0, 5, 2, 3], alpha=0.6)
ax.imshow(arr2, extent=[0, 5, 3, 4], alpha=0.3)
ax.set_facecolor((0, 0.5, 0, 1))
ax.set_xlim([0, 5])
ax.set_ylim([5, 0])

show()"><pre class="notranslate"><code class="notranslate">from pylab import *

fig = plt.figure()
ax = fig.add_subplot(111)
arr = np.zeros((11, 21, 4))
arr[:, :, 0] = 1
arr[:, :, 3] = np.concatenate((np.arange(0, 1.1, 0.1), np.arange(0, 1, 0.1)[::-1]))
arr2 = np.zeros((21, 11, 4))
arr2[:, :, 0] = 1
arr2[:, :, 1] = 1
arr2[:, :, 3] = np.concatenate((np.arange(0, 1.1, 0.1), np.arange(0, 1, 0.1)[::-1]))[:, np.newaxis]
ax.imshow(arr, extent=[1, 2, 5, 0], alpha=0.3)
ax.imshow(arr, extent=[2, 3, 5, 0], alpha=0.6)
ax.imshow(arr, extent=[3, 4, 5, 0])
ax.imshow(arr2, extent=[0, 5, 1, 2])
ax.imshow(arr2, extent=[0, 5, 2, 3], alpha=0.6)
ax.imshow(arr2, extent=[0, 5, 3, 4], alpha=0.3)
ax.set_facecolor((0, 0.5, 0, 1))
ax.set_xlim([0, 5])
ax.set_ylim([5, 0])

show()
</code></pre></div>
<p dir="auto">By default, Agg produces the following result:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/1322974/27981693-18e23188-6345-11e7-870e-43d515ac5ff9.png"><img src="https://user-images.githubusercontent.com/1322974/27981693-18e23188-6345-11e7-870e-43d515ac5ff9.png" alt="aggcomposed" style="max-width: 100%;"></a></p>
<p dir="auto">Matplotlib can also force software composition of overlapping images, which is, I guess, faster for vector backends (only one image ends up written to the file).  For Agg, this is disabled; the Agg renderer does the compositing itself.  But you can switch Agg back to use software composition with the following patch:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py
index c6af41b19..580cf0cf8 100644
--- a/lib/matplotlib/backends/backend_agg.py
+++ b/lib/matplotlib/backends/backend_agg.py
@@ -302,7 +302,7 @@ class RendererAgg(RendererBase):
         # It is generally faster to composite each image directly to
         # the Figure, and there's no file size benefit to compositing
         # with the Agg backend
-        return True
+        return False
 
     def option_scale_image(self):
         &quot;&quot;&quot;"><pre class="notranslate"><code class="notranslate">diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py
index c6af41b19..580cf0cf8 100644
--- a/lib/matplotlib/backends/backend_agg.py
+++ b/lib/matplotlib/backends/backend_agg.py
@@ -302,7 +302,7 @@ class RendererAgg(RendererBase):
         # It is generally faster to composite each image directly to
         # the Figure, and there's no file size benefit to compositing
         # with the Agg backend
-        return True
+        return False
 
     def option_scale_image(self):
         """
</code></pre></div>
<p dir="auto">With the patch on, the result is clearly different.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/1322974/27981712-5fad6452-6345-11e7-9ebf-f2967ccc058b.png"><img src="https://user-images.githubusercontent.com/1322974/27981712-5fad6452-6345-11e7-9ebf-f2967ccc058b.png" alt="mplcomposed" style="max-width: 100%;"></a></p>
<p dir="auto">I haven't looked much into which one is correct (it should be easy to manually compute the result), although <a href="https://github.com/anntzer/mpl_cairo">https://github.com/anntzer/mpl_cairo</a> seems to agree visually with Matplotlib, not Agg.</p>