Blending maps using
This example shows how to blend two maps using
matplotlib by itself provides only alpha-based transparency for
superimposing one image onto another, which can be restrictive when trying to
create visually appealing composite images.
mplcairo is an enhancement for
matplotlib that provides support for
cairo’s compositing operators,
which include a wide range of
blend modes for image overlays.
We need to tell
matplotlib to use a backend from
backend formally needs to be set prior to importing
mplcairo.qt GUI backend should work on Linux and Windows, but
you will need to use something different on macOS or Jupyter (see
mplcairo’s usage notes).
We can now import everything else.
import matplotlib.pyplot as plt from mplcairo import operator_t import astropy.units as u import sunpy.data.sample import sunpy.map from sunpy.coordinates import Helioprojective
Let’s load two maps for blending. We reproject the second map to the
coordinate frame of the first map for proper compositing, taking care to use
context manager in order to preserve off-disk data.
Let’s first plot the two maps individually.
<matplotlib.image.AxesImage object at 0x7fb37f4b1510>
We now plot the two maps on the same axes. If the plot were rendered at this
point, the second map would completely obscure the first map. We save the
matplotlib artist returned when plotting the second map (
We invoke the
mplcairo operator for the
screen blend mode
to modify the artist for the second map. The second map will
now be composited onto the first map using that blend mode.
Finally, we set the title and render the plot.
Total running time of the script: (0 minutes 3.301 seconds)