Issue 1:
Bug report

Bug summary

Colours in saved matplotlib svg files not the same as displayed originally.

Code for reproduction

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()

Actual outcome

An SVG file containing:

Expected outcome

An SVG file containing:

Matplotlib version

Operating system: Windows 10

Matplotlib version: 3.2.1

Matplotlib backend: module://ipykernel.pylab.backend_inline

Python version: 3.7.7

Jupyter version (if applicable): jupyter core: 4.6.3, jupyter-notebook: 6.0.3, jupyter client: 6.1.3, jupyter lab: 2.1.1

Other libraries:

installed matplotlib with anaconda then pip to mpl 3.2.1

Issue 2:
On master.

The following test is taken from test_images.test_image_composite_alpha:

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()

By default, Agg produces the following result:

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:

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):
"""

With the patch on, the result is clearly different.

I haven't looked much into which one is correct (it should be easy to manually compute the result), although https://github.com/anntzer/mpl_cairo seems to agree visually with Matplotlib, not Agg.