Skip to content

Agg alpha image compositor differs from Matplotlib's software image compositor #8847

@anntzer

Description

@anntzer

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:
aggcomposed

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.
mplcomposed

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    keepItems to be ignored by the “Stale” Github Actiontopic: color/alpha

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions