Skip to content

WIP: Add blend modes for compositing, supported by Agg backend#31162

Draft
ayshih wants to merge 2 commits intomatplotlib:mainfrom
ayshih:agg_compositing
Draft

WIP: Add blend modes for compositing, supported by Agg backend#31162
ayshih wants to merge 2 commits intomatplotlib:mainfrom
ayshih:agg_compositing

Conversation

@ayshih
Copy link
Contributor

@ayshih ayshih commented Feb 15, 2026

PR summary

This PR adds support for blend modes beyond alpha blending (e.g., "screen" or "hard light"). With this PR, all artists can specify blend_mode, and the Agg backend supports them. So, this PR fixes #6210 for the default backend.

Of course, mplcairo can be used to access these blend modes, but this provides support without cairo.

Thoughts?

Example with this PR

Figure_1

Generating code

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

N = 10

data = np.arange(N**2).reshape((N, N)) % (N-1)

fig, axs = plt.subplots(3, 7, figsize=(10, 6), layout="tight")
axs = axs.flatten()
fig.set_facecolor("none")

blend_modes = ["clear", "source", "over", "in", "out", "atop",
               "xor", "plus",
               "multiply", "screen", "overlay", "darken", "lighten",
               "color_dodge", "color_burn", "hard_light", "soft_light",
               "difference", "exclusion"]

for i in range(len(axs)):
    axs[i].set_facecolor("none")
    axs[i].set_xlim(0, 1)
    axs[i].set_ylim(0, 1.5)
    axs[i].set_axis_off()

for i in range(len(blend_modes)):
    axs[i].imshow(data, cmap='Reds', extent=(0, 1, 0, 1))
    axs[i].imshow(data[::-1, :], cmap='Blues', extent=(0, 1, 0.5, 1.5), blend_mode=blend_modes[i])
    axs[i].text(0.25, 0.25, "Test", weight="bold", color="c", blend_mode=blend_modes[i])
    axs[i].plot([0, 1], [1.5, 0], color="y", blend_mode=blend_modes[i])

    rect = Rectangle((0, 1.5), 1, .3, facecolor='gray', clip_on=False)
    axs[i].add_artist(rect)
    axs[i].set_title(blend_modes[i])

plt.show()

TODO:

  • Fix the RGBA8 blender (again) for blending accuracy
  • Check every type of artist
  • Look into adding support by the SVG backend
  • Look into adding support by the PDF backend
  • Implement hue/saturation/color/luminosity operators in Agg

PR checklist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Alternative compositing methods

1 participant